From ee5799805c1f173f348825cc31ee95ece1933262 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 5 Nov 2019 11:54:12 +0000 Subject: [PATCH] FIX: Allow theme translations to be accessed in initializers (#8285) Previously theme translations were loaded along with other plugin API scripts. These run after pre-initializers and initializers when the app boots. This commit moves theme translation loading into pre-initializers, so their behaviour matches core translations more closely. --- app/models/theme_field.rb | 25 +++++++++++++++---------- lib/theme_javascript_compiler.rb | 4 ++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb index ee94014585b..38c19da7bb5 100644 --- a/app/models/theme_field.rb +++ b/app/models/theme_field.rb @@ -60,7 +60,7 @@ class ThemeField < ActiveRecord::Base validates :name, format: { with: /\A[a-z_][a-z0-9_-]*\z/i }, if: Proc.new { |field| ThemeField.theme_var_type_ids.include?(field.type_id) } - BASE_COMPILER_VERSION = 13 + BASE_COMPILER_VERSION = 14 DEPENDENT_CONSTANTS = [BASE_COMPILER_VERSION, GlobalSetting.cdn_url] COMPILER_VERSION = Digest::SHA1.hexdigest(DEPENDENT_CONSTANTS.join) @@ -181,19 +181,24 @@ class ThemeField < ActiveRecord::Base data = translation_data js = <<~JS - /* Translation data for theme #{self.theme_id} (#{self.name})*/ - const data = #{data.to_json}; + export default { + name: "theme-#{theme_id}-translations", + initialize() { + /* Translation data for theme #{self.theme_id} (#{self.name})*/ + const data = #{data.to_json}; - for (let lang in data){ - let cursor = I18n.translations; - for (let key of [lang, "js", "theme_translations"]){ - cursor = cursor[key] = cursor[key] || {}; + for (let lang in data){ + let cursor = I18n.translations; + for (let key of [lang, "js", "theme_translations"]){ + cursor = cursor[key] = cursor[key] || {}; + } + cursor[#{self.theme_id}] = data[lang]; + } } - cursor[#{self.theme_id}] = data[lang]; - } + }; JS - js_compiler.append_plugin_script(js, 0) + js_compiler.append_module(js, "discourse/pre-initializers/theme-#{theme_id}-translations", include_variables: false) rescue ThemeTranslationParser::InvalidYaml => e errors << e.message end diff --git a/lib/theme_javascript_compiler.rb b/lib/theme_javascript_compiler.rb index c11f1aa5e4b..5371fa1e77d 100644 --- a/lib/theme_javascript_compiler.rb +++ b/lib/theme_javascript_compiler.rb @@ -208,8 +208,8 @@ class ThemeJavascriptCompiler @content << script + "\n" end - def append_module(script, name) - script.prepend theme_variables + def append_module(script, name, include_variables: true) + script = "#{theme_variables}#{script}" if include_variables template = Tilt::ES6ModuleTranspilerTemplate.new {} @content << template.module_transpile(script, "", name) rescue MiniRacer::RuntimeError => ex