33 * For licensing, see LICENSE.md.
44 */
55
6+ /* globals window */
7+
68/**
79 * @module utils/translation-service
810 */
@@ -18,6 +20,12 @@ let dictionaries = {};
1820 * 'Cancel [context: reject]': 'Anuluj'
1921 * } );
2022 *
23+ * That function is accessible globally via `window.CKEDITOR_TRANSLATIONS.add()`. So it's possible to add translation from
24+ * the other script, just after that one.
25+ *
26+ * <script src="./path/to/ckeditor.js"></script>
27+ * <script src="./path/to/translations/en.js"></script>
28+ *
2129 * @param {String } lang Target language.
2230 * @param {Object.<String, String> } translations Translations which will be added to the dictionary.
2331 */
@@ -34,24 +42,33 @@ export function add( lang, translations ) {
3442 * When no translation is defined in the dictionary or the dictionary doesn't exist this function returns
3543 * the original string without the `'[context: ]'` (happens in development and single-language modes).
3644 *
37- * In a single-language mode (when values passed to `t()` were replaced with target languange strings) the dictionary
45+ * In a single-language mode (when values passed to `t()` were replaced with target language strings) the dictionary
3846 * is left empty, so this function will return the original strings always.
3947 *
4048 * translate( 'pl', 'Cancel [context: reject]' );
4149 *
4250 * @param {String } lang Target language.
43- * @param {String } translationKey String which is going to be translated.
51+ * @param {String } translationKey String that will be translated.
4452 * @returns {String } Translated sentence.
4553 */
4654export function translate ( lang , translationKey ) {
47- if ( ! hasTranslation ( lang , translationKey ) ) {
55+ const numberOfLanguages = getNumberOfLanguages ( ) ;
56+
57+ if ( numberOfLanguages === 1 ) {
58+ // Override the language to the only supported one.
59+ // This can't be done in the `Locale` class, because the translations comes after the `Locale` class initialization.
60+ lang = Object . keys ( dictionaries ) [ 0 ] ;
61+ }
62+
63+ if ( numberOfLanguages === 0 || ! hasTranslation ( lang , translationKey ) ) {
4864 return translationKey . replace ( / \[ c o n t e x t : [ ^ \] ] + \] $ / , '' ) ;
4965 }
5066
51- return dictionaries [ lang ] [ translationKey ] ;
67+ // In case of missing translations we still need to cut off the `[context: ]` parts.
68+ return dictionaries [ lang ] [ translationKey ] . replace ( / \[ c o n t e x t : [ ^ \] ] + \] $ / , '' ) ;
5269}
5370
54- // Checks whether the dictionary exists and translaiton in that dictionary exists.
71+ // Checks whether the dictionary exists and translation in that dictionary exists.
5572function hasTranslation ( lang , translationKey ) {
5673 return (
5774 ( lang in dictionaries ) &&
@@ -67,3 +84,12 @@ function hasTranslation( lang, translationKey ) {
6784export function _clear ( ) {
6885 dictionaries = { } ;
6986}
87+
88+ function getNumberOfLanguages ( ) {
89+ return Object . keys ( dictionaries ) . length ;
90+ }
91+
92+ // Export globally add function to enable adding later translations.
93+ // See https://github.com/ckeditor/ckeditor5/issues/624
94+ window . CKEDITOR_TRANSLATIONS = window . CKEDITOR_TRANSLATIONS || { } ;
95+ window . CKEDITOR_TRANSLATIONS . add = add ;
0 commit comments