8
0
Просмотр исходного кода

Made Locale accept an options object instead of separate arguments. Renamed #language and #languageDirection to #uiLanguage and #uiLanguageDirection.

Aleksander Nowodzinski 6 лет назад
Родитель
Сommit
39ca6b67a0
2 измененных файлов с 88 добавлено и 39 удалено
  1. 22 18
      packages/ckeditor5-utils/src/locale.js
  2. 66 21
      packages/ckeditor5-utils/tests/locale.js

+ 22 - 18
packages/ckeditor5-utils/src/locale.js

@@ -16,42 +16,46 @@ const RTL_LANGUAGE_CODES = [ 'ar', 'fa', 'he', 'ku', 'ug' ];
  */
 export default class Locale {
 	/**
-	 * Creates a new instance of the Locale class.
+	 * Creates a new instance of the Locale class. Learn more about
+	 * {@glink features/ui-language configuring language of the editor}.
 	 *
-	 * @param {String} [language='en'] The editor language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
-	 * @param {String} [contentLanguage] The editor content language code in the
-	 * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
+	 * @param {Object} [options] Locale configuration.
+	 * @param {String} [options.uiLanguage='en'] The editor UI language code in the
+	 * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. See {@link #uiLanguage}.
+	 * @param {String} [options.contentLanguage] The editor content language code in the
+	 * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
+	 * See {@link #contentLanguage}.
 	 */
-	constructor( language, contentLanguage ) {
+	constructor( options = {} ) {
 		/**
-		 * The editor language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
+		 * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
 		 *
-		 * It controls the language of the editor UI. If the {@link #contentLanguage content language}
-		 * was not specified in the `Locale` constructor, it also defines the language of the content.
+		 * If the {@link #contentLanguage content language} was not specified in the `Locale` constructor,
+		 * it also defines the language of the content.
 		 *
 		 * @readonly
 		 * @member {String}
 		 */
-		this.language = language || 'en';
+		this.uiLanguage = options.uiLanguage || 'en';
 
 		/**
 		 * The editor content language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
 		 *
-		 * Usually the same as {@link #language editor language}, it can be customized by passing an optional
+		 * Usually the same as {@link #uiLanguage editor language}, it can be customized by passing an optional
 		 * argument to the `Locale` constructor.
 		 *
 		 * @readonly
 		 * @member {String}
 		 */
-		this.contentLanguage = contentLanguage || this.language;
+		this.contentLanguage = options.contentLanguage || this.uiLanguage;
 
 		/**
-		 * Text direction of the {@link #language editor UI language}. Either `'ltr'` or `'rtl'`.
+		 * Text direction of the {@link #uiLanguage editor UI language}. Either `'ltr'` or `'rtl'`.
 		 *
 		 * @readonly
 		 * @member {String}
 		 */
-		this.languageDirection = getLanguageDirection( this.language );
+		this.uiLanguageDirection = getLanguageDirection( this.uiLanguage );
 
 		/**
 		 * Text direction of the {@link #contentLanguage editor content language}.
@@ -59,8 +63,8 @@ export default class Locale {
 		 * If the content language was passed directly to the `Locale` constructor, this property represents the
 		 * direction of that language.
 		 *
-		 * If the {@link #contentLanguage editor content language} was derived from the {@link #language editor language},
-		 * the content language direction is the same as the {@link #languageDirection UI language direction}.
+		 * If the {@link #contentLanguage editor content language} was derived from the {@link #uiLanguage editor language},
+		 * the content language direction is the same as the {@link #uiLanguageDirection UI language direction}.
 		 *
 		 * The value is either `'ltr'` or `'rtl'`.
 		 *
@@ -70,8 +74,8 @@ export default class Locale {
 		this.contentLanguageDirection = getLanguageDirection( this.contentLanguage );
 
 		/**
-		 * 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}.
+		 * Translates the given string to the {@link #uiLanguage}. 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.
@@ -97,7 +101,7 @@ export default class Locale {
 	 * @private
 	 */
 	_t( str, values ) {
-		let translatedString = translate( this.language, str );
+		let translatedString = translate( this.uiLanguage, str );
 
 		if ( values ) {
 			translatedString = translatedString.replace( /%(\d+)/g, ( match, index ) => {

+ 66 - 21
packages/ckeditor5-utils/tests/locale.js

@@ -14,52 +14,97 @@ describe( 'Locale', () => {
 
 	describe( 'constructor', () => {
 		it( 'sets the #language', () => {
-			const locale = new Locale( 'pl' );
+			const locale = new Locale( {
+				uiLanguage: 'pl'
+			} );
 
-			expect( locale ).to.have.property( 'language', 'pl' );
+			expect( locale ).to.have.property( 'uiLanguage', 'pl' );
 		} );
 
 		it( 'sets the #contentLanguage', () => {
-			const locale = new Locale( 'pl', 'en' );
+			const locale = new Locale( {
+				uiLanguage: 'pl',
+				contentLanguage: 'en'
+			} );
 
-			expect( locale ).to.have.property( 'language', 'pl' );
+			expect( locale ).to.have.property( 'uiLanguage', 'pl' );
 			expect( locale ).to.have.property( 'contentLanguage', 'en' );
 		} );
 
 		it( 'defaults #language to en', () => {
 			const locale = new Locale();
 
-			expect( locale ).to.have.property( 'language', 'en' );
+			expect( locale ).to.have.property( 'uiLanguage', 'en' );
 		} );
 
 		it( 'inherits the #contentLanguage from the #language (if not passed)', () => {
-			const locale = new Locale( 'pl' );
+			const locale = new Locale( {
+				uiLanguage: 'pl'
+			} );
 
-			expect( locale ).to.have.property( 'language', 'pl' );
+			expect( locale ).to.have.property( 'uiLanguage', 'pl' );
 			expect( locale ).to.have.property( 'contentLanguage', 'pl' );
 		} );
 
-		it( 'determines the #languageDirection', () => {
-			expect( new Locale( 'pl' ) ).to.have.property( 'languageDirection', 'ltr' );
-			expect( new Locale( 'en' ) ).to.have.property( 'languageDirection', 'ltr' );
+		it( 'determines the #uiLanguageDirection', () => {
+			expect( new Locale( {
+				uiLanguage: 'pl'
+			} ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
 
-			expect( new Locale( 'ar' ) ).to.have.property( 'languageDirection', 'rtl' );
-			expect( new Locale( 'fa' ) ).to.have.property( 'languageDirection', 'rtl' );
-			expect( new Locale( 'he' ) ).to.have.property( 'languageDirection', 'rtl' );
-			expect( new Locale( 'ku' ) ).to.have.property( 'languageDirection', 'rtl' );
-			expect( new Locale( 'ug' ) ).to.have.property( 'languageDirection', 'rtl' );
+			expect( new Locale( {
+				uiLanguage: 'en'
+			} ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
+
+			expect( new Locale( {
+				uiLanguage: 'ar'
+			} ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
+
+			expect( new Locale( {
+				uiLanguage: 'fa'
+			} ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
+
+			expect( new Locale( {
+				uiLanguage: 'he'
+			} ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
+
+			expect( new Locale( {
+				uiLanguage: 'ku'
+			} ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
+
+			expect( new Locale( {
+				uiLanguage: 'ug'
+			} ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
 		} );
 
 		it( 'determines the #contentLanguageDirection (not passed)', () => {
-			expect( new Locale( 'pl' ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
-			expect( new Locale( 'en' ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
-			expect( new Locale( 'ar' ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
+			expect( new Locale( {
+				uiLanguage: 'pl'
+			} ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
+
+			expect( new Locale( {
+				uiLanguage: 'en'
+			} ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
+
+			expect( new Locale( {
+				uiLanguage: 'ar'
+			} ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
 		} );
 
 		it( 'determines the #contentLanguageDirection (passed)', () => {
-			expect( new Locale( 'pl', 'pl' ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
-			expect( new Locale( 'en', 'ar' ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
-			expect( new Locale( 'ar', 'pl' ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
+			expect( new Locale( {
+				uiLanguage: 'pl',
+				contentLanguage: 'pl'
+			} ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
+
+			expect( new Locale( {
+				uiLanguage: 'en',
+				contentLanguage: 'ar'
+			} ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
+
+			expect( new Locale( {
+				uiLanguage: 'ar',
+				contentLanguage: 'pl'
+			} ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
 		} );
 	} );