module Roda::RodaPlugins::RodaI18n::RequestMethods
methods used within Roda's route block
Public Instance Methods
_match_available_locales_only()
click to toggle source
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
# File lib/roda/plugins/i18n.rb 273 def _match_available_locales_only 274 lambda do 275 locale = remaining_path.split("/").reject(&:empty?).first.to_s 276 if ::R18n.available_locales.map(&:code).map(&:downcase).include?(locale.downcase) 277 @captures.push(locale) 278 @remaining_path = remaining_path.sub("/#{locale}", "") 279 end 280 end 281 end
i18n_set_locale(locale) { || ... }
click to toggle source
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
# File lib/roda/plugins/i18n.rb 255 def i18n_set_locale(locale, &blk) 256 locale = ::R18n::I18n.default.to_s if locale.nil? 257 258 i18n = ::R18n::I18n.new( 259 locale, 260 ::R18n.default_places, 261 off_filters: :untranslated, 262 on_filters: :untranslated_html 263 ) 264 ::R18n.set(i18n) 265 yield if block_given? 266 # return # NB!! needed to enable routes below to work 267 end
i18n_set_locale_from(type)
click to toggle source
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
# File lib/roda/plugins/i18n.rb 215 def i18n_set_locale_from(type) 216 case type.to_sym 217 when :http 218 loc = ::R18n::I18n.parse_http(scope.request.env['HTTP_ACCEPT_LANGUAGE']) 219 when :session 220 loc = session[:locale] if session[:locale] 221 when :params 222 loc = scope.request.params['locale'] if scope.request.params['locale'] 223 when :ENV 224 # ENV['LANG']="en_US.UTF-8" 225 loc = ENV['LANG'].split('.').first if ENV['LANG'] 226 else 227 loc = nil 228 end 229 # sanity check: set to default locale if not set above 230 loc = ::R18n::I18n.default.to_s if loc.nil? 231 232 i18n = ::R18n::I18n.new( 233 loc, 234 ::R18n.default_places, 235 off_filters: :untranslated, 236 on_filters: :untranslated_html 237 ) 238 ::R18n.set(i18n) 239 end
locale(opts = {}) { |loc| ... }
click to toggle source
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
# File lib/roda/plugins/i18n.rb 297 def locale(opts = {}, &blk) 298 on(_match_available_locales_only, opts) do |l| 299 loc = l || Roda.opts[:locale] 300 ::R18n.set(loc) 301 yield loc if block_given? 302 return # NB!! needed to enable routes below to work 303 end 304 end
Also aliased as: i18n_locale