Created
October 8, 2012 13:58
-
-
Save chrismcg/3852677 to your computer and use it in GitHub Desktop.
Sinatra and Localeapp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'sinatra' | |
gem 'localeapp' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# before running, create ./locales, bundle install with Gemfile below, and run | |
# bundle exec localeapp install --standalone <API_KEY> | |
require 'sinatra' | |
require 'i18n' | |
I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'locales', '*.yml').to_s] | |
require 'localeapp' | |
require './.localeapp/config' | |
require 'localeapp/exception_handler' | |
before do | |
handle_translation_updates | |
end | |
after do | |
send_missing_translations | |
end | |
get '/' do | |
I18n.t('greeting') | |
end | |
def handle_translation_updates | |
unless ::Localeapp.configuration.polling_disabled? | |
::Localeapp.log Time.now.to_i.to_s << '-- Handling translation updates' | |
if ::Localeapp.poller.needs_polling? | |
::Localeapp.log Time.now.to_i.to_s << ' - polling' | |
::Localeapp.poller.poll! | |
end | |
end | |
unless ::Localeapp.configuration.reloading_disabled? | |
if ::Localeapp.poller.needs_reloading? | |
::Localeapp.log Time.now.to_i.to_s << '- reloading I18n' | |
I18n.reload! | |
::Localeapp.poller.updated_at = ::Localeapp.poller.synchronization_data[:updated_at] | |
end | |
end | |
end | |
def send_missing_translations | |
return if ::Localeapp.configuration.sending_disabled? | |
::Localeapp.sender.post_missing_translations | |
end | |
def initialize_synchronization_data_file | |
if !File.exists?(Localeapp.configuration.synchronization_data_file) | |
File.open(Localeapp.configuration.synchronization_data_file, 'w') do |f| | |
f.write({:polled_at => Time.now.to_i, :updated_at => Time.now.to_i}.to_yaml) | |
end | |
end | |
end | |
initialize_synchronization_data_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment