@buurman/i18n
Version:
The buurman i18n, translations and utilities for internationalization.
120 lines (109 loc) • 4.36 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var sdk = require('@buurman/sdk');
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
var en = {
"nouns.integration": "<%= amount === 1 ? 'integration' : 'integrations' %>",
"prompt.integrations.foundApplicable": "Found <%= integrations.length %> applicable <%= i18n('nouns.integration', {amount: integrations.length}) %>, <%= integrations.length === 1 ? 'select it if you would like to apply it' : 'select the integrations you would like to apply'%>.\n"
};
var templatifyLanguage = function (language) {
return sdk.entries(language).reduce(function (words, _a) {
var _b;
var _c = __read(_a, 2), key = _c[0], value = _c[1];
try {
var tpl = sdk.template(value, { imports: { i18n: i18n } });
return __assign(__assign({}, words), (_b = {}, _b[key] = tpl, _b));
}
catch (e) {
console.error("Invalid template$");
console.error(" current locale: " + currentLocale);
console.error(" word : " + key);
return words;
}
}, {});
};
var supportsLocale = function (locale) { return locale in languages; };
var setLocale = function (locale) {
if (!supportsLocale(locale)) {
console.warn("Unable to set locale to " + locale);
return;
}
currentLocale = locale;
currentWords = languages[currentLocale];
};
var addWord = function (word, templates) {
sdk.entries(templates).forEach(function (_a) {
var _b = __read(_a, 2), locale = _b[0], templateString = _b[1];
if (!supportsLocale(locale)) {
console.warn("Can't add word to " + locale + ", locale doesn't exist.");
return;
}
languages[locale][word] = sdk.template(templateString);
});
};
var i18n = function (word, data) {
if (!(word in currentWords)) {
console.warn("Word \"" + word + "\" not defined for locale " + currentLocale);
return word;
}
return currentWords[word](data);
};
var initialize = function () {
var INITIAL_LOCALE = supportsLocale(SYSTEM_LOCALE)
? SYSTEM_LOCALE
: DEFAULT_LOCALE;
setLocale(INITIAL_LOCALE);
};
var languages = {
en: templatifyLanguage(en),
};
var currentLocale;
var currentWords;
var DEFAULT_LOCALE = "en";
// Intl.DateTimeFormat().resolvedOptions().locale equals something like "en-US"
var SYSTEM_LOCALE = Intl.DateTimeFormat()
.resolvedOptions()
.locale.substr(0, 2);
initialize();
exports.addWord = addWord;
exports.i18n = i18n;
exports.setLocale = setLocale;
exports.supportsLocale = supportsLocale;
//# sourceMappingURL=index.js.map