Browse Source

WIP - Adding support for passing object as a first argument for `t()` calls.

Maciej Bukowski 5 years ago
parent
commit
3e519c15ca

+ 13 - 4
packages/ckeditor5-utils/src/locale.js

@@ -91,10 +91,10 @@ export default class Locale {
 		 *		t( 'Label' );
 		 *
 		 * @method #t
-		 * @param {String} str The string to translate.
+		 * @param {String} message The string to translate.
 		 * @param {String[]} [values] Values that should be used to interpolate the string.
 		 */
-		this.t = ( ...args ) => this._t( ...args );
+		this.t = ( message, values ) => this._t( message, values );
 	}
 
 	/**
@@ -124,10 +124,19 @@ export default class Locale {
 	/**
 	 * Base for the {@link #t} method.
 	 *
+	 * @param {Object|String} message
+	 * @param {String[]} [values]
 	 * @private
 	 */
-	_t( str, values ) {
-		let translatedString = translate( this.uiLanguage, str );
+	_t( message, values = [] ) {
+		if ( typeof message === 'string' ) {
+			message = { string: message };
+		}
+
+		const hasPluralForm = !!message.plural;
+		const amount = hasPluralForm ? values[ 0 ] : 1;
+
+		let translatedString = translate( this.uiLanguage, message, amount );
 
 		if ( values ) {
 			translatedString = translatedString.replace( /%(\d+)/g, ( match, index ) => {

+ 32 - 13
packages/ckeditor5-utils/src/translation-service.js

@@ -40,11 +40,15 @@ if ( !window.CKEDITOR_TRANSLATIONS ) {
  *
  * @param {String} language Target language.
  * @param {Object.<String, String>} translations Translations which will be added to the dictionary.
+ * @param {Function} getFormIndex
  */
-export function add( language, translations ) {
-	const dictionary = window.CKEDITOR_TRANSLATIONS[ language ] || ( window.CKEDITOR_TRANSLATIONS[ language ] = {} );
+export function add( language, translations, getFormIndex ) {
+	const languageTranslations = window.CKEDITOR_TRANSLATIONS[ language ] || ( window.CKEDITOR_TRANSLATIONS[ language ] = {} );
 
-	Object.assign( dictionary, translations );
+	languageTranslations.dictionary = languageTranslations.dictionary || {};
+	languageTranslations.getFormIndex = getFormIndex || languageTranslations.getFormIndex || ( () => 0 );
+
+	Object.assign( languageTranslations.dictionary, translations );
 }
 
 /**
@@ -61,10 +65,11 @@ export function add( language, translations ) {
  *		translate( 'pl', 'Cancel [context: reject]' );
  *
  * @param {String} language Target language.
- * @param {String} translationKey String that will be translated.
+ * @param {Object} message A message that will be translated.
+ * @param {Number} amount
  * @returns {String} Translated sentence.
  */
-export function translate( language, translationKey ) {
+export function translate( language, message, amount ) {
 	const numberOfLanguages = getNumberOfLanguages();
 
 	if ( numberOfLanguages === 1 ) {
@@ -73,14 +78,28 @@ export function translate( language, translationKey ) {
 		language = Object.keys( window.CKEDITOR_TRANSLATIONS )[ 0 ];
 	}
 
-	if ( numberOfLanguages === 0 || !hasTranslation( language, translationKey ) ) {
-		return translationKey.replace( / \[context: [^\]]+\]$/, '' );
+	const messageId = message.id || message.string;
+
+	if ( numberOfLanguages === 0 || !hasTranslation( language, messageId ) ) {
+		// return english forms:
+		if ( amount !== 1 ) {
+			return message.plural;
+		}
+
+		return message.string;
 	}
 
-	const dictionary = window.CKEDITOR_TRANSLATIONS[ language ];
+	const dictionary = window.CKEDITOR_TRANSLATIONS[ language ].dictionary;
+	const getFormIndex = window.CKEDITOR_TRANSLATIONS[ language ].getFormIndex;
+
+	// TODO - maybe a warning could be helpful for some mismatches.
+
+	if ( typeof dictionary[ messageId ] === 'string' ) {
+		return dictionary[ messageId ];
+	}
 
-	// In case of missing translations we still need to cut off the `[context: ]` parts.
-	return dictionary[ translationKey ].replace( / \[context: [^\]]+\]$/, '' );
+	// Note: The `translate` function is not responsible for replacing `%0, %1, ...` with values.
+	return dictionary[ messageId ][ getFormIndex( amount ) ];
 }
 
 /**
@@ -93,10 +112,10 @@ export function _clear() {
 }
 
 // Checks whether the dictionary exists and translation in that dictionary exists.
-function hasTranslation( language, translationKey ) {
+function hasTranslation( language, messageId ) {
 	return (
-		( language in window.CKEDITOR_TRANSLATIONS ) &&
-		( translationKey in window.CKEDITOR_TRANSLATIONS[ language ] )
+		window.CKEDITOR_TRANSLATIONS[ language ] &&
+		window.CKEDITOR_TRANSLATIONS[ language ].dictionary[ messageId ]
 	);
 }