|
@@ -86,21 +86,25 @@ export default class Locale {
|
|
|
*
|
|
*
|
|
|
* The message can be either a string or an object implementing the {@link module:utils/translation-service~Message} interface.
|
|
* The message can be either a string or an object implementing the {@link module:utils/translation-service~Message} interface.
|
|
|
*
|
|
*
|
|
|
- * The message may contain placeholders (`%<index>`) for values that are passed as the second argument.
|
|
|
|
|
- * `<index>` is the index in the `values` array.
|
|
|
|
|
|
|
+ * The message may contain placeholders (`%<index>`) for value(s) that are passed as the second argument.
|
|
|
|
|
+ * For an array of values the `<index>` will be the index in that array.
|
|
|
*
|
|
*
|
|
|
* t( 'Created file "%0" in %1ms.', [ fileName, timeTaken ] );
|
|
* t( 'Created file "%0" in %1ms.', [ fileName, timeTaken ] );
|
|
|
*
|
|
*
|
|
|
|
|
+ * For only one argument that should be interpolated there's a shorthand:
|
|
|
|
|
+ *
|
|
|
|
|
+ * t( 'Created file "%0", fileName );
|
|
|
|
|
+ *
|
|
|
* The message supports plural forms. To specify a plural form, use the `plural` property. Single or plural form
|
|
* The message supports plural forms. To specify a plural form, use the `plural` property. Single or plural form
|
|
|
* will be chosen depending on the first value from the passed `values`. The value of this property is used
|
|
* will be chosen depending on the first value from the passed `values`. The value of this property is used
|
|
|
- * as a default plural translation when the translation for the target language is missing.
|
|
|
|
|
|
|
+ * as a default plural translation when the translation for the target language is missing. Therefor, it should be a number.
|
|
|
*
|
|
*
|
|
|
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, [ 1 ] ); // 'Add a space' for the English language.
|
|
|
|
|
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, [ 5 ] ); // 'Add 5 spaces' for the English language.
|
|
|
|
|
|
|
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Add a space' for the English language.
|
|
|
|
|
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 5 ); // 'Add 5 spaces' for the English language.
|
|
|
* t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Add 2 spaces' for the English language.
|
|
* t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Add 2 spaces' for the English language.
|
|
|
*
|
|
*
|
|
|
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, [ 1 ] ); // 'Dodaj spację' for the Polish language.
|
|
|
|
|
- * t( { string: 'Add a space', plural: 'Add %0 spaces' }, [ 5 ] ); // 'Dodaj 5 spacji' for the Polish language.
|
|
|
|
|
|
|
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Dodaj spację' for the Polish language.
|
|
|
|
|
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 5 ); // 'Dodaj 5 spacji' for the Polish language.
|
|
|
* t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Dodaj 2 spacje' for the Polish language.
|
|
* t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Dodaj 2 spacje' for the Polish language.
|
|
|
*
|
|
*
|
|
|
* The message can provide a context using the `context` property when the message ids created from message strings
|
|
* The message can provide a context using the `context` property when the message ids created from message strings
|
|
@@ -112,7 +116,7 @@ export default class Locale {
|
|
|
*
|
|
*
|
|
|
* @method #t
|
|
* @method #t
|
|
|
* @param {String|module:utils/translation-service~Message} message A message that will be localized (translated).
|
|
* @param {String|module:utils/translation-service~Message} message A message that will be localized (translated).
|
|
|
- * @param {Array.<String>} [values] An array of values that will fill message placeholders.
|
|
|
|
|
|
|
+ * @param {String|Number|Array.<String|Number>} [values] A value or an array of values that will fill message placeholders.
|
|
|
* For messages supporting plural forms the first value will determine the plural form.
|
|
* For messages supporting plural forms the first value will determine the plural form.
|
|
|
* @returns {String}
|
|
* @returns {String}
|
|
|
*/
|
|
*/
|
|
@@ -148,10 +152,14 @@ export default class Locale {
|
|
|
*
|
|
*
|
|
|
* @private
|
|
* @private
|
|
|
* @param {String|module:utils/translation-service~Message} message
|
|
* @param {String|module:utils/translation-service~Message} message
|
|
|
- * @param {Array.<String>} [values]
|
|
|
|
|
|
|
+ * @param {Number|String|Array.<Number|String>} [values]
|
|
|
* @returns {String}
|
|
* @returns {String}
|
|
|
*/
|
|
*/
|
|
|
_t( message, values = [] ) {
|
|
_t( message, values = [] ) {
|
|
|
|
|
+ if ( !Array.isArray( values ) ) {
|
|
|
|
|
+ values = [ values ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if ( typeof message === 'string' ) {
|
|
if ( typeof message === 'string' ) {
|
|
|
message = { string: message };
|
|
message = { string: message };
|
|
|
}
|
|
}
|
|
@@ -161,13 +169,17 @@ export default class Locale {
|
|
|
|
|
|
|
|
const translatedString = _translate( this.uiLanguage, message, amount );
|
|
const translatedString = _translate( this.uiLanguage, message, amount );
|
|
|
|
|
|
|
|
- return translatedString
|
|
|
|
|
- .replace( /%(\d+)/g, ( match, index ) => {
|
|
|
|
|
- return ( index < values.length ) ? values[ index ] : match;
|
|
|
|
|
- } );
|
|
|
|
|
|
|
+ return interpolateString( translatedString, values );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Fills the `%0, %1, ...` string placeholders with values.
|
|
|
|
|
+function interpolateString( string, values ) {
|
|
|
|
|
+ return string.replace( /%(\d+)/g, ( match, index ) => {
|
|
|
|
|
+ return ( index < values.length ) ? values[ index ] : match;
|
|
|
|
|
+ } );
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Helps determine whether a language is LTR or RTL.
|
|
// Helps determine whether a language is LTR or RTL.
|
|
|
//
|
|
//
|
|
|
// @param {String} language The ISO 639-1 language code.
|
|
// @param {String} language The ISO 639-1 language code.
|