Merge pull request #4471 from tgxworld/fix_plugin_translations_bundle_not_included

FIX: Plugin "admin_js" translations bundle was not fetched.
This commit is contained in:
Guo Xiang Tan 2016-09-29 07:58:31 +02:00 committed by GitHub
commit 9859bfb072
5 changed files with 63 additions and 27 deletions

View File

@ -4,27 +4,30 @@ class ExtraLocalesController < ApplicationController
skip_before_filter :check_xhr, :preload_json skip_before_filter :check_xhr, :preload_json
def show def show
locale_str = I18n.locale.to_s
translations = JsLocaleHelper.translations_for(locale_str)
bundle = params[:bundle] bundle = params[:bundle]
raise Discourse::InvalidAccess.new unless bundle =~ /^[a-z]+$/ raise Discourse::InvalidAccess.new unless bundle =~ /^[a-z]+$/
for_key = translations[locale_str]["#{bundle}_js"]
locale_str = I18n.locale.to_s
translations = JsLocaleHelper.translations_for(locale_str)
for_key = translations[locale_str]["#{bundle}_js"]
if for_key.present? if plugin_for_key = JsLocaleHelper.plugin_translations(locale_str)["#{bundle}_js"]
js = <<-JS for_key.deep_merge!(plugin_for_key)
end
js =
if for_key.present?
<<~JS
(function() { (function() {
if (window.I18n) { if (window.I18n) {
window.I18n.extras = window.I18n.extras || []; window.I18n.extras = window.I18n.extras || [];
window.I18n.extras.push(#{for_key.to_json}); window.I18n.extras.push(#{for_key.to_json});
} }
})(); })();
JS JS
else else
js = "" ""
end end
render text: js, content_type: "application/javascript" render text: js, content_type: "application/javascript"
end end

View File

@ -1,5 +1,19 @@
module JsLocaleHelper module JsLocaleHelper
def self.plugin_translations(locale_str)
@plugin_translations ||= HashWithIndifferentAccess.new
@plugin_translations[locale_str] ||= begin
translations = {}
Dir["#{Rails.root}/plugins/*/config/locales/client.#{locale_str}.yml"].each do |file|
translations.deep_merge! YAML::load(File.open(file))[locale_str]
end
translations
end
end
def self.load_translations(locale, opts=nil) def self.load_translations(locale, opts=nil)
opts ||= {} opts ||= {}
@ -11,14 +25,9 @@ module JsLocaleHelper
# load default translations # load default translations
translations = YAML::load(File.open("#{Rails.root}/config/locales/client.#{locale_str}.yml")) translations = YAML::load(File.open("#{Rails.root}/config/locales/client.#{locale_str}.yml"))
# load plugins translations
plugin_translations = {}
Dir["#{Rails.root}/plugins/*/config/locales/client.#{locale_str}.yml"].each do |file|
plugin_translations.deep_merge! YAML::load(File.open(file))
end
# merge translations (plugin translations overwrite default translations) # merge translations (plugin translations overwrite default translations)
translations[locale_str]['js'].deep_merge!(plugin_translations[locale_str]['js']) if translations[locale_str] && plugin_translations[locale_str] && plugin_translations[locale_str]['js'] translations[locale_str]['js'].deep_merge!(plugin_translations(locale_str)['js']) if translations[locale_str] && plugin_translations(locale_str) && plugin_translations(locale_str)['js']
translations translations
end end
@ -71,17 +80,16 @@ module JsLocaleHelper
site_locale = SiteSetting.default_locale.to_sym site_locale = SiteSetting.default_locale.to_sym
if Rails.env.development? translations =
translations = load_translations(locale_sym, force: true) if Rails.env.development?
else load_translations(locale_sym, force: true)
if locale_sym == :en elsif locale_sym == :en
translations = load_translations(locale_sym) load_translations(locale_sym)
elsif locale_sym == site_locale || site_locale == :en elsif locale_sym == site_locale || site_locale == :en
translations = load_translations_merged(locale_sym, :en) load_translations_merged(locale_sym, :en)
else else
translations = load_translations_merged(locale_sym, site_locale, :en) load_translations_merged(locale_sym, site_locale, :en)
end end
end
I18n.locale = current_locale I18n.locale = current_locale

View File

@ -18,6 +18,7 @@ describe JsLocaleHelper do
JsLocaleHelper.extend StubLoadTranslations JsLocaleHelper.extend StubLoadTranslations
after do after do
I18n.locale = :en
JsLocaleHelper.clear_cache! JsLocaleHelper.clear_cache!
end end

View File

@ -18,6 +18,26 @@ describe ExtraLocalesController do
get :show, bundle: '-invalid..character!!' get :show, bundle: '-invalid..character!!'
expect(response).to_not be_success expect(response).to_not be_success
end end
it "should include plugin translations" do
JsLocaleHelper.expects(:plugin_translations).with("en").returns({
"admin_js" => {
"admin" => {
"site_settings" => {
"categories" => {
"github_badges" => "Github Badges"
}
}
}
}
}).at_least_once
get :show, bundle: "admin"
expect(response).to be_success
expect(response.body.include?("github_badges")).to eq(true)
end
end end
end end

View File

@ -18,9 +18,13 @@ describe Badge do
badge = Badge.find_by_name("Basic User") badge = Badge.find_by_name("Basic User")
name_english = badge.name name_english = badge.name
I18n.locale = 'fr' begin
I18n.locale = 'fr'
expect(badge.display_name).not_to eq(name_english) expect(badge.display_name).not_to eq(name_english)
ensure
I18n.locale = :en
end
end end
it 'handles changes on badge description and long description correctly for system badges' do it 'handles changes on badge description and long description correctly for system badges' do