Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit a51767a

Browse files
authored
Merge pull request #205 from ckeditor/t/ckeditor5/624
Other: Aligned code to the new Translation Service (ckeditor/ckeditor5#624).
2 parents 848a818 + 0716dfc commit a51767a

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/translation-service.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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
*/
4654
export 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( / \[context: [^\]]+\]$/, '' );
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( / \[context: [^\]]+\]$/, '' );
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.
5572
function hasTranslation( lang, translationKey ) {
5673
return (
5774
( lang in dictionaries ) &&
@@ -67,3 +84,12 @@ function hasTranslation( lang, translationKey ) {
6784
export 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;

tests/translation-service.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
* For licensing, see LICENSE.md.
44
*/
55

6+
/* globals window */
7+
68
import { translate, add, _clear } from '../src/translation-service';
79

810
describe( 'translation-service', () => {
9-
beforeEach( () => {
11+
afterEach( () => {
1012
_clear();
1113
} );
1214

@@ -45,6 +47,17 @@ describe( 'translation-service', () => {
4547
expect( translation ).to.be.equal( 'Bold' );
4648
} );
4749

50+
it( 'should use provided language if only one is provided', () => {
51+
add( 'pl', {
52+
'OK': 'OK',
53+
'Cancel [context: reject]': 'Anuluj'
54+
} );
55+
56+
const translation = translate( 'de', 'Cancel [context: reject]' );
57+
58+
expect( translation ).to.be.equal( 'Anuluj' );
59+
} );
60+
4861
it( 'should be able to merge translations', () => {
4962
add( 'pl', {
5063
'OK': 'OK',
@@ -62,4 +75,9 @@ describe( 'translation-service', () => {
6275
expect( translationPL ).to.be.equal( 'Anuluj' );
6376
expect( translationEN ).to.be.equal( 'Cancel' );
6477
} );
78+
79+
it( 'should expose `add` function globally', () => {
80+
expect( window.CKEDITOR_TRANSLATIONS ).to.be.an( 'object' );
81+
expect( window.CKEDITOR_TRANSLATIONS.add ).to.be.a( 'function' );
82+
} );
6583
} );

0 commit comments

Comments
 (0)