├── README.md ├── angular-translate-interpolation-messageformat.js ├── angular-translate-interpolation-messageformat.min.js ├── bower.json └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # angular-translate-interpolation-messageformat (bower shadow repository) 2 | 3 | This is the _Bower shadow_ repository for *angular-translate-interpolation-messageformat*. 4 | 5 | ## Bugs and issues 6 | 7 | Please file any issues and bugs in our main repository at [angular-translate/angular-translate](https://github.com/angular-translate/angular-translate/issues). 8 | 9 | ## Usage 10 | 11 | ### via Bower 12 | 13 | ```bash 14 | $ bower install angular-translate-interpolation-messageformat 15 | ``` 16 | 17 | ### via NPM 18 | 19 | ```bash 20 | $ npm install angular-translate-interpolation-messageformat 21 | ``` 22 | 23 | ### via cdnjs 24 | 25 | Please have a look at https://cdnjs.com/libraries/angular-translate-interpolation-messageformat for specific versions. 26 | 27 | ## License 28 | 29 | Licensed under MIT. See more details at [angular-translate/angular-translate](https://github.com/angular-translate/angular-translate). 30 | -------------------------------------------------------------------------------- /angular-translate-interpolation-messageformat.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * angular-translate - v2.19.1 - 2024-01-21 3 | * 4 | * Copyright (c) 2024 The angular-translate team, Pascal Precht; Licensed MIT 5 | */ 6 | (function (root, factory) { 7 | if (typeof define === 'function' && define.amd) { 8 | // AMD. Register as an anonymous module unless amdModuleId is set 9 | define(["messageformat"], function (a0) { 10 | return (factory(a0)); 11 | }); 12 | } else if (typeof module === 'object' && module.exports) { 13 | // Node. Does not work with strict CommonJS, but 14 | // only CommonJS-like environments that support module.exports, 15 | // like Node. 16 | module.exports = factory(require("messageformat")); 17 | } else { 18 | factory(root["MessageFormat"]); 19 | } 20 | }(this, function (MessageFormat) { 21 | 22 | angular.module('pascalprecht.translate') 23 | 24 | /** 25 | * @ngdoc property 26 | * @name pascalprecht.translate.TRANSLATE_MF_INTERPOLATION_CACHE 27 | * @requires TRANSLATE_MF_INTERPOLATION_CACHE 28 | * 29 | * @description 30 | * Uses MessageFormat.js to interpolate strings against some values. 31 | */ 32 | .constant('TRANSLATE_MF_INTERPOLATION_CACHE', '$translateMessageFormatInterpolation') 33 | 34 | /** 35 | * @ngdoc object 36 | * @name pascalprecht.translate.$translateMessageFormatInterpolationProvider 37 | * 38 | * @description 39 | * Configurations for $translateMessageFormatInterpolation 40 | */ 41 | .provider('$translateMessageFormatInterpolation', $translateMessageFormatInterpolationProvider); 42 | 43 | function $translateMessageFormatInterpolationProvider() { 44 | 45 | 'use strict'; 46 | 47 | var configurer; 48 | 49 | /** 50 | * @ngdoc function 51 | * @name pascalprecht.translate.$translateMessageFormatInterpolationProvider#messageFormatConfigurer 52 | * @methodOf pascalprecht.translate.$translateMessageFormatInterpolationProvider 53 | * 54 | * @description 55 | * Defines an optional configurer for the MessageFormat instance. 56 | * 57 | * Note: This hook will be called whenever a new instance of MessageFormat will be created. 58 | * 59 | * @param {function} fn callback with the instance as argument 60 | */ 61 | this.messageFormatConfigurer = function (fn) { 62 | configurer = fn; 63 | }; 64 | 65 | /** 66 | * @ngdoc object 67 | * @name pascalprecht.translate.$translateMessageFormatInterpolation 68 | * @requires pascalprecht.translate.TRANSLATE_MF_INTERPOLATION_CACHE 69 | * 70 | * @description 71 | * Uses MessageFormat.js to interpolate strings against some values. 72 | * 73 | * Be aware to configure a proper sanitization strategy. 74 | * 75 | * See also: 76 | * * {@link pascalprecht.translate.$translateSanitization} 77 | * * {@link https://github.com/SlexAxton/messageformat.js} 78 | * 79 | * @return {object} $translateMessageFormatInterpolation Interpolator service 80 | */ 81 | this.$get = ['$translateSanitization', '$cacheFactory', 'TRANSLATE_MF_INTERPOLATION_CACHE', function ($translateSanitization, $cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE) { 82 | return $translateMessageFormatInterpolation($translateSanitization, $cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE, configurer); 83 | }]; 84 | 85 | } 86 | 87 | function $translateMessageFormatInterpolation($translateSanitization, $cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE, messageFormatConfigurer) { 88 | 89 | 'use strict'; 90 | 91 | var $translateInterpolator = {}, 92 | $cache = $cacheFactory.get(TRANSLATE_MF_INTERPOLATION_CACHE), 93 | // instantiate with default locale (which is 'en') 94 | $mf = new MessageFormat('en'), 95 | $identifier = 'messageformat'; 96 | 97 | if (angular.isFunction(messageFormatConfigurer)) { 98 | messageFormatConfigurer($mf); 99 | } 100 | 101 | if (!$cache) { 102 | // create cache if it doesn't exist already 103 | $cache = $cacheFactory(TRANSLATE_MF_INTERPOLATION_CACHE); 104 | } 105 | 106 | $cache.put('en', $mf); 107 | 108 | /** 109 | * @ngdoc function 110 | * @name pascalprecht.translate.$translateMessageFormatInterpolation#setLocale 111 | * @methodOf pascalprecht.translate.$translateMessageFormatInterpolation 112 | * 113 | * @description 114 | * Sets current locale (this is currently not use in this interpolation). 115 | * 116 | * @param {string} locale Language key or locale. 117 | */ 118 | $translateInterpolator.setLocale = function (locale) { 119 | $mf = $cache.get(locale); 120 | if (!$mf) { 121 | $mf = new MessageFormat(locale); 122 | if (angular.isFunction(messageFormatConfigurer)) { 123 | messageFormatConfigurer($mf); 124 | } 125 | $cache.put(locale, $mf); 126 | } 127 | }; 128 | 129 | /** 130 | * @ngdoc function 131 | * @name pascalprecht.translate.$translateMessageFormatInterpolation#getInterpolationIdentifier 132 | * @methodOf pascalprecht.translate.$translateMessageFormatInterpolation 133 | * 134 | * @description 135 | * Returns an identifier for this interpolation service. 136 | * 137 | * @returns {string} $identifier 138 | */ 139 | $translateInterpolator.getInterpolationIdentifier = function () { 140 | return $identifier; 141 | }; 142 | 143 | /** 144 | * @deprecated will be removed in 3.0 145 | * @see {@link pascalprecht.translate.$translateSanitization} 146 | */ 147 | $translateInterpolator.useSanitizeValueStrategy = function (value) { 148 | $translateSanitization.useStrategy(value); 149 | return this; 150 | }; 151 | 152 | /** 153 | * @ngdoc function 154 | * @name pascalprecht.translate.$translateMessageFormatInterpolation#interpolate 155 | * @methodOf pascalprecht.translate.$translateMessageFormatInterpolation 156 | * 157 | * @description 158 | * Interpolates given string against given interpolate params using MessageFormat.js. 159 | * 160 | * @returns {string} interpolated string. 161 | */ 162 | $translateInterpolator.interpolate = function (string, interpolationParams, context, sanitizeStrategy) { 163 | interpolationParams = interpolationParams || {}; 164 | interpolationParams = $translateSanitization.sanitize(interpolationParams, 'params', sanitizeStrategy); 165 | 166 | var compiledFunction = $cache.get('mf:' + string); 167 | 168 | // if given string wasn't compiled yet, we do so now and never have to do it again 169 | if (!compiledFunction) { 170 | 171 | // Ensure explicit type if possible 172 | // MessageFormat checks the actual type (i.e. for amount based conditions) 173 | for (var key in interpolationParams) { 174 | if (interpolationParams.hasOwnProperty(key)) { 175 | // ensure number 176 | var number = parseInt(interpolationParams[key], 10); 177 | if (angular.isNumber(number) && ('' + number) === interpolationParams[key]) { 178 | interpolationParams[key] = number; 179 | } 180 | } 181 | } 182 | 183 | compiledFunction = $mf.compile(string); 184 | $cache.put('mf:' + string, compiledFunction); 185 | } 186 | 187 | var interpolatedText = compiledFunction(interpolationParams); 188 | return $translateSanitization.sanitize(interpolatedText, 'text', sanitizeStrategy); 189 | }; 190 | 191 | return $translateInterpolator; 192 | } 193 | 194 | $translateMessageFormatInterpolation.displayName = '$translateMessageFormatInterpolation'; 195 | return 'pascalprecht.translate'; 196 | 197 | })); 198 | -------------------------------------------------------------------------------- /angular-translate-interpolation-messageformat.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * angular-translate - v2.19.1 - 2024-01-21 3 | * 4 | * Copyright (c) 2024 The angular-translate team, Pascal Precht; Licensed MIT 5 | */ 6 | !function(t,e){"function"==typeof define&&define.amd?define(["messageformat"],function(t){return e(t)}):"object"==typeof module&&module.exports?module.exports=e(require("messageformat")):e(t.MessageFormat)}(this,function(r){function i(u,t,e,n){"use strict";var a={},c=t.get(e),f=new r("en");return angular.isFunction(n)&&n(f),c||(c=t(e)),c.put("en",f),a.setLocale=function(t){(f=c.get(t))||(f=new r(t),angular.isFunction(n)&&n(f),c.put(t,f))},a.getInterpolationIdentifier=function(){return"messageformat"},a.useSanitizeValueStrategy=function(t){return u.useStrategy(t),this},a.interpolate=function(t,e,n,a){e=e||{},e=u.sanitize(e,"params",a);var r=c.get("mf:"+t);if(!r){for(var i in e)if(e.hasOwnProperty(i)){var o=parseInt(e[i],10);angular.isNumber(o)&&""+o===e[i]&&(e[i]=o)}r=f.compile(t),c.put("mf:"+t,r)}var s=r(e);return u.sanitize(s,"text",a)},a}return angular.module("pascalprecht.translate").constant("TRANSLATE_MF_INTERPOLATION_CACHE","$translateMessageFormatInterpolation").provider("$translateMessageFormatInterpolation",function(){"use strict";var a;this.messageFormatConfigurer=function(t){a=t},this.$get=["$translateSanitization","$cacheFactory","TRANSLATE_MF_INTERPOLATION_CACHE",function(t,e,n){return i(t,e,n,a)}]}),i.displayName="$translateMessageFormatInterpolation","pascalprecht.translate"}); -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-translate-interpolation-messageformat", 3 | "description": "A plugin for Angular Translate", 4 | "version": "2.19.1", 5 | "main": "./angular-translate-interpolation-messageformat.js", 6 | "ignore": [], 7 | "author": "Pascal Precht", 8 | "license": "MIT", 9 | "dependencies": { 10 | "angular-translate": "~2.19.1", 11 | "messageformat": "~1.0.2" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-translate-interpolation-messageformat", 3 | "version": "2.19.1", 4 | "description": "Uses MessageFormat.js to interpolate strings against some values.", 5 | "main": "angular-translate-interpolation-messageformat.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/angular-translate/bower-angular-translate-interpolation-messageformat.git" 9 | }, 10 | "keywords": [ 11 | "angular", 12 | "translate", 13 | "messageformat" 14 | ], 15 | "author": "Pascal Precht", 16 | "license": "MIT", 17 | "bugs": { 18 | "url": "https://github.com/angular-translate/angular-translate/issues" 19 | }, 20 | "homepage": "https://angular-translate.github.io", 21 | "dependencies": { 22 | "angular-translate": "~2.19.1", 23 | "messageformat": "~1.0.2" 24 | } 25 | } 26 | --------------------------------------------------------------------------------