Skip to content

A platform for community discussion. Free, open, simple.

FIX: Allow theme translations to be accessed in initializers (#8285)

David Tayloree5799805c1f
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};
-
- for (let lang in data){
- let cursor = I18n.translations;
- for (let key of [lang, "js", "theme_translations"]){
- cursor = cursor[key] = cursor[key] || {};
+ 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] || {};
+ }
+ 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