Created
December 17, 2010 10:54
-
-
Save qoobaa/744775 to your computer and use it in GitHub Desktop.
Simple Rack application for JavaScript I18n
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
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 |
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
map "/javascripts/i18n" do | |
run JavaScriptI18n | |
end | |
map "/" do | |
run MyShinyApp::Application | |
end |
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
javascript_include_tag "i18n/#{I18n.locale}" |
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
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