FIX: moment_js locale files provided by plugins were ignored

FIX: moment_js locale files provided by plugins were ignored

diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb
index 22fe03b..aaefc70 100644
--- a/lib/js_locale_helper.rb
+++ b/lib/js_locale_helper.rb
@@ -164,10 +164,10 @@ module JsLocaleHelper
   def self.find_moment_locale(locale_chain)
     path = "#{Rails.root}/vendor/assets/javascripts/moment-locale"
 
-    # moment.js uses a different naming scheme for locale files
-    locale_chain = locale_chain.map { |l| l.tr('_', '-').downcase }
-
-    find_locale(locale_chain, path, :moment_js, fallback_to_english: false)
+    find_locale(locale_chain, path, :moment_js, fallback_to_english: false) do
+      # moment.js uses a different naming scheme for locale files
+      locale_chain.map { |l| l.tr('_', '-').downcase }
+    end
   end
 
   def self.find_message_format_locale(locale_chain, fallback_to_english:)
@@ -184,6 +184,8 @@ module JsLocaleHelper
       return [locale, filename] if File.exist?(filename)
     end
 
+    locale_chain = yield if block_given?
+
     # try again, but this time only with the language itself
     locale_chain = locale_chain.map { |l| l.split(/[-_]/)[0] }
       .uniq.reject { |l| locale_chain.include?(l) }
diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb
index 936f99b..ad31cfb 100644
--- a/spec/components/plugin/instance_spec.rb
+++ b/spec/components/plugin/instance_spec.rb
@@ -353,7 +353,7 @@ describe Plugin::Instance do
     end
 
     it "enables the registered locales only on activate" do
-      plugin.register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
+      plugin.register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
       plugin.register_locale("es_MX", name: "Spanish (Mexico)", nativeName: "Español (México)", fallbackLocale: "es")
       expect(DiscoursePluginRegistry.locales.count).to eq(0)
 
@@ -362,24 +362,27 @@ describe Plugin::Instance do
     end
 
     it "allows finding the locale by string and symbol" do
-      register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
+      register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
 
-      expect(DiscoursePluginRegistry.locales).to have_key(:foo)
-      expect(DiscoursePluginRegistry.locales).to have_key('foo')
+      expect(DiscoursePluginRegistry.locales).to have_key(:foo_BAR)
+      expect(DiscoursePluginRegistry.locales).to have_key('foo_BAR')
     end
 
     it "correctly registers a new locale" do
-      locale = register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
+      locale = register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
 
       expect(DiscoursePluginRegistry.locales.count).to eq(1)
-      expect(DiscoursePluginRegistry.locales).to have_key(:foo)
+      expect(DiscoursePluginRegistry.locales).to have_key(:foo_BAR)
 
       expect(locale[:fallbackLocale]).to be_nil
-      expect(locale[:message_format]).to eq(["foo", "#{plugin_path}/lib/javascripts/locale/message_format/foo.js"])
-      expect(locale[:moment_js]).to eq(["foo", "#{plugin_path}/lib/javascripts/locale/moment_js/foo.js"])
+      expect(locale[:message_format]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/message_format/foo_BAR.js"])
+      expect(locale[:moment_js]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/moment_js/foo_BAR.js"])
       expect(locale[:plural]).to eq(plural.with_indifferent_access)
 
-      expect(Rails.configuration.assets.precompile).to include("locales/foo.js")
+      expect(Rails.configuration.assets.precompile).to include("locales/foo_BAR.js")
+
+      expect(JsLocaleHelper.find_message_format_locale(["foo_BAR"], fallback_to_english: true)).to eq(locale[:message_format])
+      expect(JsLocaleHelper.find_moment_locale(["foo_BAR"])).to eq (locale[:moment_js])
     end
 
     it "correctly registers a new locale using a fallback locale" do
@@ -394,6 +397,9 @@ describe Plugin::Instance do
       expect(locale[:plural]).to be_nil
 
       expect(Rails.configuration.assets.precompile).to include("locales/es_MX.js")
+
+      expect(JsLocaleHelper.find_message_format_locale(["es_MX"], fallback_to_english: true)).to eq(locale[:message_format])
+      expect(JsLocaleHelper.find_moment_locale(["es_MX"])).to eq (locale[:moment_js])
     end
 
     it "correctly registers a new locale when some files exist in core" do
@@ -408,6 +414,9 @@ describe Plugin::Instance do
       expect(locale[:plural]).to eq(plural.with_indifferent_access)
 
       expect(Rails.configuration.assets.precompile).to include("locales/tlh.js")
+
+      expect(JsLocaleHelper.find_message_format_locale(["tlh"], fallback_to_english: true)).to eq(locale[:message_format])
+      expect(JsLocaleHelper.find_moment_locale(["tlh"])).to eq (locale[:moment_js])
     end
 
     it "does not register a new locale when the fallback locale does not exist" do
@@ -416,11 +425,11 @@ describe Plugin::Instance do
     end
 
     [
-      "config/locales/client.foo.yml",
-      "config/locales/server.foo.yml",
-      "lib/javascripts/locale/message_format/foo.js",
-      "lib/javascripts/locale/moment_js/foo.js",
-      "assets/locales/foo.js.erb"
+      "config/locales/client.foo_BAR.yml",
+      "config/locales/server.foo_BAR.yml",
+      "lib/javascripts/locale/message_format/foo_BAR.js",
+      "lib/javascripts/locale/moment_js/foo_BAR.js",
+      "assets/locales/foo_BAR.js.erb"
     ].each do |path|
       it "does not register a new locale when #{path} is missing" do
         path = "#{plugin_path}/#{path}"
@@ -428,7 +437,7 @@ describe Plugin::Instance do
         File.stubs('exist?').with(regexp_matches(/#{Regexp.quote(plugin_path)}.*/)).returns(true)
         File.stubs('exist?').with(path).returns(false)
 
-        register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
+        register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
         expect(DiscoursePluginRegistry.locales.count).to eq(0)
       end
     end
diff --git a/spec/fixtures/plugins/custom_locales/assets/locales/foo.js.erb b/spec/fixtures/plugins/custom_locales/assets/locales/foo.js.erb
deleted file mode 100644
index 601dadb..0000000
--- a/spec/fixtures/plugins/custom_locales/assets/locales/foo.js.erb
+++ /dev/null
@@ -1,2 +0,0 @@
-//= require locales/i18n
-<%= JsLocaleHelper.output_locale(:foo) %>
diff --git a/spec/fixtures/plugins/custom_locales/assets/locales/foo_BAR.js.erb b/spec/fixtures/plugins/custom_locales/assets/locales/foo_BAR.js.erb
new file mode 100644
index 0000000..0c55240
--- /dev/null
+++ b/spec/fixtures/plugins/custom_locales/assets/locales/foo_BAR.js.erb
@@ -0,0 +1,2 @@
+//= require locales/i18n
+<%= JsLocaleHelper.output_locale(:foo_BAR) %>
diff --git a/spec/fixtures/plugins/custom_locales/config/locales/client.foo.yml b/spec/fixtures/plugins/custom_locales/config/locales/client.foo.yml
deleted file mode 100644
index 0152a79..0000000
--- a/spec/fixtures/plugins/custom_locales/config/locales/client.foo.yml
+++ /dev/null
@@ -1 +0,0 @@
-foo:
diff --git a/spec/fixtures/plugins/custom_locales/config/locales/client.foo_BAR.yml b/spec/fixtures/plugins/custom_locales/config/locales/client.foo_BAR.yml
new file mode 100644
index 0000000..434b790
--- /dev/null
+++ b/spec/fixtures/plugins/custom_locales/config/locales/client.foo_BAR.yml
@@ -0,0 +1 @@
+foo_BAR:
diff --git a/spec/fixtures/plugins/custom_locales/config/locales/server.foo.yml b/spec/fixtures/plugins/custom_locales/config/locales/server.foo.yml
deleted file mode 100644
index 0152a79..0000000
--- a/spec/fixtures/plugins/custom_locales/config/locales/server.foo.yml
+++ /dev/null
@@ -1 +0,0 @@
-foo:
diff --git a/spec/fixtures/plugins/custom_locales/config/locales/server.foo_BAR.yml b/spec/fixtures/plugins/custom_locales/config/locales/server.foo_BAR.yml
new file mode 100644
index 0000000..434b790
--- /dev/null
+++ b/spec/fixtures/plugins/custom_locales/config/locales/server.foo_BAR.yml
@@ -0,0 +1 @@
+foo_BAR:

[... diff too long, it was truncated ...]

GitHub sha: 4799cf28

This fixes the issue mentioned in FEATURE: Add localized timezone names by pmusaraj · Pull Request #7004 · discourse/discourse · GitHub

1 Like

FIX: moment_js locale files for zh_CN and zh_TW were not found