From be07853cc19c61858f3e0b0235b8a02d6059db43 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Wed, 11 Nov 2020 09:44:01 -0600 Subject: [PATCH] DEV: Add plugins client/server translation yml file priority structure (#11194) Plugin client.en.yml and server.en.yml can now be client/server-(1-100).en.yml. 1 is the lowest priority, and 100 is the highest priority. This allows plugins to set their priority higher than other plugins, so that they can override eachothers' translations. --- lib/i18n/backend/discourse_i18n.rb | 11 ++++++++++- lib/js_locale_helper.rb | 4 +++- lib/plugin/instance.rb | 4 ++-- spec/lib/i18n/discourse_i18n_spec.rb | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/i18n/backend/discourse_i18n.rb b/lib/i18n/backend/discourse_i18n.rb index 96971d93dd1..fa6533165d8 100644 --- a/lib/i18n/backend/discourse_i18n.rb +++ b/lib/i18n/backend/discourse_i18n.rb @@ -20,7 +20,9 @@ module I18n # force explicit loading def load_translations(*filenames) unless filenames.empty? - filenames.flatten.each { |filename| load_file(filename) } + self.class.sort_locale_files(filenames.flatten).each do |filename| + load_file(filename) + end end end @@ -33,6 +35,13 @@ module I18n end end + def self.sort_locale_files(files) + files.sort_by do |filename| + matches = /(?:client|server)-([1-9]|[1-9][0-9]|100)\..+\.yml/.match(filename) + matches&.[](1)&.to_i || 0 + end + end + def self.create_search_regexp(query, as_string: false) regexp = Regexp.escape(query) diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb index 8864eca71a2..d15ad161e19 100644 --- a/lib/js_locale_helper.rb +++ b/lib/js_locale_helper.rb @@ -3,7 +3,9 @@ module JsLocaleHelper def self.plugin_client_files(locale_str) - Dir["#{Rails.root}/plugins/*/config/locales/client.#{locale_str}.yml"] + I18n::Backend::DiscourseI18n.sort_locale_files( + Dir["#{Rails.root}/plugins/*/config/locales/client*.#{locale_str}.yml"] + ) end def self.reloadable_plugins(locale_sym, ctx) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 0f0dc1d2291..3471c385050 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -886,8 +886,8 @@ class Plugin::Instance locales.each do |locale, opts| opts = opts.dup - opts[:client_locale_file] = File.join(root_path, "config/locales/client.#{locale}.yml") - opts[:server_locale_file] = File.join(root_path, "config/locales/server.#{locale}.yml") + opts[:client_locale_file] = Dir["#{root_path}/config/locales/client*.#{locale}.yml"].first || "" + opts[:server_locale_file] = Dir["#{root_path}/config/locales/server*.#{locale}.yml"].first || "" opts[:js_locale_file] = File.join(root_path, "assets/locales/#{locale}.js.erb") locale_chain = opts[:fallbackLocale] ? [locale, opts[:fallbackLocale]] : [locale] diff --git a/spec/lib/i18n/discourse_i18n_spec.rb b/spec/lib/i18n/discourse_i18n_spec.rb index 39c0691d363..ee2258db139 100644 --- a/spec/lib/i18n/discourse_i18n_spec.rb +++ b/spec/lib/i18n/discourse_i18n_spec.rb @@ -104,4 +104,24 @@ describe I18n::Backend::DiscourseI18n do expect { backend.translate(:ru, :airplanes, count: 2) }.to raise_error(I18n::InvalidPluralizationData) end end + + describe ".sort_local_files" do + it "sorts an array of client ymls with '-(highest-number)' being last" do + expect(I18n::Backend::DiscourseI18n.sort_locale_files( + [ + 'discourse/plugins/discourse-second/config/locales/client-99.es.yml', + 'discourse/plugins/discourse-first/config/locales/client.es.yml', + 'discourse/plugins/discourse-third/config/locales/client-2.es.yml', + 'discourse/plugins/discourse-third/config/locales/client-3.bs_BA.yml', + ] + )).to eq( + [ + 'discourse/plugins/discourse-first/config/locales/client.es.yml', + 'discourse/plugins/discourse-third/config/locales/client-2.es.yml', + 'discourse/plugins/discourse-third/config/locales/client-3.bs_BA.yml', + 'discourse/plugins/discourse-second/config/locales/client-99.es.yml', + ] + ) + end + end end