mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 15:48:43 -05:00
FIX: I18n Fallbacks were not applying correctly
This commit is contained in:
parent
47410f1b99
commit
d1ebb9d0b5
5 changed files with 42 additions and 28 deletions
|
@ -289,7 +289,7 @@ class ApplicationController < ActionController::Base
|
|||
store_preloaded("customHTML", custom_html_json)
|
||||
store_preloaded("banner", banner_json)
|
||||
store_preloaded("customEmoji", custom_emoji)
|
||||
store_preloaded("translationOverrides", I18n.client_overrides_json)
|
||||
store_preloaded("translationOverrides", I18n.client_overrides_json(I18n.locale))
|
||||
end
|
||||
|
||||
def preload_current_user_data
|
||||
|
|
|
@ -52,14 +52,16 @@ module I18n
|
|||
end
|
||||
|
||||
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 ||= {}
|
||||
|
||||
target = opts[:backend] || backend
|
||||
results = opts[:overridden] ? {} : target.search(config.locale, query)
|
||||
|
||||
regexp = /#{query}/i
|
||||
(overrides_by_locale || {}).each do |k, v|
|
||||
(overrides_by_locale(locale) || {}).each do |k, v|
|
||||
results.delete(k)
|
||||
results[k] = v if (k =~ regexp || v =~ regexp)
|
||||
end
|
||||
|
@ -80,18 +82,23 @@ module I18n
|
|||
@overrides_enabled = true
|
||||
end
|
||||
|
||||
def translate_no_override(key, *args)
|
||||
return translate_no_cache(key, *args) if args.length > 0
|
||||
def translate_no_override(*args)
|
||||
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)
|
||||
k = "#{key}#{config.locale}#{config.backend.object_id}"
|
||||
k = "#{key}#{locale}#{config.backend.object_id}"
|
||||
|
||||
@cache.getset(k) do
|
||||
translate_no_cache(key).freeze
|
||||
translate_no_cache(key, options).freeze
|
||||
end
|
||||
end
|
||||
|
||||
def overrides_by_locale
|
||||
def overrides_by_locale(locale)
|
||||
return unless @overrides_enabled
|
||||
|
||||
site = RailsMultisite::ConnectionManagement.current_db
|
||||
|
@ -109,25 +116,30 @@ module I18n
|
|||
end
|
||||
end
|
||||
|
||||
by_site[config.locale]
|
||||
by_site[locale]
|
||||
end
|
||||
|
||||
def client_overrides_json
|
||||
client_json = (overrides_by_locale || {}).select {|k, _| k.starts_with?('js.') || k.starts_with?('admin_js.')}
|
||||
def client_overrides_json(locale)
|
||||
client_json = (overrides_by_locale(locale) || {}).select {|k, _| k.starts_with?('js.') || k.starts_with?('admin_js.')}
|
||||
MultiJson.dump(client_json)
|
||||
end
|
||||
|
||||
def translate(key, *args)
|
||||
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
|
||||
def translate(*args)
|
||||
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
|
||||
by_locale = overrides_by_locale
|
||||
by_locale = overrides_by_locale(locale)
|
||||
if by_locale
|
||||
if args.size > 0 && args[0].is_a?(Hash)
|
||||
args[0][:overrides] = by_locale
|
||||
if options.present?
|
||||
options[:overrides] = by_locale
|
||||
|
||||
# I18n likes to use throw...
|
||||
catch(:exception) do
|
||||
return backend.translate(config.locale, key, args[0])
|
||||
return backend.translate(locale, key, options)
|
||||
end
|
||||
else
|
||||
if result = by_locale[key]
|
||||
|
@ -137,14 +149,15 @@ module I18n
|
|||
|
||||
end
|
||||
end
|
||||
translate_no_override(key, *args)
|
||||
translate_no_override(key, options)
|
||||
end
|
||||
|
||||
alias_method :t, :translate
|
||||
|
||||
def exists?(*args)
|
||||
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
|
||||
exists_no_cache?(*args)
|
||||
def exists?(key, locale=nil)
|
||||
locale ||= config.locale
|
||||
load_locale(locale) unless @loaded_locales.include?(locale)
|
||||
exists_no_cache?(key, locale)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -15,6 +15,7 @@ module I18n
|
|||
|
||||
def reload!
|
||||
@overrides = {}
|
||||
@pluralizers = {}
|
||||
super
|
||||
end
|
||||
|
||||
|
|
|
@ -81,9 +81,9 @@ module JsLocaleHelper
|
|||
|
||||
site_locale = SiteSetting.default_locale.to_sym
|
||||
|
||||
if Rails.env.development?
|
||||
translations = load_translations(locale_sym, force: true)
|
||||
else
|
||||
# if Rails.env.development?
|
||||
# translations = load_translations(locale_sym, force: true)
|
||||
# else
|
||||
if locale_sym == :en
|
||||
translations = load_translations(locale_sym)
|
||||
elsif locale_sym == site_locale || site_locale == :en
|
||||
|
@ -91,7 +91,7 @@ module JsLocaleHelper
|
|||
else
|
||||
translations = load_translations_merged(locale_sym, site_locale, :en)
|
||||
end
|
||||
end
|
||||
# end
|
||||
|
||||
message_formats = strip_out_message_formats!(translations[locale_str]['js'])
|
||||
|
||||
|
|
|
@ -118,18 +118,18 @@ describe I18n::Backend::DiscourseI18n do
|
|||
|
||||
describe "client json" do
|
||||
it "is empty by default" do
|
||||
expect(I18n.client_overrides_json).to eq("{}")
|
||||
expect(I18n.client_overrides_json('en')).to eq("{}")
|
||||
end
|
||||
|
||||
it "doesn't return server overrides" do
|
||||
TranslationOverride.upsert!('en', 'foo', 'bar')
|
||||
expect(I18n.client_overrides_json).to eq("{}")
|
||||
expect(I18n.client_overrides_json('en')).to eq("{}")
|
||||
end
|
||||
|
||||
it "returns client overrides" do
|
||||
TranslationOverride.upsert!('en', 'js.foo', 'bar')
|
||||
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['js.foo']).to eq('bar')
|
||||
|
|
Loading…
Reference in a new issue