Class: CKEditor5::Rails::Plugins::CustomTranslationsLoader
- Inherits:
-
Editor::PropsInlinePlugin
- Object
- Editor::PropsBasePlugin
- Editor::PropsInlinePlugin
- CKEditor5::Rails::Plugins::CustomTranslationsLoader
- Defined in:
- lib/ckeditor5/rails/plugins/custom_translations_loader.rb
Instance Attribute Summary
Attributes inherited from Editor::PropsInlinePlugin
Attributes inherited from Editor::PropsBasePlugin
Instance Method Summary collapse
-
#initialize(translations) ⇒ CustomTranslationsLoader
constructor
rubocop:disable Metrics/MethodLength.
Methods inherited from Editor::PropsInlinePlugin
Methods inherited from Editor::PropsBasePlugin
normalize, #preload_assets_bundle, #to_h
Constructor Details
#initialize(translations) ⇒ CustomTranslationsLoader
rubocop:disable Metrics/MethodLength
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ckeditor5/rails/plugins/custom_translations_loader.rb', line 7 def initialize(translations) # rubocop:disable Metrics/MethodLength code = <<~JS.freeze const { Plugin } = await import('ckeditor5'); function resolveTranslationReferences(uiLanguage, config, visited = new WeakSet()) { if (!config || typeof config !== 'object') { return config; } if (visited.has(config)) { return config; } visited.add(config); if (Array.isArray(config)) { config.forEach((item, index) => { config[index] = resolveTranslationReferences(uiLanguage, item, visited); }); return config; } for (const key of Object.getOwnPropertyNames(config)) { const value = config[key]; if (value && typeof value === 'object') { if (value.$translation) { const translationKey = value.$translation; const translations = window.CKEDITOR_TRANSLATIONS?.[uiLanguage]; if (!translations?.dictionary[translationKey]) { console.warn(`Translation not found for key: ${translationKey}`); } config[key] = translations?.dictionary[translationKey] || translationKey; } else { resolveTranslationReferences(uiLanguage, value, visited); } } } return config; } return class CustomTranslationsLoader extends Plugin { static CUSTOM_TRANSLATIONS = Object.create( #{translations.to_json} ); static get pluginName() { return 'CustomTranslationsLoader'; } constructor( editor ) { super( editor ); const { locale, config } = this.editor; this.#extendPack(); resolveTranslationReferences(locale.uiLanguage, config._config) } #extendPack() { const { uiLanguage } = this.editor.locale; const translations = this.#translations; if (!window.CKEDITOR_TRANSLATIONS) { window.CKEDITOR_TRANSLATIONS = {}; } if (!window.CKEDITOR_TRANSLATIONS[uiLanguage]) { window.CKEDITOR_TRANSLATIONS[uiLanguage] = { dictionary: {} }; } Object.entries(translations).forEach(([key, value]) => { window.CKEDITOR_TRANSLATIONS[uiLanguage].dictionary[key] = value; }); } get #translations() { const { uiLanguage } = this.editor.locale; return CustomTranslationsLoader.CUSTOM_TRANSLATIONS[uiLanguage]; } } JS super(:CustomTranslationsLoader, code) compress! end |