Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Created December 17, 2010 10:54
Show Gist options
  • Save qoobaa/744775 to your computer and use it in GitHub Desktop.
Save qoobaa/744775 to your computer and use it in GitHub Desktop.
Simple Rack application for JavaScript I18n
require "json"
class JavaScriptI18n
def self.call(env)
locale = env["PATH_INFO"][1..-4]
if translations.key?(locale)
[200, {"Content-Type" => "text/javascript"}, new(translations[locale])]
else
[404, {}, "Not found"]
end
end
def self.translations
I18n.backend.send(:init_translations) if I18n.backend.send(:translations).empty?
I18n.backend.send(:translations).with_indifferent_access
end
def initialize(translations)
@translations = translations
end
def each
yield "var I18n=I18n||{};I18n.translations="
yield @translations.to_json
yield ";"
end
end
map "/javascripts/i18n" do
run JavaScriptI18n
end
map "/" do
run MyShinyApp::Application
end
javascript_include_tag "i18n/#{I18n.locale}"
require "rack/contrib/response_cache"
map "/javascripts/i18n" do
use(Rack::ResponseCache, "public/javascripts/i18n") { |env| env["PATH_INFO"] }
run JavaScriptI18n
end
map "/" do
run MyShinyApp::Application
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment