Created
April 26, 2010 21:15
-
-
Save qoobaa/379943 to your computer and use it in GitHub Desktop.
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" | |
require "rack/contrib/simple_endpoint" | |
MINIFIED_LIB = <<EOJS | |
var I18n=I18n||(function(){function a(g,f){return g.replace(/\{\{([^}]+)\}\}/g,function(){return f[arguments[1]]||arguments[0]})}function c(f){if(!f){return[]}if(typeof f!="string"){return f}return f.split(".")}function d(g,j){var f=0,h=I18n.translations;j=(typeof j==="string")?[j]:(j||[]);while(g[f]){h=h&&h[g[f]];f++}if(h){return h}else{if(j.length===0){return null}else{if(j[0].substr(0,1)===":"){return d(g.slice(0,g.length-1).concat(c(j[0].substr(1))),j.slice(1))}else{return j[0]}}}}function b(g,f){if(f===undefined){return g}return f===1?g.one:g.other}function e(j,g){if(typeof j!="string"){var f=[],h;for(h=0;h<j.length;h++){f.push(e(j[h],g))}return f}else{g=g||{};g.defaultValue=g.defaultValue||null;j=c(g.scope).concat(c(j));var k=d(j,g.defaultValue);if(typeof k!=="string"&&k){k=b(k,g.count)}if(typeof k==="string"){k=a(k,g)}return k}}return{translate:e,t:e}})(); | |
EOJS | |
class RackjsMiddleware < Rack::SimpleEndpoint | |
def initialize(app, options = {}) | |
@options = options | |
super(app, path_regexp) do |request, response, match| | |
response["Content-Type"] = "application/javascript" | |
compiled(match[1]) | |
end | |
end | |
private | |
def path | |
@options.fetch(:path, "/javascripts/i18n").sub(%r{/$}, "") | |
end | |
def path_regexp | |
%r{^#{path}/(#{available_locales.join("|")}).js$} | |
end | |
def translations | |
I18n.backend.send(:init_translations) if I18n.backend.send(:translations).empty? | |
I18n.backend.send(:translations) | |
end | |
def available_locales | |
translations.keys | |
end | |
def compiled(locale) | |
"I18n.locale=I18n.locale||\"#{locale}\";I18n.translations=I18n.translations||#{translations[locale].to_json};#{MINIFIED_LIB}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment