oxjs/source/Ox/js/Locale.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

2013-05-09 13:12:26 +00:00
'use strict';
(function() {
var log, translations = {};
/*@
Ox.setLocale <f> Sets locale
(locale[, url], callback)
locale <s> Locale (like 'de' or 'fr')
url <s> URL of JSON file with additional translations
callback <f> Callback function
success <b> If true, locale has been set
@*/
Ox.setLocale = function(locale, url, callback) {
var isValidLocale = Ox.contains(Object.keys(Ox.LOCALE_NAMES), locale),
urls = [];
2013-05-09 13:12:26 +00:00
if (arguments.length == 2) {
callback = arguments[1];
url = null;
}
if (locale != Ox.LOCALE && isValidLocale) {
Ox.LOCALE = locale;
if (locale == 'en') {
translations = {};
callback(true);
} else {
Ox.forEach(Ox.LOCALES, function(locales, module) {
if (Ox.contains(locales, locale)) {
urls.push([
Ox.PATH + 'Ox' + (module ? '.' + module : '')
+ '/json/locale.' + locale + '.json'
]);
2013-05-09 13:12:26 +00:00
}
});
url && urls.push([url]);
Ox.getJSON(urls, function(data) {
urls.forEach(function(url) {
Ox.extend(translations, data[url]);
callback(true);
});
});
2013-05-09 13:12:26 +00:00
}
} else {
callback(isValidLocale);
}
};
/*@
Ox._ <f> Localizes a string
(string[, options]) -> <s> Localized string
string <s> English string
options <o> Options passed to Ox.formatString
@*/
Ox._ = function(value, options) {
var translation = translations[value];
log && log(value, translation);
translation = translation || value;
2013-05-09 13:03:33 +00:00
return Ox.formatString(translation, options, true);
2013-05-09 13:12:26 +00:00
};
/*@
Ox._.log <f> Registers a logging function
(callback) -> <u> undefined
callback <f> Callback function
english <s> English string
translation <s> Translated string
@*/
Ox._.log = function(callback) {
log = callback;
};
2013-05-09 13:03:33 +00:00
})();