Skip to content

Instantly share code, notes, and snippets.

@jamesbebbington
Created April 7, 2010 16:30
Show Gist options
  • Save jamesbebbington/359096 to your computer and use it in GitHub Desktop.
Save jamesbebbington/359096 to your computer and use it in GitHub Desktop.
class I18nController < ApplicationController
caches_page :show
# Renders HTTP cacheable JSON encoded dictionary of translation strings for requested locale and currently selected locale
def show
@locale = params[:locale].to_sym
raise ActiveRecord::RecordNotFound unless I18n.selectable_locales.include?(@locale)
respond_to do |format|
format.js do
@strings = I18n.t('javascript', :locale => @locale)
end
end
end
end
ActionController::Routing::Routes.draw do |map|
map.i18n '/javascripts/i18n/i18n.:locale.:format', :controller => "i18n", :action => "show"
end
// Quick and dirty I18n object that echos the basic functionality of the I18n gem.
// e.g. I18n.t('key.to.translate', {interpolation_key: interpolation_value})
var I18n = {
locale: '<%= @locale %>',
strings: <%= raw(@strings.to_json) %>,
t: function(key, interpolations) {
string = eval("this.strings." + key);
for (key in interpolations) {
string = string.replace(new RegExp("\{\{" + key + "\}\}"), interpolations[key])
}
return string;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment