Module: Roda::RodaPlugins::RodaI18n::RequestMethods
- Defined in:
- lib/roda/plugins/i18n.rb
Overview
methods used within Roda’s route block
Instance Method Summary collapse
-
#_match_available_locales_only ⇒ Object
Match only paths that contain one available locale from the ::R18n.available_locales list, otherwise skip it.
-
#i18n_set_locale(locale) ⇒ 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 = {}) ⇒ Object
(also: #i18n_locale)
Sets the locale based upon
:localeprefixed routes.
Instance Method Details
#_match_available_locales_only ⇒ Object
Match only paths that contain one available locale from the ::R18n.available_locales list, otherwise skip it.
This custom matcher allows us to have other routes below the r.locale .. declaration
266 267 268 269 270 271 272 273 274 |
# File 'lib/roda/plugins/i18n.rb', line 266 def _match_available_locales_only lambda do locale = remaining_path.split('/').reject(&:empty?).first.to_s if ::R18n.available_locales.map { |locale| locale.code.downcase }.include?(locale.downcase) @captures.push(locale) @remaining_path = remaining_path.sub("/#{locale}", '') end end end |
#i18n_set_locale(locale) ⇒ 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
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/roda/plugins/i18n.rb', line 248 def i18n_set_locale(locale) 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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/roda/plugins/i18n.rb', line 208 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 = {}) ⇒ 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
r.get(:about) { erb(:about) }
end
290 291 292 293 294 295 296 297 |
# File 'lib/roda/plugins/i18n.rb', line 290 def locale(opts = {}) on(_match_available_locales_only, opts) do |l| loc = l || Roda.opts[:locale] ::R18n.set(loc) yield loc if block_given? return # NB!! needed to enable routes below to work end end |