WIP: try to convert to ES modules
This commit is contained in:
parent
ec5b050496
commit
299a08b6a3
29 changed files with 3003 additions and 2014 deletions
|
|
@ -1,90 +1,103 @@
|
|||
'use strict';
|
||||
import * as OxArray from './Array.js';
|
||||
import * as OxObject from './Object.js';
|
||||
import * as OxConstants from './Constants.js';
|
||||
import * as OxMath from './Math.js';
|
||||
import * as OxString from './String.js';
|
||||
|
||||
(function() {
|
||||
const Ox = {};
|
||||
|
||||
var log, translations = {};
|
||||
Object.assign(Ox,
|
||||
OxArray,
|
||||
OxObject,
|
||||
OxConstants,
|
||||
OxMath,
|
||||
OxString,
|
||||
);
|
||||
|
||||
/*@
|
||||
Ox.getLocale <f> Returns locale
|
||||
() -> <s> Locale (like 'de' or 'fr')
|
||||
@*/
|
||||
Ox.getLocale = function() {
|
||||
return Ox.LOCALE;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.setLocale <f> Sets locale
|
||||
(locale[, url], callback)
|
||||
locale <s> Locale (like 'de' or 'fr')
|
||||
url <s|[s]> one or more URLs 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 = [];
|
||||
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);
|
||||
}
|
||||
var log, translations = {};
|
||||
|
||||
/*@
|
||||
Ox.getLocale <f> Returns locale
|
||||
() -> <s> Locale (like 'de' or 'fr')
|
||||
@*/
|
||||
export function getLocale() {
|
||||
return Ox.LOCALE;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.setLocale <f> Sets locale
|
||||
(locale[, url], callback)
|
||||
locale <s> Locale (like 'de' or 'fr')
|
||||
url <s|[s]> one or more URLs of JSON file with additional translations
|
||||
callback <f> Callback function
|
||||
success <b> If true, locale has been set
|
||||
@*/
|
||||
export function setLocale(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 {
|
||||
callback(false);
|
||||
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._ <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 || '';
|
||||
return Ox.formatString(translation, options);
|
||||
};
|
||||
/*@
|
||||
Ox._ <f> Localizes a string
|
||||
(string[, options]) -> <s> Localized string
|
||||
string <s> English string
|
||||
options <o> Options passed to Ox.formatString
|
||||
@*/
|
||||
export function _(value, options) {
|
||||
var translation = translations[value];
|
||||
log && log(value, translation);
|
||||
translation = translation || value || '';
|
||||
return Ox.formatString(translation, options);
|
||||
};
|
||||
|
||||
/*@
|
||||
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;
|
||||
};
|
||||
/*@
|
||||
Ox._.log <f> Registers a logging function
|
||||
(callback) -> <u> undefined
|
||||
callback <f> Callback function
|
||||
english <s> English string
|
||||
translation <s> Translated string
|
||||
@*/
|
||||
_.log = function(callback) {
|
||||
log = callback;
|
||||
};
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue