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

Removed translation-utils's add() #232

Merged
merged 4 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 29 additions & 41 deletions src/translation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@

/**
* @module utils/translation-service
*/

let dictionaries = {};

/**
* Adds package translations to existing ones.
* These translations will later be available for {@link module:utils/translation-service~translate translate}.
*
* add( 'pl', {
* 'OK': 'OK',
* '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.
* Translation service provides {module:utils/translation-service.translate translate} method which can be used
* to translate phrase to the given language. Translation should be previously added directly to
* the window.CKEDITOR_TRANSLATIONS variable, safely extending current ones.
*
* <script src="./path/to/ckeditor.js"></script>
* <script src="./path/to/translations/en.js"></script>
* <script src="./path/to/translations/de.js"></script>
*
* @param {String} lang Target language.
* @param {Object.<String, String>} translations Translations which will be added to the dictionary.
* Example of the function that can add translations to the given language.
*
* function addTranslations( lang, translations ) {
* if ( !window.CKEDITOR_TRANSLATIONS ) {
* window.CKEDITOR_TRANSLATIONS = {};
* }
*
* const dictionary = window.CKEDITOR_TRANSLATIONS[ lang ] || ( window.CKEDITOR_TRANSLATIONS[ lang ] = {} );
*
* // Extend the dictionary for the given language.
* Object.assign( dictionary, translations );
* }
*/
export function add( lang, translations ) {
dictionaries[ lang ] = dictionaries[ lang ] || {};

Object.assign( dictionaries[ lang ], translations );
// Initialize CKEDITOR_TRANSLATIONS if it's not initialized.
if ( !window.CKEDITOR_TRANSLATIONS ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think that such an access to the global variable is unsafe in envs like Electron, but let's go with it for now. I'll only change it so it imports the global object from src/global.js.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe I won't... because it's src/dom/global.js. I'll report a followup.

window.CKEDITOR_TRANSLATIONS = {};
}

/**
* Translates string if the translation of the string was previously {@link module:utils/translation-service~add added}
* to the dictionary. This happens in a multi-language mode were translation modules are created by the bundler.
* Translates string if the translation of the string was previously added to the dictionary.
* See {@link module:utils/translation-service Translation Service}.
* This happens in a multi-language mode were translation modules are created by the bundler.
*
* 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).
Expand All @@ -57,39 +57,27 @@ 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 initialization.
lang = Object.keys( dictionaries )[ 0 ];
lang = Object.keys( window.CKEDITOR_TRANSLATIONS )[ 0 ];
}

if ( numberOfLanguages === 0 || !hasTranslation( lang, translationKey ) ) {
return translationKey.replace( / \[context: [^\]]+\]$/, '' );
}

const dictionary = window.CKEDITOR_TRANSLATIONS[ lang ];

// In case of missing translations we still need to cut off the `[context: ]` parts.
return dictionaries[ lang ][ translationKey ].replace( / \[context: [^\]]+\]$/, '' );
return dictionary[ translationKey ].replace( / \[context: [^\]]+\]$/, '' );
}

// Checks whether the dictionary exists and translation in that dictionary exists.
function hasTranslation( lang, translationKey ) {
return (
( lang in dictionaries ) &&
( translationKey in dictionaries[ lang ] )
( lang in window.CKEDITOR_TRANSLATIONS ) &&
( translationKey in window.CKEDITOR_TRANSLATIONS[ lang ] )
);
}

/**
* Clears dictionaries for test purposes.
*
* @protected
*/
export function _clear() {
dictionaries = {};
}

function getNumberOfLanguages() {
return Object.keys( dictionaries ).length;
return Object.keys( window.CKEDITOR_TRANSLATIONS ).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;
18 changes: 12 additions & 6 deletions tests/translation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

/* globals window */

import { translate, add, _clear } from '../src/translation-service';
import { translate } from '../src/translation-service';

describe( 'translation-service', () => {
afterEach( () => {
_clear();
window.CKEDITOR_TRANSLATIONS = {};
} );

it( 'should return english string if no translation exists', () => {
Expand Down Expand Up @@ -76,8 +76,14 @@ describe( 'translation-service', () => {
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' );
} );
function add( lang, translations ) {
if ( !window.CKEDITOR_TRANSLATIONS ) {
window.CKEDITOR_TRANSLATIONS = {};
}

const dictionary = window.CKEDITOR_TRANSLATIONS[ lang ] || ( window.CKEDITOR_TRANSLATIONS[ lang ] = {} );

// Extend the dictionary for the given language.
Object.assign( dictionary, translations );
}
} );