'use strict'; (function() { var log, translations = {}; /*@ Ox.getLocale Returns locale () -> Locale (like 'de' or 'fr') @*/ Ox.getLocale = function() { return Ox.LOCALE; }; /*@ Ox.setLocale Sets locale (locale[, url], callback) locale Locale (like 'de' or 'fr') url one or more URLs of JSON file with additional translations callback Callback function success If true, locale has been set @*/ Ox.setLocale = function(locale, url, callback) { var isValidLocale = Ox.contains(Object.keys(Ox.LOCALE_NAMES), locale), urls = []; if (arguments.length == 2) { callback = arguments[1]; url = null; } if (isValidLocale) { Ox.LOCALE = locale; if (locale == 'en') { translations = {}; } else { translations = {}; Ox.forEach(Ox.LOCALES, function(locales, module) { if ( (module == 'Ox' || Ox.load[module]) && Ox.contains(locales, locale) ) { urls.push([ Ox.PATH + module + '/json/locale.' + locale + '.json' ]); } }); } url && Ox.makeArray(url).forEach(function(value) { urls.push(Ox.makeArray(value)); }); if (urls.length) { Ox.getJSON(urls, function(data) { Ox.forEach(data, function(values, url) { Ox.extend(translations, values); }); callback(true); }) } else { callback(true); } } else { callback(false); } }; /*@ Ox._ Localizes a string (string[, options]) -> Localized string string English string options Options passed to Ox.formatString @*/ Ox._ = function(value, options) { var translation = translations[value]; log && log(value, translation); translation = translation || value || ''; return Ox.formatString(translation, options); }; /*@ Ox._.log Registers a logging function (callback) -> undefined callback Callback function english English string translation Translated string @*/ Ox._.log = function(callback) { log = callback; }; })();