/**
* Copyright (c) 2016, Oracle and/or its affiliates.
* All rights reserved.
*/
define([], function () {
'use strict';
/**
* Constructs a new Translatable.
*
* @AbcsAPI stable
* @version 16.3.1
* @param {string} i18nKey - an i18n key that must uniquely identify the managed
* translatble string within ABCS
* @constructor
* @see {@link module:translations/js/api/I18n.key I18n.key()}
* @see {@link module:translations/js/api/I18n.createTranslatable I18n.createTranslatable()}
* @exports translations/js/api/Translatable
*/
var Translatable = function (i18nKey) {
AbcsLib.checkThis(this);
if (arguments.length > 0) {
AbcsLib.checkDefined(i18nKey);
AbcsLib.checkDataType(i18nKey, AbcsLib.Type.STRING);
AbcsLib.checkParameterCount(arguments, 1);
this.i18n = (i18nKey.length !== 0 ? i18nKey : Translatable.EMPTY_STRING);
} else { // no args is also treated as an empty-string translatable
this.i18n = Translatable.EMPTY_STRING;
}
};
/** @constant **/
Translatable.EMPTY_STRING = 'EMPTY_STRING';
/**
* Check if the translatable wraps an empty string.
*
* @version 16.3.1
* @returns if the current translatable is an empty string wrapper
*/
Translatable.prototype.isEmptyStringWrapper = function () {
return this.i18n === Translatable.EMPTY_STRING;
};
/**
* Binds to a value for the current key and returns it. If a translation
* key is missing, returns the empty string. Accepts an optional list of
* message parameters.
*
* @example <caption>Calling bind() without parameters</caption>
* // If the i18n key greeting.header.name stores the value 'My Hello World ABCS App!'
* Translatable translatable = new Translatable('greeting.header.name');
* var value = translatable.bind();
* // value is now 'My Hello World ABCS App!'
*
* @example <caption>Calling bind() with numbered parameters</caption>
* // If the i18n key greeting.header.name stores the value 'My {0} {1} ABCS App!'
* Translatable translatable = new Translatable('greeting.header.name');
* var value = translatable.bind('Hello', 'World');
* // value is now 'My Hello World ABCS App!'
*
* @example <caption>Calling bind() with named parameters</caption>
* // If the i18n key greeting.header.name stores the value 'My {greeting} {who} ABCS App!'
* Translatable translatable = new Translatable('greeting.header.name');
* var value = translatable.bind({greeting: 'Hello', who: 'World'});
* // value is now 'My Hello World ABCS App!'
*
* @AbcsAPI stable
* @version 16.3.1
* @param {...string|...Translatable} [varArgs=[]] - optional parameters to be inserted into the
* translated message
* @returns {string} the language-dependent value of this Translatable
*/
Translatable.prototype.bind = function () {
if (this.i18n !== Translatable.EMPTY_STRING) {
var args = this.__buildAbcsLib_i18nArgsArray(arguments);
return AbcsLib.i18n.apply(AbcsLib, args);
} else {
return '';
}
};
Translatable.prototype.__buildAbcsLib_i18nArgsArray = function (varArgObject) {
var args = [this.i18n];
if (varArgObject.length > 1) {
args = args.concat(Array.prototype.slice.call(varArgObject));
} else if (varArgObject.length === 1) {
var params = varArgObject[0];
if (typeof params !== 'object' && !(params instanceof Array)) {
params = [params];
}
args = args.concat(params);
}
return args;
};
/**
* Binds to the translated value for the current key. Overriding this method
* ensures that in String contexts, the translatable is rendered as a String.
*
* <p>
* In cases where there are no run-time paramaters, this method is no
* different that calling {@link module:translations/js/api/Translatable#bind Translatable.bind()} directly. If there
* are run-time parameters, {@link module:translations/js/api/Translatable#bind Translatable.bind()} must be used instead.
* </p>
*
* @function
* @AbcsAPI stable
* @version 16.3.1
* @returns {String} the translated value of this translatable
*/
Translatable.prototype.toString = Translatable.prototype.bind;
return Translatable;
});