Module: Sinatra::R18n

Defined in:
lib/sinatra/r18n.rb,
lib/sinatra/r18n/version.rb

Overview

R18n module for Sinatra with appropriate hooks

Constant Summary collapse

VERSION =
'5.0.2'

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sinatra/r18n.rb', line 27

def self.registered(app)
  app.helpers ::R18n::Helpers
  app.set :default_locale, (proc { ::R18n::I18n.default })
  app.set :translations,   (proc { ::R18n.default_places })

  ::R18n.default_places { File.join(app.root, 'i18n') }

  app.before do
    ::R18n.clear_cache! if self.class.development?

    ::R18n::I18n.default = settings.default_locale if settings.default_locale

    locales = ::R18n::I18n.parse_http(request.env['HTTP_ACCEPT_LANGUAGE'])
    if params[:locale]
      locales.unshift(params[:locale])
    elsif session[:locale]
      locales.unshift(session[:locale])
    end

    i18n = ::R18n::I18n.new(
      locales, ::R18n.default_places,
      off_filters: :untranslated, on_filters: :untranslated_html
    )
    ::R18n.thread_set i18n
  end
end