diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8bbbc55a2cd..b20a59e2e74 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/lib/freedom_patches/translate_accelerator.rb b/lib/freedom_patches/translate_accelerator.rb index 570b4c24230..8b131d32473 100644 --- a/lib/freedom_patches/translate_accelerator.rb +++ b/lib/freedom_patches/translate_accelerator.rb @@ -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 diff --git a/lib/i18n/backend/discourse_i18n.rb b/lib/i18n/backend/discourse_i18n.rb index f8f072a3415..c027f88b496 100644 --- a/lib/i18n/backend/discourse_i18n.rb +++ b/lib/i18n/backend/discourse_i18n.rb @@ -15,6 +15,7 @@ module I18n def reload! @overrides = {} + @pluralizers = {} super end diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb index af06c334092..42c303cf437 100644 --- a/lib/js_locale_helper.rb +++ b/lib/js_locale_helper.rb @@ -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']) diff --git a/spec/components/discourse_i18n_spec.rb b/spec/components/discourse_i18n_spec.rb index ffff68a6a28..c5a5611902d 100644 --- a/spec/components/discourse_i18n_spec.rb +++ b/spec/components/discourse_i18n_spec.rb @@ -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')