A platform for community discussion. Free, open, simple.
DEV: Simplify theme translation JS generation (#33104)
David Taylor033cccdf17a5
diff --git a/app/models/theme.rb b/app/models/theme.rbindex b0a02921cdd..e41c08324aa 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -6,7 +6,7 @@ require "json_schemer"class Theme < ActiveRecord::Baseinclude GlobalPath- BASE_COMPILER_VERSION = 88+ BASE_COMPILER_VERSION = 89class SettingsMigrationError < StandardErrorenddiff --git a/app/models/theme_field.rb b/app/models/theme_field.rbindex a63b6835a7c..c7e200f8fa1 100644--- a/app/models/theme_field.rb+++ b/app/models/theme_field.rb@@ -318,46 +318,33 @@ class ThemeField < ActiveRecord::Basedef process_translationerrors = []javascript_cache || build_javascript_cache- js_compiler = ThemeJavascriptCompiler.new(theme_id, self.theme.name)- begin- data = translation_data- js = <<~JS- 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];- }- }- };- JS+ data = translation_data- js_compiler.append_module(- js,- "discourse/pre-initializers/theme-#{theme_id}-translations",- "js",- include_variables: false,- )- rescue ThemeTranslationParser::InvalidYaml => e- errors << e.message- end+ js = <<~JS+ /* Translation data for theme #{self.theme_id} (#{self.name})*/+ const data = #{data.to_json};- javascript_cache.content = js_compiler.content- javascript_cache.source_map = js_compiler.source_map+ for (let lang in data){+ let cursor = I18n.translations;+ for (let key of [lang, "js", "theme_translations"]){+ cursor = cursor[key] ??= {};+ }+ cursor[#{self.theme_id}] = data[lang];+ }+ JS++ javascript_cache.content = js+ javascript_cache.source_map = niljavascript_cache.save!+doc = ""doc = <<~HTML.html_safe if javascript_cache.content.present?<script defer src="#{javascript_cache.url}" data-theme-id="#{theme_id}" nonce="#{ThemeField::CSP_NONCE_PLACEHOLDER}"></script>HTML[doc, errors&.join("\n")]+ rescue ThemeTranslationParser::InvalidYaml => e+ ["", e.message]enddef validate_yaml!