This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
default-interpolation.js
105 lines (94 loc) · 3.51 KB
/
default-interpolation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @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';