Module: Conjoin::I18N

Includes:
R18n::Helpers
Defined in:
lib/conjoin/i18n.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup(app) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/conjoin/i18n.rb', line 8

def self.setup(app)
  app.settings[:default_locale] = 'en-US'
  app.settings[:translations] = File.join(app.root, 'i18n')
  ::R18n::Filters.off :untranslated
  ::R18n::Filters.on :untranslated_html
  if Conjoin.env.test? or Conjoin.env.development?
    ::R18n.clear_cache!
  end
end

Instance Method Details

#get_locale_from_hostObject



40
41
42
43
44
# File 'lib/conjoin/i18n.rb', line 40

def get_locale_from_host
  # auxiliar method to get locale from the subdomain (assuming it is a valid locale).
  data = req.host.split('.')[0]
  data if ::R18n::Locale.exists? data
end

#set_locale(req, force_default = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/conjoin/i18n.rb', line 18

def set_locale(req, force_default = false)
  ::R18n.set do
    ::R18n::I18n.default = settings[:default_locale]
    locale = get_locale_from_host
    # You can add support for path language info :) Just do it and pull request it ;)
    # locale = get_locale_from_path if locale.nil?
    if locale.nil? and not force_default
      locales = ::R18n::I18n.parse_http req.env['HTTP_ACCEPT_LANGUAGE']
      if req.params['locale']
        locales.insert 0, req.params['locale']
      elsif req.session['locale']
        locales.insert 0, req.session['locale']
      end
    else
      locales = []
      locales << locale
      locales << settings[:default_locale]
    end
    ::R18n::I18n.new locales, settings[:translations]
  end
end