From 02ea5058a3065f439ee41482456d8b4387f6cd7f Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 19 Aug 2021 08:51:05 +0100 Subject: [PATCH] Fix locale loading when application boots In development mode Rails might output some deprecation warnings which don't appear in test or production. It seems Rails 6 prepares these warnings ahead of time and they rely on the `#to_sentence` this in turn loads the locales and but because our gettext based strings are loaded in an initializer the available locales aren't yet set. Resulting in a error being raised with our i18n fixes. This change moves all the gettext and locale setup into a Railtie and sets everything up in a `before_configuration` block which runs before any initializers are run. Fixes: ``` ./lib/i18n_fixes.rb:54:in `available_locales': undefined method `map' for nil:NilClass (NoMethodError) ``` --- config/application.rb | 8 +------ config/initializers/alaveteli.rb | 3 --- config/initializers/fast_gettext.rb | 16 -------------- lib/alaveteli_localization.rb | 6 ++--- lib/alaveteli_localization/railtie.rb | 32 +++++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 30 deletions(-) delete mode 100644 config/initializers/fast_gettext.rb create mode 100644 lib/alaveteli_localization/railtie.rb diff --git a/config/application.rb b/config/application.rb index 8bc8b672c8..a38c9119d7 100644 --- a/config/application.rb +++ b/config/application.rb @@ -16,6 +16,7 @@ # require 'rails/test_unit/railtie' require File.dirname(__FILE__) + '/../lib/configuration' +require File.dirname(__FILE__) + '/../lib/alaveteli_localization' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. @@ -104,12 +105,5 @@ class Application < Rails::Application if AlaveteliConfiguration::force_ssl config.action_mailer.default_url_options[:protocol] = "https" end - - config.after_initialize do - AlaveteliLocalization.set_locales( - AlaveteliConfiguration.available_locales, - AlaveteliConfiguration.default_locale - ) - end end end diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index 4d596fd38a..8f8882e324 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -38,7 +38,6 @@ require 'core_ext/warning' require 'use_spans_for_errors.rb' -require 'i18n_fixes.rb' require 'world_foi_websites.rb' require 'alaveteli_external_command.rb' require 'quiet_opener.rb' @@ -46,12 +45,10 @@ require 'ability' require 'normalize_string' require 'alaveteli_file_types' -require 'alaveteli_localization' require 'theme' require 'xapian_queries' require 'date_quarter' require 'public_body_csv' -require 'routing_filters' require 'alaveteli_text_masker' require 'database_collation' require 'alaveteli_geoip' diff --git a/config/initializers/fast_gettext.rb b/config/initializers/fast_gettext.rb deleted file mode 100644 index 75436a4650..0000000000 --- a/config/initializers/fast_gettext.rb +++ /dev/null @@ -1,16 +0,0 @@ - - -repos = [ FastGettext::TranslationRepository.build('app', - :path => 'locale', - :type => :po) ] -if AlaveteliConfiguration::enable_alaveteli_pro - pro_repo = FastGettext::TranslationRepository.build('app', - :path => 'locale_alaveteli_pro', - :type => :po) - repos << pro_repo -end -AlaveteliLocalization.set_default_text_domain('app', repos) - -I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) - -AlaveteliLocalization.set_default_locale_urls(AlaveteliConfiguration::include_default_locale_in_urls) diff --git a/lib/alaveteli_localization.rb b/lib/alaveteli_localization.rb index 68dab4b89e..80e450e99e 100644 --- a/lib/alaveteli_localization.rb +++ b/lib/alaveteli_localization.rb @@ -1,8 +1,6 @@ -require 'alaveteli_localization/locale' -require 'alaveteli_localization/hyphenated_locale' -require 'alaveteli_localization/underscorred_locale' - class AlaveteliLocalization + require_relative 'alaveteli_localization/railtie' + class << self def set_locales(available_locales, default_locale) available, default = parse_locales(available_locales, default_locale) diff --git a/lib/alaveteli_localization/railtie.rb b/lib/alaveteli_localization/railtie.rb new file mode 100644 index 0000000000..280a19c829 --- /dev/null +++ b/lib/alaveteli_localization/railtie.rb @@ -0,0 +1,32 @@ +class AlaveteliLocalization + class Railtie < Rails::Railtie + config.before_configuration do + require 'alaveteli_localization/locale' + require 'alaveteli_localization/hyphenated_locale' + require 'alaveteli_localization/underscorred_locale' + + require 'routing_filters' + require 'i18n_fixes' + + paths = ['locale'] + paths << 'locale_alaveteli_pro' if AlaveteliConfiguration. + enable_alaveteli_pro + + repos = paths.map do |path| + FastGettext::TranslationRepository.build('app', path: path, type: :po) + end + AlaveteliLocalization.set_default_text_domain('app', repos) + + I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) + + AlaveteliLocalization.set_default_locale_urls( + AlaveteliConfiguration.include_default_locale_in_urls + ) + + AlaveteliLocalization.set_locales( + AlaveteliConfiguration.available_locales, + AlaveteliConfiguration.default_locale + ) + end + end +end