Module: Roda::RodaPlugins::RodaI18n::RequestMethods
- Defined in:
- lib/roda/plugins/i18n.rb
Instance Method Summary collapse
-
#i18n_set_locale(locale, &blk) ⇒ Object
Enables setting temporary :locale.
-
#i18n_set_locale_from(type) ⇒ Object
Obtains the locale from either ENV, HTTP (browser), Params or Session values.
-
#locale(opts = {}, &blk) ⇒ Object
(also: #i18n_locale)
Sets the locale based upon
:locale
prefixed routes route do |r| r.locale do # all routes are prefixed with ‘/:locale’ # ie: GET /de/posts => will use DE translations # ie: GET /es/posts => will use ES translations r.is ‘posts’ do t.posts.header # use translations or locales end end end.
Instance Method Details
#i18n_set_locale(locale, &blk) ⇒ Object
Enables setting temporary :locale.
route do |r|
r.i18n_set_locale('de') do
# within this block the locale is DE (German)
end
r.i18n_set_locale('es') do
# within this block the locale is ES (Spanish)
end
end
210 211 212 213 214 215 216 217 218 |
# File 'lib/roda/plugins/i18n.rb', line 210 def i18n_set_locale(locale, &blk) locale = ::R18n::I18n.default.to_s if locale.nil? _i18n = ::R18n::I18n.new(locale, ::R18n.default_places, off_filters: :untranslated, on_filters: :untranslated_html) ::R18n.set(_i18n) yield end |
#i18n_set_locale_from(type) ⇒ Object
Obtains the locale from either ENV, HTTP (browser), Params or Session values.
route do |r|
# A): set from URL params ie: GET /posts?locale=de
r.i18n_set_locale_from(:params)
# B): set from session[:locale] (if present)
r.i18n_set_locale_from(:session)
# C): set from the browser's HTTP request locale
r.i18n_set_locale_from(:http)
# D): set from the server ENV['LANG'] variable
r.i18n_set_locale_from(:ENV)
r.is 'posts' do
t.posts.header # use translations
end
end
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/roda/plugins/i18n.rb', line 174 def i18n_set_locale_from(type) case type.to_sym when :http _locale = ::R18n::I18n.parse_http(scope.request.env['HTTP_ACCEPT_LANGUAGE']) when :session _locale = session[:locale] if session[:locale] when :params _locale = scope.request.params['locale'] if scope.request.params['locale'] when :ENV _locale = ENV['LANG'].split('.').first if ENV['LANG'] else _locale = nil end # sanity check: set to default locale if not set above _locale = ::R18n::I18n.default.to_s if _locale.nil? _i18n = ::R18n::I18n.new(_locale, ::R18n.default_places, off_filters: :untranslated, on_filters: :untranslated_html) ::R18n.set(_i18n) end |
#locale(opts = {}, &blk) ⇒ Object Also known as: i18n_locale
Sets the locale based upon :locale
prefixed routes
route do |r|
r.locale do
# all routes are prefixed with '/:locale'
# ie: GET /de/posts => will use DE translations
# ie: GET /es/posts => will use ES translations
r.is 'posts' do
t.posts.header # use translations or locales
end
end
end
233 234 235 236 237 238 239 240 |
# File 'lib/roda/plugins/i18n.rb', line 233 def locale(opts={}, &blk) on(':locale', opts) do |l| _locale = l || self.class.opts[:locale] session[:locale] = _locale unless session[:locale] ::R18n.set(_locale) yield end end |