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.
This commit is contained in:
Mark VanLandingham 2020-11-11 09:44:01 -06:00 committed by GitHub
parent a48f7ba61c
commit be07853cc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 4 deletions

View File

@ -20,7 +20,9 @@ module I18n
# force explicit loading # force explicit loading
def load_translations(*filenames) def load_translations(*filenames)
unless filenames.empty? 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
end end
@ -33,6 +35,13 @@ module I18n
end end
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) def self.create_search_regexp(query, as_string: false)
regexp = Regexp.escape(query) regexp = Regexp.escape(query)

View File

@ -3,7 +3,9 @@
module JsLocaleHelper module JsLocaleHelper
def self.plugin_client_files(locale_str) 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 end
def self.reloadable_plugins(locale_sym, ctx) def self.reloadable_plugins(locale_sym, ctx)

View File

@ -886,8 +886,8 @@ class Plugin::Instance
locales.each do |locale, opts| locales.each do |locale, opts|
opts = opts.dup opts = opts.dup
opts[:client_locale_file] = File.join(root_path, "config/locales/client.#{locale}.yml") opts[:client_locale_file] = Dir["#{root_path}/config/locales/client*.#{locale}.yml"].first || ""
opts[:server_locale_file] = File.join(root_path, "config/locales/server.#{locale}.yml") 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") opts[:js_locale_file] = File.join(root_path, "assets/locales/#{locale}.js.erb")
locale_chain = opts[:fallbackLocale] ? [locale, opts[:fallbackLocale]] : [locale] locale_chain = opts[:fallbackLocale] ? [locale, opts[:fallbackLocale]] : [locale]

View File

@ -104,4 +104,24 @@ describe I18n::Backend::DiscourseI18n do
expect { backend.translate(:ru, :airplanes, count: 2) }.to raise_error(I18n::InvalidPluralizationData) expect { backend.translate(:ru, :airplanes, count: 2) }.to raise_error(I18n::InvalidPluralizationData)
end end
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 end