Skip to content

Commit

Permalink
Fix locale loading when application boots
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
gbp committed Sep 7, 2021
1 parent 1710bd4 commit 0ca6063
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
8 changes: 1 addition & 7 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
3 changes: 0 additions & 3 deletions config/initializers/alaveteli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,17 @@
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'
require 'mail_handler'
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'
Expand Down
16 changes: 0 additions & 16 deletions config/initializers/fast_gettext.rb

This file was deleted.

35 changes: 31 additions & 4 deletions lib/alaveteli_localization.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
require 'alaveteli_localization/locale'
require 'alaveteli_localization/hyphenated_locale'
require 'alaveteli_localization/underscorred_locale'

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

class << self
def set_locales(available_locales, default_locale)
available, default = parse_locales(available_locales, default_locale)
Expand Down

0 comments on commit 0ca6063

Please sign in to comment.