From 53e6ccf17b48ca216def631cc199af3e2993b412 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 9 Aug 2017 13:57:58 -0400 Subject: [PATCH] Allow reloading of translation keys for plugins in development mode --- app/assets/javascripts/locales/en.js.erb | 2 ++ lib/js_locale_helper.rb | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/locales/en.js.erb b/app/assets/javascripts/locales/en.js.erb index df19d22fa84..edf422ff76f 100644 --- a/app/assets/javascripts/locales/en.js.erb +++ b/app/assets/javascripts/locales/en.js.erb @@ -1,3 +1,5 @@ //= depend_on 'client.en.yml' //= require locales/i18n + +<% JsLocaleHelper.reloadable_plugins(:en, self) %> <%= JsLocaleHelper.output_locale(:en) %> diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb index c352ee42d74..43a572c43da 100644 --- a/lib/js_locale_helper.rb +++ b/lib/js_locale_helper.rb @@ -1,12 +1,23 @@ module JsLocaleHelper + def self.plugin_client_files(locale_str) + @plugin_client_fileds ||= Dir["#{Rails.root}/plugins/*/config/locales/client.#{locale_str}.yml"] + end + + def self.reloadable_plugins(locale, ctx) + return unless Rails.env.development? + plugin_client_files(locale.to_s).each do |file| + ctx.depend_on(file) + end + end + 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| + plugin_client_files(locale_str).each do |file| if plugin_translations = YAML.load_file(file)[locale_str] translations.deep_merge!(plugin_translations) end @@ -20,6 +31,7 @@ module JsLocaleHelper opts ||= {} @loaded_translations = nil if opts[:force] + @plugin_translations = nil if opts[:force] @loaded_translations ||= HashWithIndifferentAccess.new @loaded_translations[locale] ||= begin