Skip to content

Commit ecfa17b

Browse files
committed
FEATURE: Localization fallbacks (server-side)
The FallbackLocaleList object tells I18n::Backend::Fallbacks what order the languages should be attempted in. Because of the translate_accelerator patch, the SiteSetting.default_locale is *not* guaranteed to be fully loaded after the server starts, so a call to ensure_loaded! is added after the locale is set for the current user. The declarations of config.i18n.fallbacks = true in the environment files were actually garbage, because the I18n.default_locale was SiteSetting.default_locale, so there was nothing to fall back to. *derp*
1 parent 728845d commit ecfa17b

File tree

7 files changed

+31
-15
lines changed

7 files changed

+31
-15
lines changed

app/controllers/application_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ def set_locale
155155
else
156156
SiteSetting.default_locale
157157
end
158+
159+
I18n.fallbacks.ensure_loaded!
158160
end
159161

160162
def store_preloaded(key, json)

config/cloud/cloud66/files/production.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
# Specifies the header that your server uses for sending files
2424
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
2525

26-
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
27-
# the I18n.default_locale when a translation can not be found)
28-
config.i18n.fallbacks = true
29-
30-
3126
# you may use other configuration here for mail eg: sendgrid
3227

3328
config.action_mailer.delivery_method = :smtp

config/environments/production.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
config.log_level = :info
2626

27-
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
28-
# the I18n.default_locale when a translation can not be found)
29-
config.i18n.fallbacks = true
30-
3127
if GlobalSetting.smtp_address
3228
settings = {
3329
address: GlobalSetting.smtp_address,

config/environments/profile.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
# Specifies the header that your server uses for sending files
2828
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
2929

30-
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
31-
# the I18n.default_locale when a translation can not be found)
32-
config.i18n.fallbacks = true
33-
3430
# we recommend you use mailcatcher https://github.com/sj26/mailcatcher
3531
config.action_mailer.smtp_settings = { address: "localhost", port: 1025 }
3632

config/initializers/i18n.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# order: after 02-freedom_patches.rb
2+
3+
# Include pluralization module
4+
require 'i18n/backend/pluralization'
5+
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
6+
7+
# Include fallbacks module
8+
require 'i18n/backend/fallbacks'
9+
I18n.backend.class.send(:include, I18n::Backend::Fallbacks)
10+
11+
# Configure custom fallback order
12+
class FallbackLocaleList < Hash
13+
def [](locale)
14+
# user locale, site locale, english
15+
# TODO - this can be extended to be per-language for a better user experience
16+
# (e.g. fallback zh_TW to zh_CN / vice versa)
17+
[locale, SiteSetting.default_locale.to_sym, :en].uniq.compact
18+
end
19+
20+
def ensure_loaded!
21+
self[I18n.locale].each { |l| I18n.ensure_loaded! l }
22+
end
23+
end
24+
I18n.fallbacks = FallbackLocaleList.new

config/initializers/pluralization.rb

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/freedom_patches/translate_accelerator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def load_locale(locale)
5959
end
6060
end
6161

62+
def ensure_loaded!(locale)
63+
@loaded_locales ||= []
64+
load_locale locale unless @loaded_locales.include?(locale)
65+
end
66+
6267
def translate(key, *args)
6368
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
6469
return translate_no_cache(key, *args) if args.length > 0

0 commit comments

Comments
 (0)