Class: Railj::Rack::I18n

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of I18n.



4
5
6
7
# File 'lib/i18n/i18n.rb', line 4

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

Instance Method Details

#call(env) ⇒ Object



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
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/i18n/i18n.rb', line 9

def call env
  # Valid url is /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( \
                  /^\/i18n-(\w+)-(\w{1,3})[.](js[on]*)$/)[0]

    key, locale, type = data_array

    # Return if :accepted_keys not :all and
    # not included in :accepted_keys array

    return @app.call env if \
      @options[:accepted_keys] and
      @options[:accepted_keys] != :all and
      not @options[:accepted_keys].include? key.to_sym # Key don't accepted

    # Get yaml branch by key

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

    return @app.call env if json == 'null' # Branch not found

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

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