Module: Roda::RodaPlugins::RodaI18n::RequestMethods
- Defined in:
- lib/roda/plugins/i18n.rb
Overview
methods used within Roda’s route block
Instance Method Summary collapse
-
#i18n_set_locale(locale, &blk) ⇒ Object
Enables setting temporary :locale blocks within the routing block.
-
#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
:localeprefixed 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 blocks within the routing block.
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
255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/roda/plugins/i18n.rb', line 255 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 if block_given? # return # NB!! needed to enable routes below to work 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)
/url?locale=de
<%= t.one %> #=> Ein
/url?locale=es
<%= t.one %> #=> Uno
# B): set from session[:locale] (if present)
r.i18n_set_locale_from(:session)
session[:locale] = 'de'
<%= t.one %> #=> Ein
session[:locale] = 'es'
<%= t.one %> #=> Uno
# C): set from the browser's HTTP request locale
r.i18n_set_locale_from(:http)
HTTP_ACCEPT_LANGUAGE = 'sv-se;q=1,es;q=0.8,en;q=0.6'
<%= t.one %> #=> Ett
# D): set from the server ENV['LANG'] variable
r.i18n_set_locale_from(:ENV)
ENV['LANG'] = 'en_US.UTF8'
<%= t.one %> #=> One
ENV['LANG'] = 'es'
<%= t.one %> #=> Uno
r.is 'posts' do
t.posts.header # use translations
end
end
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/roda/plugins/i18n.rb', line 215 def i18n_set_locale_from(type) case type.to_sym when :http loc = ::R18n::I18n.parse_http(scope.request.env['HTTP_ACCEPT_LANGUAGE']) when :session loc = session[:locale] if session[:locale] when :params loc = scope.request.params['locale'] if scope.request.params['locale'] when :ENV # ENV['LANG']="en_US.UTF-8" loc = ENV['LANG'].split('.').first if ENV['LANG'] else loc = nil end # sanity check: set to default locale if not set above loc = ::R18n::I18n.default.to_s if loc.nil? i18n = ::R18n::I18n.new( loc, ::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
282 283 284 285 286 287 288 289 290 |
# File 'lib/roda/plugins/i18n.rb', line 282 def locale(opts = {}, &blk) on(':locale', opts) do |l| loc = l || self.class.opts[:locale] session[:locale] = loc unless session[:locale] ::R18n.set(loc) yield if block_given? return # NB!! needed to enable routes below to work end end |