SmartIT_Extensions/BMC/smart-it-full/scripts/app/i18n/i18n-service.js

114 lines
5.4 KiB
JavaScript

"use strict";
(function () {
'use strict';
/*
* An AngularJS Localization Service
*
* Written by Jim Lavin
* http://codingsmackdown.tv
*
*/
angular.module('i18nModule')
// localization service responsible for retrieving resource files from the server and
// managing the translation dictionary
.factory('i18nService', ['$http', '$rootScope', '$window', function ($http, $rootScope, $window) {
var i18nService = {
language: window.myitsmLocale,
// array to hold the localized resource string entries
dictionary: [],
// location of the resource file
url: undefined,
// flag to indicate if the service hs loaded the resource file
resourceFileLoaded: false,
// success handler for all server communication
successCallback: function (data) {
// store the returned array in the dictionary
i18nService.dictionary = data;
// set the flag that the resource are loaded
i18nService.resourceFileLoaded = true;
// broadcast that the file has been loaded
$rootScope.$broadcast('i18nResourcesUpdated');
},
// allows setting of language on the fly
setLanguage: function (value) {
i18nService.language = value;
i18nService.initLocalizedResources();
},
// allows setting of resource url on the fly
setUrl: function (value) {
i18nService.url = value;
i18nService.initLocalizedResources();
},
// builds the url for locating the resource file
buildUrl: function () {
if (!i18nService.language) {
var lang, androidLang;
// works for earlier version of Android (2.3.x)
if ($window.navigator && $window.navigator.userAgent && (androidLang = $window.navigator.userAgent.match(/android.*\W(\w\w)-(\w\w)\W/i))) {
lang = androidLang[1];
}
else {
// works for iOS, Android 4.x and other devices
lang = $window.navigator.userLanguage || $window.navigator.language;
}
// set language
i18nService.language = lang;
}
return 'scripts/app/i18n/resources-locale_' + i18nService.language + '.json';
},
// loads the language resource file from the server
initLocalizedResources: function () {
if (window.appLocalilzation) {
i18nService.successCallback(window.appLocalilzation);
return;
}
// build the url to retrieve the localized resource file
var url = i18nService.url || i18nService.buildUrl();
// request the resource file
$http({
method: 'GET',
url: url,
cache: false
}).success(i18nService.successCallback).error(function () {
// the request failed set the url to the default resource file
var url = 'scripts/app/i18n/resources-locale_en.json';
// request the default resource file
$http({ method: 'GET', url: url, cache: false }).success(i18nService.successCallback);
});
},
// checks the dictionary for a localized resource string
getLocalizedString: function (value) {
// default the result to dictionary key name
var result = value;
// make sure the dictionary has valid data
if (typeof i18nService.dictionary !== 'undefined') {
result = i18nService.dictionary[value] || value;
}
// return the value to the call
return result;
},
// gets the localized string and replace parameters
getLocalizedStringwithParams: function (input, params) {
if (!angular.isArray(params)) {
params = [params];
}
var result = i18nService.getLocalizedString(input);
if (params) {
for (var i = 0, l = params.length; i < l; i++) {
result = result.replace('{' + i + '}', params[i]);
}
}
return result;
},
// calculates number of bytes for a string
getByteLength: function (text) {
return encodeURIComponent(text).replace(/%[A-F\d]{2}/g, 'U').length;
}
};
// force the load of the resource file
i18nService.initLocalizedResources();
// return the local instance when called
return i18nService;
}]);
}());