GUAC-605: Use MessageFormat interpolation for angular-translate.

This commit is contained in:
Michael Jumper
2014-11-28 22:25:42 -08:00
parent dfcbd80b35
commit 1d86419cc1
4 changed files with 1664 additions and 0 deletions

View File

@@ -135,9 +135,11 @@
<jsSourceFile>lib/plugins/angular-route.js</jsSourceFile> <jsSourceFile>lib/plugins/angular-route.js</jsSourceFile>
<jsSourceFile>lib/plugins/angular-translate.js</jsSourceFile> <jsSourceFile>lib/plugins/angular-translate.js</jsSourceFile>
<jsSourceFile>lib/plugins/angular-translate-loader-static-files.js</jsSourceFile> <jsSourceFile>lib/plugins/angular-translate-loader-static-files.js</jsSourceFile>
<jsSourceFile>lib/plugins/angular-translate-interpolation-messageformat.js</jsSourceFile>
<jsSourceFile>lib/plugins/modal.min.js</jsSourceFile> <jsSourceFile>lib/plugins/modal.min.js</jsSourceFile>
<jsSourceFile>lib/blob/blob.js</jsSourceFile> <jsSourceFile>lib/blob/blob.js</jsSourceFile>
<jsSourceFile>lib/filesaver/filesaver.js</jsSourceFile> <jsSourceFile>lib/filesaver/filesaver.js</jsSourceFile>
<jsSourceFile>lib/messageformat/messageformat.js</jsSourceFile>
<jsSourceFile>license.txt</jsSourceFile> <jsSourceFile>license.txt</jsSourceFile>
<jsSourceFile>guacamole-common-js/all.js</jsSourceFile> <jsSourceFile>guacamole-common-js/all.js</jsSourceFile>
<jsSourceFile>scripts/session.js</jsSourceFile> <jsSourceFile>scripts/session.js</jsSourceFile>

View File

@@ -24,10 +24,17 @@
* The configuration block for setting up everything having to do with i18n. * The configuration block for setting up everything having to do with i18n.
*/ */
angular.module('index').config(['$translateProvider', function($translateProvider) { angular.module('index').config(['$translateProvider', function($translateProvider) {
// Use US English by default
$translateProvider.preferredLanguage('en_US'); $translateProvider.preferredLanguage('en_US');
// Load translations from static JSON files
$translateProvider.useStaticFilesLoader({ $translateProvider.useStaticFilesLoader({
prefix: 'translations/', prefix: 'translations/',
suffix: '.json' suffix: '.json'
}); });
// Provide pluralization, etc. via messageformat.js
$translateProvider.useMessageFormatInterpolation();
}]); }]);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,62 @@
/*!
* angular-translate - v2.2.0 - 2014-06-03
* http://github.com/PascalPrecht/angular-translate
* Copyright (c) 2014 ; Licensed MIT
*/
angular.module('pascalprecht.translate').constant('TRANSLATE_MF_INTERPOLATION_CACHE', '$translateMessageFormatInterpolation').factory('$translateMessageFormatInterpolation', [
'$cacheFactory',
'TRANSLATE_MF_INTERPOLATION_CACHE',
function ($cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE) {
var $translateInterpolator = {}, $cache = $cacheFactory.get(TRANSLATE_MF_INTERPOLATION_CACHE), $mf = new MessageFormat(), $identifier = 'messageformat', $sanitizeValueStrategy = null, sanitizeValueStrategies = {
escaped: function (params) {
var result = {};
for (var key in params) {
if (params.hasOwnProperty(key)) {
result[key] = angular.element('<div></div>').text(params[key]).html();
}
}
return result;
}
};
var sanitizeParams = function (params) {
var result;
if (angular.isFunction(sanitizeValueStrategies[$sanitizeValueStrategy])) {
result = sanitizeValueStrategies[$sanitizeValueStrategy](params);
} else {
result = params;
}
return result;
};
if (!$cache) {
$cache = $cacheFactory(TRANSLATE_MF_INTERPOLATION_CACHE);
}
$cache.put('en', $mf);
$translateInterpolator.setLocale = function (locale) {
$mf = $cache.get(locale);
if (!$mf) {
$mf = new MessageFormat(locale);
$cache.put(locale, $mf);
}
};
$translateInterpolator.getInterpolationIdentifier = function () {
return $identifier;
};
$translateInterpolator.useSanitizeValueStrategy = function (value) {
$sanitizeValueStrategy = value;
return this;
};
$translateInterpolator.interpolate = function (string, interpolateParams) {
interpolateParams = interpolateParams || {};
if ($sanitizeValueStrategy) {
interpolateParams = sanitizeParams(interpolateParams);
}
var interpolatedText = $cache.get(string + angular.toJson(interpolateParams));
if (!interpolatedText) {
interpolatedText = $mf.compile(string)(interpolateParams);
$cache.put(string + angular.toJson(interpolateParams), interpolatedText);
}
return interpolatedText;
};
return $translateInterpolator;
}
]);