From 4799cf281131e1bf34fcb6aad49b00769cff469e Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Tue, 19 Feb 2019 15:27:30 +0100 Subject: [PATCH] FIX: moment_js locale files provided by plugins were ignored --- lib/js_locale_helper.rb | 10 +++-- spec/components/plugin/instance_spec.rb | 39 ++++++++++++------- .../custom_locales/assets/locales/foo.js.erb | 2 - .../assets/locales/foo_BAR.js.erb | 2 + .../config/locales/client.foo.yml | 1 - .../config/locales/client.foo_BAR.yml | 1 + .../config/locales/server.foo.yml | 1 - .../config/locales/server.foo_BAR.yml | 1 + .../message_format/{foo.js => foo_BAR.js} | 0 .../locale/moment_js/{foo.js => foo_BAR.js} | 0 10 files changed, 34 insertions(+), 23 deletions(-) delete mode 100644 spec/fixtures/plugins/custom_locales/assets/locales/foo.js.erb create mode 100644 spec/fixtures/plugins/custom_locales/assets/locales/foo_BAR.js.erb delete mode 100644 spec/fixtures/plugins/custom_locales/config/locales/client.foo.yml create mode 100644 spec/fixtures/plugins/custom_locales/config/locales/client.foo_BAR.yml delete mode 100644 spec/fixtures/plugins/custom_locales/config/locales/server.foo.yml create mode 100644 spec/fixtures/plugins/custom_locales/config/locales/server.foo_BAR.yml rename spec/fixtures/plugins/custom_locales/lib/javascripts/locale/message_format/{foo.js => foo_BAR.js} (100%) rename spec/fixtures/plugins/custom_locales/lib/javascripts/locale/moment_js/{foo.js => foo_BAR.js} (100%) diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb index 22fe03be3b9..aaefc703db5 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 936f99b2eea..ad31cfb4984 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 601dadb7774..00000000000 --- 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 00000000000..0c552402810 --- /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 0152a79652d..00000000000 --- 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 00000000000..434b7903d5f --- /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 0152a79652d..00000000000 --- 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 00000000000..434b7903d5f --- /dev/null +++ b/spec/fixtures/plugins/custom_locales/config/locales/server.foo_BAR.yml @@ -0,0 +1 @@ +foo_BAR: diff --git a/spec/fixtures/plugins/custom_locales/lib/javascripts/locale/message_format/foo.js b/spec/fixtures/plugins/custom_locales/lib/javascripts/locale/message_format/foo_BAR.js similarity index 100% rename from spec/fixtures/plugins/custom_locales/lib/javascripts/locale/message_format/foo.js rename to spec/fixtures/plugins/custom_locales/lib/javascripts/locale/message_format/foo_BAR.js diff --git a/spec/fixtures/plugins/custom_locales/lib/javascripts/locale/moment_js/foo.js b/spec/fixtures/plugins/custom_locales/lib/javascripts/locale/moment_js/foo_BAR.js similarity index 100% rename from spec/fixtures/plugins/custom_locales/lib/javascripts/locale/moment_js/foo.js rename to spec/fixtures/plugins/custom_locales/lib/javascripts/locale/moment_js/foo_BAR.js