Allow reloading of translation keys for plugins in development mode

This commit is contained in:
Robin Ward 2017-08-09 13:57:58 -04:00
parent 197944edaa
commit 53e6ccf17b
2 changed files with 15 additions and 1 deletions

View File

@ -1,3 +1,5 @@
//= depend_on 'client.en.yml'
//= require locales/i18n
<% JsLocaleHelper.reloadable_plugins(:en, self) %>
<%= JsLocaleHelper.output_locale(:en) %>

View File

@ -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