From 69894a0aa9e9f55dae656ca12ad656bd23db34cf Mon Sep 17 00:00:00 2001 From: Maciej Bukowski Date: Fri, 10 Nov 2017 17:57:29 +0100 Subject: [PATCH 1/5] WIP - Aligning code to translation service v2. --- src/translation-service.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/translation-service.js b/src/translation-service.js index 1aad935..0b66cd2 100644 --- a/src/translation-service.js +++ b/src/translation-service.js @@ -3,6 +3,8 @@ * For licensing, see LICENSE.md. */ +/* globals window */ + /** * @module utils/translation-service */ @@ -34,7 +36,7 @@ export function add( lang, translations ) { * When no translation is defined in the dictionary or the dictionary doesn't exist this function returns * the original string without the `'[context: ]'` (happens in development and single-language modes). * - * In a single-language mode (when values passed to `t()` were replaced with target languange strings) the dictionary + * In a single-language mode (when values passed to `t()` were replaced with target language strings) the dictionary * is left empty, so this function will return the original strings always. * * translate( 'pl', 'Cancel [context: reject]' ); @@ -44,14 +46,22 @@ export function add( lang, translations ) { * @returns {String} Translated sentence. */ export function translate( lang, translationKey ) { - if ( !hasTranslation( lang, translationKey ) ) { + const numberOfLanguages = getNumberOfLanguages(); + + if ( numberOfLanguages === 1 ) { + // Override the language to the only supported one. + // This can't be done in the `Locale` class, because the translations comes after the `Locale` class initializes. + lang = Object.keys( dictionaries )[ 0 ]; + } + + if ( numberOfLanguages === 0 || !hasTranslation( lang, translationKey ) ) { return translationKey.replace( / \[context: [^\]]+\]$/, '' ); } return dictionaries[ lang ][ translationKey ]; } -// Checks whether the dictionary exists and translaiton in that dictionary exists. +// Checks whether the dictionary exists and translation in that dictionary exists. function hasTranslation( lang, translationKey ) { return ( ( lang in dictionaries ) && @@ -67,3 +77,12 @@ function hasTranslation( lang, translationKey ) { export function _clear() { dictionaries = {}; } + +function getNumberOfLanguages() { + return Object.keys( dictionaries ).length; +} + +// Export globally add function to enable adding later translations. +// See https://github.com/ckeditor/ckeditor5/issues/624 +window.CKEDITOR_TRANSLATIONS = window.CKEDITOR_TRANSLATIONS || {}; +window.CKEDITOR_TRANSLATIONS.add = add; From 72e14f1fed68144408fab32b3c63aa6ee9dcf6b3 Mon Sep 17 00:00:00 2001 From: Maciej Bukowski Date: Tue, 14 Nov 2017 19:51:12 +0100 Subject: [PATCH 2/5] Improved comment. --- src/translation-service.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/translation-service.js b/src/translation-service.js index 0b66cd2..a624a50 100644 --- a/src/translation-service.js +++ b/src/translation-service.js @@ -50,7 +50,8 @@ export function translate( lang, translationKey ) { if ( numberOfLanguages === 1 ) { // Override the language to the only supported one. - // This can't be done in the `Locale` class, because the translations comes after the `Locale` class initializes. + // This can't be done in the `Locale` class, + // because the translations comes after the `Locale` class initializes. lang = Object.keys( dictionaries )[ 0 ]; } From f197ea39542e0ad3bf4d5442a24de2a869e438cc Mon Sep 17 00:00:00 2001 From: Maciej Bukowski Date: Thu, 16 Nov 2017 18:14:02 +0100 Subject: [PATCH 3/5] Fixed `translate-service` fn. --- src/translation-service.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/translation-service.js b/src/translation-service.js index a624a50..309abcc 100644 --- a/src/translation-service.js +++ b/src/translation-service.js @@ -59,7 +59,8 @@ export function translate( lang, translationKey ) { return translationKey.replace( / \[context: [^\]]+\]$/, '' ); } - return dictionaries[ lang ][ translationKey ]; + // In case of missing translations we still need to cut off the `[context: ]` parts. + return dictionaries[ lang ][ translationKey ].replace( / \[context: [^\]]+\]$/, '' ); } // Checks whether the dictionary exists and translation in that dictionary exists. From 433b820e9f30a342f48e3be4a395cec4084d0715 Mon Sep 17 00:00:00 2001 From: Maciej Bukowski Date: Tue, 21 Nov 2017 13:04:25 +0100 Subject: [PATCH 4/5] Added missing tests for `ckeditor5-utils`. --- src/translation-service.js | 11 ++++++++--- tests/translation-service.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/translation-service.js b/src/translation-service.js index 309abcc..392bdde 100644 --- a/src/translation-service.js +++ b/src/translation-service.js @@ -20,6 +20,12 @@ let dictionaries = {}; * 'Cancel [context: reject]': 'Anuluj' * } ); * + * That function is accessible globally via `window.CKEDITOR_TRANSLATIONS.add()`. So it's possible to add translation from + * the other script, just after that one. + * + * + * + * * @param {String} lang Target language. * @param {Object.} translations Translations which will be added to the dictionary. */ @@ -42,7 +48,7 @@ export function add( lang, translations ) { * translate( 'pl', 'Cancel [context: reject]' ); * * @param {String} lang Target language. - * @param {String} translationKey String which is going to be translated. + * @param {String} translationKey String that will be translated. * @returns {String} Translated sentence. */ export function translate( lang, translationKey ) { @@ -50,8 +56,7 @@ export function translate( lang, translationKey ) { if ( numberOfLanguages === 1 ) { // Override the language to the only supported one. - // This can't be done in the `Locale` class, - // because the translations comes after the `Locale` class initializes. + // This can't be done in the `Locale` class, because the translations comes after the `Locale` class initialization. lang = Object.keys( dictionaries )[ 0 ]; } diff --git a/tests/translation-service.js b/tests/translation-service.js index 04938ab..59d7868 100644 --- a/tests/translation-service.js +++ b/tests/translation-service.js @@ -3,6 +3,8 @@ * For licensing, see LICENSE.md. */ +/* globals window */ + import { translate, add, _clear } from '../src/translation-service'; describe( 'translation-service', () => { @@ -45,6 +47,17 @@ describe( 'translation-service', () => { expect( translation ).to.be.equal( 'Bold' ); } ); + it( 'should use provided language if only one is provided', () => { + add( 'pl', { + 'OK': 'OK', + 'Cancel [context: reject]': 'Anuluj' + } ); + + const translation = translate( 'de', 'Cancel [context: reject]' ); + + expect( translation ).to.be.equal( 'Anuluj' ); + } ); + it( 'should be able to merge translations', () => { add( 'pl', { 'OK': 'OK', @@ -62,4 +75,19 @@ describe( 'translation-service', () => { expect( translationPL ).to.be.equal( 'Anuluj' ); expect( translationEN ).to.be.equal( 'Cancel' ); } ); + + it( 'should expose `add` function globally', () => { + expect( window.CKEDITOR_TRANSLATIONS ).to.be.an( 'object' ); + expect( window.CKEDITOR_TRANSLATIONS.add ).to.be.a( 'function' ); + expect( window.CKEDITOR_TRANSLATIONS.add ).to.equal( add ); + + window.CKEDITOR_TRANSLATIONS.add( 'pl', { + 'OK': 'OK', + 'Cancel [context: reject]': 'Anuluj' + } ); + + const translationPL = translate( 'pl', 'Cancel [context: reject]' ); + + expect( translationPL ).to.be.equal( 'Anuluj' ); + } ); } ); From cf8af23902f3d506affaf41419304292efc56f4e Mon Sep 17 00:00:00 2001 From: Maciej Bukowski Date: Mon, 27 Nov 2017 16:37:35 +0100 Subject: [PATCH 5/5] Fixed tests. --- tests/translation-service.js | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/translation-service.js b/tests/translation-service.js index 59d7868..c00dbfd 100644 --- a/tests/translation-service.js +++ b/tests/translation-service.js @@ -8,7 +8,7 @@ import { translate, add, _clear } from '../src/translation-service'; describe( 'translation-service', () => { - beforeEach( () => { + afterEach( () => { _clear(); } ); @@ -79,15 +79,5 @@ describe( 'translation-service', () => { it( 'should expose `add` function globally', () => { expect( window.CKEDITOR_TRANSLATIONS ).to.be.an( 'object' ); expect( window.CKEDITOR_TRANSLATIONS.add ).to.be.a( 'function' ); - expect( window.CKEDITOR_TRANSLATIONS.add ).to.equal( add ); - - window.CKEDITOR_TRANSLATIONS.add( 'pl', { - 'OK': 'OK', - 'Cancel [context: reject]': 'Anuluj' - } ); - - const translationPL = translate( 'pl', 'Cancel [context: reject]' ); - - expect( translationPL ).to.be.equal( 'Anuluj' ); } ); } );