Skip to content

Instantly share code, notes, and snippets.

@errorstudio
Created August 24, 2012 12:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save errorstudio/3450135 to your computer and use it in GitHub Desktop.
Save errorstudio/3450135 to your computer and use it in GitHub Desktop.
Refinery CMS - how to override refinery-i18n's find_or_set_locale method
# encoding: utf-8
Refinery::I18n.configure do |config|
config.enabled = true
config.default_locale = :en
config.current_locale = :en
config.default_frontend_locale = :en
locales = [ :"en-GB",
:"en-US",
:"en-NZ",
:"en-AU",
:"en-IE",
:"fr-FR",
:"fr-BE",
:"fr-LU",
:"fr-CH",
:"nl-NL",
:"nl-BE",
:"da-DK",
:"no-NO",
:"fi-FI",
:"es-ES",
:"it-IT",
:"it-CH",
:"de-DE",
:"de-LU",
:"de-CH",
:"de-AT",
:"sv-SE",
:"pl-PL",
:"pt-PT"
]
config.frontend_locales = locales.collect {|l| l.to_s.split("-").first.to_sym}.uniq!
config.frontend_locales_with_territory = locales
config.locales = { :en=>"English",
:fr=>"Français",
:nl=>"Nederlands",
:pt=>"Português",
:da=>"Dansk",
:no=>"Norsk Bokmål",
# :sl=>"Slovenian",
:fi=>"Suomi",
:es=>"Español",
:it=>"Italiano",
:de=>"Deutsch",
# :lv=>"Latviski",
# :ru=>"Русский",
:sv=>"Svenska",
:pl=>"Polski",
# :"zh-CN"=>"Simplified Chinese",
# :"zh-TW"=>"Traditional Chinese",
# :el=>"Ελληνικά",
# :rs=>"Srpski",
# :cs=>"Česky",
# :sk=>"Slovenský",
# :ja=>"日本語",
# :bg=>"Български"
}
config.locale_currencies = {
:"en-GB" => "£",
:"en-US" => "US $",
:"en-NZ" => "NZ $",
:"en-AU" => "AU $",
:"en-IE" => "IE €",
:"fr-FR" => "FR €",
:"fr-BE" => "BE €",
:"fr-LU" => "LU €",
:"fr-CH" => "CHF",
:"nl-NL" => "NL €",
:"nl-BE" => "BE €",
:"da-DK" => "DK kr",
:"no-NO" => "NO kr",
:"fi-FI" => "FI kr",
:"es-ES" => "ES €",
:"it-IT" => "IT €",
:"it-CH" => "CHF",
:"de-DE" => "DE €",
:"de-LU" => "LU €",
:"de-CH" => "CHF",
:"de-AT" => "AT €",
:"sv-SE" => "kr",
:"pl-PL" => "zł",
:"pt-PT" => "PT €"
}
end
Rails.application.config.to_prepare do
::RoutingFilter::RefineryLocales.class_eval do
def around_recognize(path, env, &block)
explicitly_set = false
if path =~ %r{^/(#{::Refinery::I18n.locales.keys.join('|')})(/|$)}
path.sub! %r(^/(([a-zA-Z\-_])*)(?=/|$)) do
::I18n.locale = $1
''
end
path.sub!(%r{^$}) { '/' }
explicitly_set = true
else
::I18n.locale = ::Refinery::I18n.default_frontend_locale
end
yield.tap do |params|
params[:locale] = ::I18n.locale if explicitly_set == true
end
end
end
::ApplicationController.module_eval do
def find_or_set_locale
if ::Refinery::I18n.enabled?
#order of precendence for setting the locale is:
# - explicitly-requested locale (i.e. in params[:locale]) which is then stored in a cookie if params[:remember] == "true"
# - locale from cookie
# - first appropriate locale from browser accept-language header
# - default locale (:en)
if ::Refinery::I18n.has_locale?(locale = params[:locale].try(:to_sym))
::I18n.locale = locale
cookies[:locale] = {
:value => locale,
:expires => 1.year.from_now.utc
}
elsif ::Refinery::I18n.has_locale?(locale = cookies[:locale].try(:to_sym))
::I18n.locale = locale
elsif !(locale = http_accept_language.compatible_language_from(::Refinery::I18n.frontend_locales)).nil?
::I18n.locale = locale
else
::I18n.locale = ::Refinery::I18n.frontend_locales.first
end
Thread.current[:globalize_locale] = ::I18n.locale
#stick the territory in a permanent cookie if it's sensible (not en-DE or something)
unless params[:t].nil?
territory = params[:t].upcase
if ::Refinery::I18n.config.frontend_locales_with_territory.include?(:"#{locale.to_s}-#{territory}")
cookies[:territory] = {
:value => territory,
:expires => 1.year.from_now.utc
}
end
end
end
end
prepend_before_filter :find_or_set_locale
protected :default_url_options, :find_or_set_locale
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment