angular-l10n
Version:
An Angular library to translate messages, dates and numbers
92 lines • 2.83 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
import { Injectable, Inject } from '@angular/core';
import { L10N_CONFIG } from "../models/l10n-config";
/**
* Implement this class-interface to create a custom handler for translated values.
* @abstract
*/
export class TranslationHandler {
}
TranslationHandler.decorators = [
{ type: Injectable }
];
if (false) {
/**
* This method must contain the logic to parse the translated value.
* @abstract
* @param {?} path The path of the key
* @param {?} key The key that has been requested
* @param {?} value The translated value
* @param {?} args The parameters passed along with the key
* @param {?} lang The current language
* @return {?} The parsed value
*/
TranslationHandler.prototype.parseValue = function (path, key, value, args, lang) { };
}
export class L10nTranslationHandler {
/**
* @param {?} configuration
*/
constructor(configuration) {
this.configuration = configuration;
}
/**
* @param {?} path
* @param {?} key
* @param {?} value
* @param {?} args
* @param {?} lang
* @return {?}
*/
parseValue(path, key, value, args, lang) {
if (value == null) {
return this.handleMissingValue(path);
}
else if (args) {
return this.handleArgs(value, args);
}
return value;
}
/**
* @param {?} path
* @return {?}
*/
handleMissingValue(path) {
if (this.configuration.translation.missingValue != null) {
return typeof this.configuration.translation.missingValue === "function"
? this.configuration.translation.missingValue(path)
: this.configuration.translation.missingValue;
}
// The same path is returned.
return path;
}
/**
* @param {?} value
* @param {?} args
* @return {?}
*/
handleArgs(value, args) {
/** @type {?} */
const TEMPLATE_REGEXP = /{{\s?([^{}\s]*)\s?}}/g;
return value.replace(TEMPLATE_REGEXP, (substring, parsedKey) => {
/** @type {?} */
const replacer = (/** @type {?} */ (args[parsedKey]));
return typeof replacer !== "undefined" ? replacer : substring;
});
}
}
L10nTranslationHandler.decorators = [
{ type: Injectable }
];
/** @nocollapse */
L10nTranslationHandler.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [L10N_CONFIG,] }] }
];
if (false) {
/** @type {?} */
L10nTranslationHandler.prototype.configuration;
}
//# sourceMappingURL=translation-handler.js.map