Skip to content

Commit e06d9da

Browse files
committed
Load translations for CKEDITOR if language is not english
1 parent b035014 commit e06d9da

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

assets/controllers/elements/ckeditor_controller.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ import {EditorWatchdog} from 'ckeditor5';
2828
import "ckeditor5/ckeditor5.css";;
2929
import "../../css/components/ckeditor.css";
3030

31+
const translationContext = require.context(
32+
'ckeditor5/translations',
33+
false,
34+
/\.js$/
35+
);
36+
37+
function loadTranslation(language) {
38+
if (!language || language === 'en') {
39+
return null;
40+
}
41+
const lang = language.slice(0, 2);
42+
const path = `./${lang}.js`;
43+
if (translationContext.keys().includes(path)) {
44+
const module = translationContext(path);
45+
return module.default;
46+
} else {
47+
return null;
48+
}
49+
}
50+
3151
/* stimulusFetch: 'lazy' */
3252
export default class extends Controller {
3353
connect() {
@@ -63,6 +83,12 @@ export default class extends Controller {
6383
}
6484
}
6585

86+
//Load translations if not english
87+
let translations = loadTranslation(language);
88+
if (translations) {
89+
config.translations = [translations];
90+
}
91+
6692
const watchdog = new EditorWatchdog();
6793
watchdog.setCreator((elementOrData, editorConfig) => {
6894
return EDITOR_TYPE.create(elementOrData, editorConfig)

templates/base.html.twig

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@
6666
{% block javascripts %}
6767
{{ encore_entry_script_tags('app') }}
6868
{{ encore_entry_script_tags('webauthn_tfa') }}
69-
70-
{# load translation files for ckeditor #}
71-
{% set two_chars_locale = app.request.locale|default("en")|slice(0,2) %}
72-
{% if two_chars_locale != "en" %}
73-
<script src="{{ asset("build/ckeditor_translations/" ~ two_chars_locale ~ ".js") }}"></script>
74-
{% endif %}
7569
{% endblock %}
7670
</head>
7771
<body data-base-url="{{ path('homepage', {'_locale': app.request.locale}) }}" data-locale="{{ app.request.locale|default("en")|slice(0,2) }}">

0 commit comments

Comments
 (0)