Class: Rack::I18nJs

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_js.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ I18nJs

Returns a new instance of I18nJs.



3
4
5
6
# File 'lib/i18n_js.rb', line 3

def initialize app, options = {}
  @app = app
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/i18n_js.rb', line 8

def call env
  # Valid url is /javascripts/i18n-<i18n key>-<locale>.<format> where
  # i18n key - yaml branch named "locale.key"
  # locale - locale as is
  # format - js or json

  if data_array = env['PATH_INFO'].scan( \
                  /^\/javascripts\/locale_(\w{1,3})[.](js[on]*)$/)[0]

    locale, type = data_array

    # Get yaml branch by key

    json = ::YAML::load(
      ::File.open("#{RAILS_ROOT}/config/locales/#{locale}.yml")
    )[locale].to_json

    content_type, response = type == 'js' ?
      ['application/javascript', "var I18n = #{json};"] :
      ['application/json', json]

    [200, {'Content-Type' => content_type}, [response]]
  else
    @app.call env
  end
end