From 3f491c8b36f3c63169308462dac6a57c43eaa30f Mon Sep 17 00:00:00 2001 From: Philip Arndt Date: Wed, 30 Jun 2010 13:23:19 +1200 Subject: [PATCH] Properly make the default locale settable by refinery setting and clean up method signatures. --- config/application.rb | 9 --------- vendor/plugins/i18n/lib/i18n.rb | 23 +++++++++++++++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/config/application.rb b/config/application.rb index c651681acd..1bf0a6cd00 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,15 +24,6 @@ # Note: These are settings that aren't dependent on environment type. For those, use the files in config/environments/ require Rails.root.join('config', 'settings.rb').to_s -# Mislav trick to fallback to default for missing translations -I18n.exception_handler = lambda do |e, locale, key, options| - if I18n::MissingTranslationData === e and locale != I18n.default_locale - I18n.translate(key, (options || {}).update(:locale => I18n.default_locale, :raise => true)) - else - raise e - end -end if Rails.env == 'production' - # Bundler has shown a weakness using Rails < 3 so we are going to # require these dependencies here until we can find another solution or until we move to # Rails 3.0 which should fix the issue (or until Bundler fixes the issue). diff --git a/vendor/plugins/i18n/lib/i18n.rb b/vendor/plugins/i18n/lib/i18n.rb index 3034a85410..06ca545e29 100644 --- a/vendor/plugins/i18n/lib/i18n.rb +++ b/vendor/plugins/i18n/lib/i18n.rb @@ -1,7 +1,7 @@ class Refinery::I18n class << self - attr_accessor :enabled, :current_locale, :locales + attr_accessor :enabled, :current_locale, :locales, :default_locale def enabled? # cache this lookup as it gets very expensive. @@ -19,8 +19,8 @@ def current_locale @current_locale = :en RefinerySetting[:refinery_i18n_current_locale] = :en else - @current_locale ||= RefinerySetting.find_or_set(:refinery_i18n_current_locale, - RefinerySetting.find_or_set(:refinery_i18n_default_locale, :en)) + @current_locale ||= RefinerySetting.find_or_set(:refinery_i18n_current_locale, I18n.default_locale) + end end @@ -45,7 +45,7 @@ def locales }) end - def has_locale? locale + def has_locale?(locale) locales.has_key? locale.try(:to_sym) end @@ -58,6 +58,15 @@ def setup! self.load_refinery_locales! self.load_app_locales! self.set_default_locale! + + # Mislav trick to fallback to default for missing translations + I18n.exception_handler = lambda do |e, locale, key, options| + if I18n::MissingTranslationData === e and locale != I18n.default_locale + I18n.translate(key, (options || {}).update(:locale => I18n.default_locale, :raise => true)) + else + raise e + end + end if Rails.env == 'production' end def load_base_locales! @@ -73,10 +82,12 @@ def load_app_locales! end def set_default_locale! - I18n.default_locale = current_locale + I18n.default_locale = @default_locale ||= RefinerySetting.find_or_set(:refinery_i18n_default_locale, :en, { + :callback_proc_as_string => %q{::Refinery::I18n.setup!} + }) end - def load_locales locale_files + def load_locales(locale_files) locale_files = locale_files.to_s if locale_files.is_a? Pathname locale_files = Dir[locale_files] if locale_files.is_a? String locale_files.each do |locale_file|