Przeglądaj źródła

Merge branch 'master' into t/201

Piotrek Koszuliński 8 lat temu
rodzic
commit
22d26d1062

+ 1 - 1
packages/ckeditor5-utils/package.json

@@ -11,7 +11,7 @@
     "@ckeditor/ckeditor5-engine": "^1.0.0-alpha.2",
     "del": "^2.2.0",
     "eslint": "^4.8.0",
-    "eslint-config-ckeditor5": "^1.0.6",
+    "eslint-config-ckeditor5": "^1.0.7",
     "husky": "^0.14.3",
     "lint-staged": "^4.2.3",
     "lodash-cli": "^4"

+ 6 - 6
packages/ckeditor5-utils/src/locale.js

@@ -16,20 +16,20 @@ export default class Locale {
 	/**
 	 * Creates a new instance of the Locale class.
 	 *
-	 * @param {String} [lang='en'] The language code in [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
+	 * @param {String} [language='en'] The language code in [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
 	 */
-	constructor( lang ) {
+	constructor( language ) {
 		/**
 		 * The language code in [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
 		 *
 		 * @readonly
 		 * @member {String}
 		 */
-		this.lang = lang || 'en';
+		this.language = language || 'en';
 
 		/**
-		 * Translates the given string to the {@link #lang}. This method is also availble in {@link module:core/editor/editor~Editor#t} and
-		 * {@link module:ui/view~View#t}.
+		 * Translates the given string to the {@link #language}. This method is also available in {@link module:core/editor/editor~Editor#t}
+		 * and {@link module:ui/view~View#t}.
 		 *
 		 * The strings may contain placeholders (`%<index>`) for values which are passed as the second argument.
 		 * `<index>` is the index in the `values` array.
@@ -55,7 +55,7 @@ export default class Locale {
 	 * @private
 	 */
 	_t( str, values ) {
-		let translatedString = translate( this.lang, str );
+		let translatedString = translate( this.language, str );
 
 		if ( values ) {
 			translatedString = translatedString.replace( /%(\d+)/g, ( match, index ) => {

+ 6 - 0
packages/ckeditor5-utils/src/observablemixin.js

@@ -216,6 +216,12 @@ const ObservableMixin = {
 
 			unbindAttrs.forEach( attrName => {
 				const binding = boundAttributes.get( attrName );
+
+				// Nothing to do if the binding is not defined
+				if ( !binding ) {
+					return;
+				}
+
 				let toObservable, toAttr, toAttrs, toAttrBindings;
 
 				binding.to.forEach( to => {

+ 31 - 5
packages/ckeditor5-utils/src/translation-service.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+/* globals window */
+
 /**
  * @module utils/translation-service
  */
@@ -18,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.
+ *
+ * 		<script src="./path/to/ckeditor.js"></script>
+ * 		<script src="./path/to/translations/en.js"></script>
+ *
  * @param {String} lang Target language.
  * @param {Object.<String, String>} translations Translations which will be added to the dictionary.
  */
@@ -34,24 +42,33 @@ 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]' );
  *
  * @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 ) {
-	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 initialization.
+		lang = Object.keys( dictionaries )[ 0 ];
+	}
+
+	if ( numberOfLanguages === 0 || !hasTranslation( 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 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 +84,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;

+ 5 - 2
packages/ckeditor5-utils/tests/dom/rect.js

@@ -435,7 +435,7 @@ describe( 'Rect', () => {
 			ancestorA = document.createElement( 'div' );
 			ancestorB = document.createElement( 'div' );
 
-			ancestorA.append( element );
+			ancestorA.appendChild( element );
 			document.body.appendChild( ancestorA );
 		} );
 
@@ -575,7 +575,7 @@ describe( 'Rect', () => {
 		} );
 
 		it( 'should return the visible rect (HTMLElement), partially cropped, deep ancestor overflow', () => {
-			ancestorB.append( ancestorA );
+			ancestorB.appendChild( ancestorA );
 			document.body.appendChild( ancestorB );
 
 			testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( {
@@ -909,6 +909,9 @@ describe( 'Rect', () => {
 					height: 230
 				} );
 
+				// Safari fails because of "afterEach()" hook tries to restore values from removed element.
+				// We need to restore these values manually.
+				testUtils.sinon.restore();
 				iframe.remove();
 				done();
 			} );

+ 3 - 0
packages/ckeditor5-utils/tests/dom/scroll.js

@@ -248,6 +248,9 @@ describe( 'scrollViewportToShowTarget()', () => {
 		} );
 
 		afterEach( () => {
+			// Safari fails because of "afterEach()" hook tries to restore values from removed element.
+			// We need to restore these values manually.
+			testUtils.sinon.restore();
 			iframeAncestor.remove();
 		} );
 

+ 4 - 4
packages/ckeditor5-utils/tests/locale.js

@@ -13,16 +13,16 @@ describe( 'Locale', () => {
 	} );
 
 	describe( 'constructor', () => {
-		it( 'sets the lang', () => {
+		it( 'sets the language', () => {
 			const locale = new Locale( 'pl' );
 
-			expect( locale ).to.have.property( 'lang', 'pl' );
+			expect( locale ).to.have.property( 'language', 'pl' );
 		} );
 
-		it( 'defaults lang to en', () => {
+		it( 'defaults language to en', () => {
 			const locale = new Locale();
 
-			expect( locale ).to.have.property( 'lang', 'en' );
+			expect( locale ).to.have.property( 'language', 'en' );
 		} );
 	} );
 

+ 8 - 0
packages/ckeditor5-utils/tests/observablemixin.js

@@ -739,6 +739,14 @@ describe( 'Observable', () => {
 			observable.unbind();
 		} );
 
+		it( 'should not fail when unbinding attribute that is not bound', () => {
+			const observable = new Observable();
+
+			observable.bind( 'foo' ).to( car, 'color' );
+
+			expect( () => observable.unbind( 'bar' ) ).to.not.throw();
+		} );
+
 		it( 'should throw when non-string attribute is passed', () => {
 			expect( () => {
 				car.unbind( new Date() );

+ 19 - 1
packages/ckeditor5-utils/tests/translation-service.js

@@ -3,10 +3,12 @@
  * For licensing, see LICENSE.md.
  */
 
+/* globals window */
+
 import { translate, add, _clear } from '../src/translation-service';
 
 describe( 'translation-service', () => {
-	beforeEach( () => {
+	afterEach( () => {
 		_clear();
 	} );
 
@@ -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,9 @@ 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' );
+	} );
 } );