A platform for community discussion. Free, open, simple.
PERF: Ensure locales are always handled as symbols internally (#12897)
David Taylor1bc1a439ee0e
diff --git a/lib/freedom_patches/translate_accelerator.rb b/lib/freedom_patches/translate_accelerator.rbindex 0ba2ac554c1..2bc4cac1e8d 100644--- a/lib/freedom_patches/translate_accelerator.rb+++ b/lib/freedom_patches/translate_accelerator.rb@@ -33,6 +33,7 @@ module I18nLOAD_MUTEX = Mutex.newdef load_locale(locale)+ locale = locale.to_symLOAD_MUTEX.synchronize doreturn if @loaded_locales.include?(locale)@@ -70,7 +71,7 @@ module I18ndef search(query, opts = {})execute_reload if @requires_reload- locale = opts[:locale] || config.locale+ locale = (opts[:locale] || config.locale).to_symload_locale(locale) unless @loaded_locales.include?(locale)opts ||= {}@@ -87,6 +88,7 @@ module I18nenddef ensure_loaded!(locale)+ locale = locale.to_sym@loaded_locales ||= []load_locale(locale) unless @loaded_locales.include?(locale)end@@ -119,6 +121,7 @@ module I18nendlocale ||= config.locale+ locale = locale.to_sym@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)k = "#{key}#{locale}#{config.backend.object_id}"@@ -143,6 +146,7 @@ module I18ndef overrides_by_locale(locale)return unless @overrides_enabledreturn {} if GlobalSetting.skip_db?+ locale = locale.to_symexecute_reload if @requires_reload@@ -181,7 +185,7 @@ module I18noptions = args.last.is_a?(Hash) ? args.pop.dup : {}key = args.shift- locale = options[:locale] || config.locale+ locale = (options[:locale] || config.locale).to_symload_locale(locale) unless @loaded_locales.include?(locale)@@ -224,11 +228,13 @@ module I18nexecute_reload if @requires_reloadlocale ||= config.locale+ locale = locale.to_symload_locale(locale) unless @loaded_locales.include?(locale)exists_no_cache?(key, locale)enddef locale=(value)+ value = value.to_symexecute_reload if @requires_reloadself.locale_no_cache = valueenddiff --git a/spec/components/freedom_patches/translate_accelerator_spec.rb b/spec/components/freedom_patches/translate_accelerator_spec.rbindex ba4d377261b..effea352b3f 100644--- a/spec/components/freedom_patches/translate_accelerator_spec.rb+++ b/spec/components/freedom_patches/translate_accelerator_spec.rb@@ -46,6 +46,13 @@ describe "translate accelerator" doendend+ it "converts language keys to symbols" do+ expect(I18n.t('foo', locale: :en)).to eq('Foo in :en')+ expect(I18n.t('foo', locale: "en")).to eq('Foo in :en')++ expect(I18n.instance_variable_get(:@loaded_locales)).to contain_exactly(:en)+ end+it "overrides for both string and symbol keys" dokey = 'user.email.not_allowed'text_overriden = 'foobar'@@ -68,8 +75,8 @@ describe "translate accelerator" doexpect(I18n.instance_variable_get(:@overrides_by_site)).to eq('default' => {- 'en' => { 'got' => 'summer' },- 'zh_TW' => { 'got' => '冬季' }+ en: { 'got' => 'summer' },+ zh_TW: { 'got' => '冬季' }})end