From 291acca4fa106ef61213e0a0be0baf87c1093030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 19 Nov 2013 16:42:28 +0100 Subject: [PATCH] autoload server/client locales in plugins --- config/application.rb | 6 ++---- lib/discourse_plugin_registry.rb | 31 ++++++++++++++----------------- lib/js_locale_helper.rb | 9 ++++++++- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/config/application.rb b/config/application.rb index 0c6cd5e05..5bc51eefc 100644 --- a/config/application.rb +++ b/config/application.rb @@ -57,7 +57,6 @@ module Discourse config.assets.precompile += ['common.css', 'desktop.css', 'mobile.css', 'admin.js', 'admin.css', 'shiny/shiny.css', 'preload_store.js'] - # Precompile all defer Dir.glob("#{config.root}/app/assets/javascripts/defer/*.js").each do |file| config.assets.precompile << "defer/#{File.basename(file)}" @@ -80,9 +79,8 @@ module Discourse # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. config.time_zone = 'Eastern Time (US & Canada)' - # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. - # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] - # config.i18n.default_locale = :de + # auto-load server locale in plugins + config.i18n.load_path += Dir["#{Rails.root}/plugins/*/config/locales/server.*.yml"] # Configure the default encoding used in templates for Ruby 1.9. config.encoding = 'utf-8' diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index 11d12d18b..c5266664e 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -9,11 +9,6 @@ class DiscoursePluginRegistry attr_accessor :stylesheets # Default accessor values - # - def stylesheets - @stylesheets ||= Set.new - end - def javascripts @javascripts ||= Set.new end @@ -21,13 +16,15 @@ class DiscoursePluginRegistry def server_side_javascripts @server_side_javascripts ||= Set.new end - end + def stylesheets + @stylesheets ||= Set.new + end + end def register_js(filename, options={}) # If we have a server side option, add that too. self.class.server_side_javascripts << options[:server_side] if options[:server_side].present? - self.class.javascripts << filename end @@ -35,26 +32,26 @@ class DiscoursePluginRegistry self.class.stylesheets << filename end - def stylesheets - self.class.stylesheets - end - def register_archetype(name, options={}) Archetype.register(name, options) end - def server_side_javascripts - self.class.javascripts - end - def javascripts self.class.javascripts end + def server_side_javascripts + self.class.server_side_javascripts + end + + def stylesheets + self.class.stylesheets + end + def self.clear - self.stylesheets = nil - self.server_side_javascripts = nil self.javascripts = nil + self.server_side_javascripts = nil + self.stylesheets = nil end def self.setup(plugin_class) diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb index 7956ccdb5..b17038020 100644 --- a/lib/js_locale_helper.rb +++ b/lib/js_locale_helper.rb @@ -1,10 +1,17 @@ module JsLocaleHelper def self.output_locale(locale, translations = nil) - locale_str = locale.to_s + # load default translations 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.merge! YAML::load(File.open(file)) + end + # merge translations (plugin translations overwrite default translations) + translations[locale_str]['js'].merge!(plugin_translations[locale_str]['js']) # We used to split the admin versus the client side, but it's much simpler to just # include both for now due to the small size of the admin section.