FIX: I18n Fallbacks were not applying correctly

This commit is contained in:
Robin Ward 2015-12-23 12:09:18 -05:00
parent 47410f1b99
commit d1ebb9d0b5
5 changed files with 42 additions and 28 deletions

View File

@ -289,7 +289,7 @@ class ApplicationController < ActionController::Base
store_preloaded("customHTML", custom_html_json) store_preloaded("customHTML", custom_html_json)
store_preloaded("banner", banner_json) store_preloaded("banner", banner_json)
store_preloaded("customEmoji", custom_emoji) store_preloaded("customEmoji", custom_emoji)
store_preloaded("translationOverrides", I18n.client_overrides_json) store_preloaded("translationOverrides", I18n.client_overrides_json(I18n.locale))
end end
def preload_current_user_data def preload_current_user_data

View File

@ -52,14 +52,16 @@ module I18n
end end
def search(query, opts=nil) def search(query, opts=nil)
load_locale(config.locale) unless @loaded_locales.include?(config.locale) locale = opts[:locale] || config.locale
load_locale(locale) unless @loaded_locales.include?(locale)
opts ||= {} opts ||= {}
target = opts[:backend] || backend target = opts[:backend] || backend
results = opts[:overridden] ? {} : target.search(config.locale, query) results = opts[:overridden] ? {} : target.search(config.locale, query)
regexp = /#{query}/i regexp = /#{query}/i
(overrides_by_locale || {}).each do |k, v| (overrides_by_locale(locale) || {}).each do |k, v|
results.delete(k) results.delete(k)
results[k] = v if (k =~ regexp || v =~ regexp) results[k] = v if (k =~ regexp || v =~ regexp)
end end
@ -80,18 +82,23 @@ module I18n
@overrides_enabled = true @overrides_enabled = true
end end
def translate_no_override(key, *args) def translate_no_override(*args)
return translate_no_cache(key, *args) if args.length > 0 return translate_no_cache(*args) if args.length > 1 && args[1].present?
options = args.last.is_a?(Hash) ? args.pop.dup : {}
key = args.shift
locale = options[:locale] || config.locale
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE) @cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
k = "#{key}#{config.locale}#{config.backend.object_id}" k = "#{key}#{locale}#{config.backend.object_id}"
@cache.getset(k) do @cache.getset(k) do
translate_no_cache(key).freeze translate_no_cache(key, options).freeze
end end
end end
def overrides_by_locale def overrides_by_locale(locale)
return unless @overrides_enabled return unless @overrides_enabled
site = RailsMultisite::ConnectionManagement.current_db site = RailsMultisite::ConnectionManagement.current_db
@ -109,25 +116,30 @@ module I18n
end end
end end
by_site[config.locale] by_site[locale]
end end
def client_overrides_json def client_overrides_json(locale)
client_json = (overrides_by_locale || {}).select {|k, _| k.starts_with?('js.') || k.starts_with?('admin_js.')} client_json = (overrides_by_locale(locale) || {}).select {|k, _| k.starts_with?('js.') || k.starts_with?('admin_js.')}
MultiJson.dump(client_json) MultiJson.dump(client_json)
end end
def translate(key, *args) def translate(*args)
load_locale(config.locale) unless @loaded_locales.include?(config.locale) options = args.last.is_a?(Hash) ? args.pop.dup : {}
key = args.shift
locale = options[:locale] || config.locale
load_locale(locale) unless @loaded_locales.include?(locale)
if @overrides_enabled if @overrides_enabled
by_locale = overrides_by_locale by_locale = overrides_by_locale(locale)
if by_locale if by_locale
if args.size > 0 && args[0].is_a?(Hash) if options.present?
args[0][:overrides] = by_locale options[:overrides] = by_locale
# I18n likes to use throw... # I18n likes to use throw...
catch(:exception) do catch(:exception) do
return backend.translate(config.locale, key, args[0]) return backend.translate(locale, key, options)
end end
else else
if result = by_locale[key] if result = by_locale[key]
@ -137,14 +149,15 @@ module I18n
end end
end end
translate_no_override(key, *args) translate_no_override(key, options)
end end
alias_method :t, :translate alias_method :t, :translate
def exists?(*args) def exists?(key, locale=nil)
load_locale(config.locale) unless @loaded_locales.include?(config.locale) locale ||= config.locale
exists_no_cache?(*args) load_locale(locale) unless @loaded_locales.include?(locale)
exists_no_cache?(key, locale)
end end
end end

View File

@ -15,6 +15,7 @@ module I18n
def reload! def reload!
@overrides = {} @overrides = {}
@pluralizers = {}
super super
end end

View File

@ -81,9 +81,9 @@ module JsLocaleHelper
site_locale = SiteSetting.default_locale.to_sym site_locale = SiteSetting.default_locale.to_sym
if Rails.env.development? # if Rails.env.development?
translations = load_translations(locale_sym, force: true) # translations = load_translations(locale_sym, force: true)
else # else
if locale_sym == :en if locale_sym == :en
translations = load_translations(locale_sym) translations = load_translations(locale_sym)
elsif locale_sym == site_locale || site_locale == :en elsif locale_sym == site_locale || site_locale == :en
@ -91,7 +91,7 @@ module JsLocaleHelper
else else
translations = load_translations_merged(locale_sym, site_locale, :en) translations = load_translations_merged(locale_sym, site_locale, :en)
end end
end # end
message_formats = strip_out_message_formats!(translations[locale_str]['js']) message_formats = strip_out_message_formats!(translations[locale_str]['js'])

View File

@ -118,18 +118,18 @@ describe I18n::Backend::DiscourseI18n do
describe "client json" do describe "client json" do
it "is empty by default" do it "is empty by default" do
expect(I18n.client_overrides_json).to eq("{}") expect(I18n.client_overrides_json('en')).to eq("{}")
end end
it "doesn't return server overrides" do it "doesn't return server overrides" do
TranslationOverride.upsert!('en', 'foo', 'bar') TranslationOverride.upsert!('en', 'foo', 'bar')
expect(I18n.client_overrides_json).to eq("{}") expect(I18n.client_overrides_json('en')).to eq("{}")
end end
it "returns client overrides" do it "returns client overrides" do
TranslationOverride.upsert!('en', 'js.foo', 'bar') TranslationOverride.upsert!('en', 'js.foo', 'bar')
TranslationOverride.upsert!('en', 'admin_js.beep', 'boop') TranslationOverride.upsert!('en', 'admin_js.beep', 'boop')
json = ::JSON.parse(I18n.client_overrides_json) json = ::JSON.parse(I18n.client_overrides_json('en'))
expect(json).to be_present expect(json).to be_present
expect(json['js.foo']).to eq('bar') expect(json['js.foo']).to eq('bar')