Browse Source

i18n service improved

Supported basic markdown for translations
Improved insertParams function
master
Igor Zhukov 10 years ago committed by Roman Anasal
parent
commit
452bcc9834
  1. 78
      app/js/i18n.js

78
app/js/i18n.js

@ -4,41 +4,72 @@ angular.module('myApp.i18n', ['izhukov.utils'])
.factory('_', ['$http', '$route', 'Storage', '$locale', function($http, $route, Storage, $locale) { .factory('_', ['$http', '$route', 'Storage', '$locale', function($http, $route, Storage, $locale) {
var locale = 'en-us'; var locale = 'en-us';
var messages = {}; var messages = {};
var fallback_messages = {}; var fallbackMessages = {};
var supported = { var supported = {
'en-us': 'English' 'en-us': 'English'
}; };
var aliases = { var aliases = {
'en': 'en-us' 'en': 'en-us'
}; };
var paramRegEx = /\{\s*([a-zA-Z\d\--]+)(?:\s*:\s*(.*?))?\s*\}/g;
function insertParams(msgstr, params) { function insertParams(msgstr, params) {
for (var i in params) { return msgstr.replace(paramRegEx, function ($0, paramKey, nestedMsgStr) {
if (params.hasOwnProperty(i)){ var param = params[paramKey];
var param = params[i]; if (param === undefined) {
var regex = new RegExp('\{ *' + i + '(?: *: *(.*))? *\}'); console.warn('[i18n] missing param ' + paramKey + ' for message "' + msgstr + '"');
var match = regex.exec(msgstr); return '';
if (match) {
if (match[1] != undefined) {
param = insertParams(param, match[1].split('|'));
}
msgstr = msgstr.replace(match[0], param.toString().trim());
}
} }
} if (nestedMsgStr !== undefined) {
param = insertParams(param, nestedMsgStr.split('|'));
}
return param.toString().trim();
});
}
function encodeEntities(value) {
return value.
replace(/&/g, '&').
replace(/([^\#-~| |!])/g, function (value) { // non-alphanumeric
return '&#' + value.charCodeAt(0) + ';';
}).
replace(/</g, '&lt;').
replace(/>/g, '&gt;');
}
function parseMarkdownString(msgstr, msgid) {
msgstr = msgstr.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>")
.replace(/\n/g, "<br/>");
return msgstr; return msgstr;
} }
function _(msgid, params) { function _(msgid, params) {
var raw = false;
var msgstr = msgid; var msgstr = msgid;
if (msgid.substr(-4) === '_raw') {
raw = true;
msgid = msgid.substr(0, msgid.length - 4);
}
if (messages.hasOwnProperty(msgid)) { if (messages.hasOwnProperty(msgid)) {
msgstr = messages[msgid]; msgstr = messages[msgid];
} else if (fallback_messages.hasOwnProperty(msgid)) { } else if (fallbackMessages.hasOwnProperty(msgid)) {
msgstr = fallback_messages[msgid]; msgstr = fallbackMessages[msgid];
console.log('missing message for key ' + msgid + ' for current locale ' + locale); console.warn('[i18n] missing locale key ' + locale + ' / ' + msgid);
} else { } else {
console.log('missing message for key ' + msgid); console.warn('[i18n] missing key ' + msgid);
return msgid;
}
if (!raw) {
msgstr = encodeEntities(msgstr);
} }
if (msgid.substr(-3) == '_md') {
msgstr = parseMarkdownString(msgstr);
}
if (arguments.length > 1) { if (arguments.length > 1) {
if (typeof params == 'string') { if (typeof params == 'string') {
Array.prototype.shift.apply(arguments); Array.prototype.shift.apply(arguments);
@ -47,6 +78,7 @@ angular.module('myApp.i18n', ['izhukov.utils'])
msgstr = insertParams(msgstr, params); msgstr = insertParams(msgstr, params);
} }
} }
return msgstr; return msgstr;
} }
@ -64,10 +96,10 @@ angular.module('myApp.i18n', ['izhukov.utils'])
} }
if (locale != newValue) { if (locale != newValue) {
var new_messages = false; var newMessages = false;
var ngLocaleReady = false; var ngLocaleReady = false;
var onReady = function() { var onReady = function() {
if (new_messages === false || ngLocaleReady === false) { if (newMessages === false || ngLocaleReady === false) {
// only execute when both - ngLocale and the new messages - are loaded // only execute when both - ngLocale and the new messages - are loaded
return; return;
} }
@ -85,7 +117,7 @@ angular.module('myApp.i18n', ['izhukov.utils'])
// already holding a reference to our $locale will get the new values as well // already holding a reference to our $locale will get the new values as well
// this hack is necessary because ngLocale just isn't designed to be changed at runtime // this hack is necessary because ngLocale just isn't designed to be changed at runtime
deepUpdate($locale, angular.injector(['ngLocale']).get('$locale')); deepUpdate($locale, angular.injector(['ngLocale']).get('$locale'));
messages = new_messages; messages = newMessages;
locale = newValue; locale = newValue;
$route.reload(); $route.reload();
}; };
@ -99,16 +131,16 @@ angular.module('myApp.i18n', ['izhukov.utils'])
.attr('src', 'vendor/angular/i18n/angular-locale_' + newValue + '.js'); .attr('src', 'vendor/angular/i18n/angular-locale_' + newValue + '.js');
$http({method: 'GET', url: 'js/locales/' + newValue + '.json'}).success(function(json){ $http({method: 'GET', url: 'js/locales/' + newValue + '.json'}).success(function(json){
new_messages = json; newMessages = json;
onReady(); onReady();
}); });
} }
}; };
$http({method: 'GET', url: 'js/locales/en-us.json'}).success(function(json){ $http({method: 'GET', url: 'js/locales/en-us.json'}).success(function(json){
fallback_messages = json; fallbackMessages = json;
if (locale == 'en-us') { if (locale == 'en-us') {
messages = fallback_messages; messages = fallbackMessages;
} }
$route.reload(); $route.reload();
}); });

Loading…
Cancel
Save