Plato on Github
Report Home
service/default-interpolation.js
Maintainability
71.07
Lines of code
105
Difficulty
12.46
Estimated Errors
0.24
Function weight
By Complexity
By SLOC
/** * @ngdoc object * @name pascalprecht.translate.$translateDefaultInterpolation * @requires $interpolate * * @description * Uses angular's `$interpolate` services to interpolate strings against some values. * * Be aware to configure a proper sanitization strategy. * * See also: * * {@link pascalprecht.translate.$translateSanitization} * * @return {object} $translateDefaultInterpolation Interpolator service */ angular.module('pascalprecht.translate').factory('$translateDefaultInterpolation', $translateDefaultInterpolation); function $translateDefaultInterpolation ($interpolate, $translateSanitization) { 'use strict'; var $translateInterpolator = {}, $locale, $identifier = 'default'; /** * @ngdoc function * @name pascalprecht.translate.$translateDefaultInterpolation#setLocale * @methodOf pascalprecht.translate.$translateDefaultInterpolation * * @description * Sets current locale (this is currently not use in this interpolation). * * @param {string} locale Language key or locale. */ $translateInterpolator.setLocale = function (locale) { $locale = locale; }; /** * @ngdoc function * @name pascalprecht.translate.$translateDefaultInterpolation#getInterpolationIdentifier * @methodOf pascalprecht.translate.$translateDefaultInterpolation * * @description * Returns an identifier for this interpolation service. * * @returns {string} $identifier */ $translateInterpolator.getInterpolationIdentifier = function () { return $identifier; }; /** * @deprecated will be removed in 3.0 * @see {@link pascalprecht.translate.$translateSanitization} */ $translateInterpolator.useSanitizeValueStrategy = function (value) { $translateSanitization.useStrategy(value); return this; }; /** * @ngdoc function * @name pascalprecht.translate.$translateDefaultInterpolation#interpolate * @methodOf pascalprecht.translate.$translateDefaultInterpolation * * @description * Interpolates given value agains given interpolate params using angulars * `$interpolate` service. * * Since AngularJS 1.5, `value` must not be a string but can be anything input. * * @param {string} value translation * @param {object} [interpolationParams={}] interpolation params * @param {string} [context=undefined] current context (filter, directive, service) * @param {string} [sanitizeStrategy=undefined] sanitize strategy (use default unless set) * @param {string} translationId current translationId * * @returns {string} interpolated string */ $translateInterpolator.interpolate = function (value, interpolationParams, context, sanitizeStrategy, translationId) { // jshint ignore:line interpolationParams = interpolationParams || {}; interpolationParams = $translateSanitization.sanitize(interpolationParams, 'params', sanitizeStrategy, context); var interpolatedText; if (angular.isNumber(value)) { // numbers are safe interpolatedText = '' + value; } else if (angular.isString(value)) { // strings must be interpolated (that's the job here) interpolatedText = $interpolate(value)(interpolationParams); interpolatedText = $translateSanitization.sanitize(interpolatedText, 'text', sanitizeStrategy, context); } else { // neither a number or a string, cant interpolate => empty string interpolatedText = ''; } return interpolatedText; }; return $translateInterpolator; } $translateDefaultInterpolation.displayName = '$translateDefaultInterpolation';