UNPKG

@iobroker/adapter-react

Version:

React classes to develop admin interfaces for ioBroker with react.

181 lines (156 loc) 5.54 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); /*** * Copyright 2018-2022 bluefox <dogafox@gmail.com> * * MIT License * ***/ /** * Translation string management. */ var I18n = /*#__PURE__*/function () { function I18n() { (0, _classCallCheck2["default"])(this, I18n); } (0, _createClass2["default"])(I18n, null, [{ key: "setLanguage", value: /** * List of all languages with their translations. * @type {{ [lang in ioBroker.Languages]?: Record<string, string>; }} */ /** * The currently displayed language. * @type {ioBroker.Languages} */ /** * Set the language to display. * @param {ioBroker.Languages} lang */ function setLanguage(lang) { if (lang) { I18n.lang = lang; } } /** * Add translations * User can provide two types of structures: * - {"word1": "translated word1", "word2": "translated word2"}, but in this case the lang must be provided * - {"word1": {"en": "translated en word1", "de": "translated de word1"}, "word2": {"en": "translated en word2", "de": "translated de word2"}}, but no lang must be provided * @param {object} words additional words for specific language * @param {ioBroker.Languages} lang */ }, { key: "extendTranslations", value: function extendTranslations(words, lang) { try { if (!lang) { Object.keys(words).forEach(function (word) { Object.keys(words[word]).forEach(function (lang) { if (!I18n.translations[lang]) { console.warn("Used unknown language: ".concat(lang)); } if (!I18n.translations[lang][word]) { I18n.translations[lang][word] = words[word][lang]; } else if (I18n.translations[lang][word] !== words[word][lang]) { console.warn("Translation for word \"".concat(word, "\" in \"").concat(lang, "\" was ignored: existing = \"").concat(I18n.translations[lang][word], "\", new = ").concat(words[word][lang])); } }); }); } else { if (!I18n.translations[lang]) { console.warn("Used unknown language: ".concat(lang)); } I18n.translations[lang] = I18n.translations[lang] || {}; Object.keys(words).forEach(function (word) { if (!I18n.translations[lang][word]) { I18n.translations[lang][word] = words[word]; } else if (I18n.translations[lang][word] !== words[word]) { console.warn("Translation for word \"".concat(word, "\" in \"").concat(lang, "\" was ignored: existing = \"").concat(I18n.translations[lang][word], "\", new = ").concat(words[word])); } }); } } catch (e) { console.error("Cannot apply translations: ".concat(e)); } } /** * Sets all translations (in all languages). * @param {{ [lang in ioBroker.Languages]?: Record<string, string>; }} translations */ }, { key: "setTranslations", value: function setTranslations(translations) { if (translations) { I18n.translations = translations; } } /** * Get the currently chosen language. * @returns {ioBroker.Languages} The current language. */ }, { key: "getLanguage", value: function getLanguage() { return I18n.lang; } /** * Translate the given string to the selected language. * @param {string} word The (key) word to look up the string. * @param {string[]} args Optional arguments which will replace the first (second, third, ...) occurrences of %s */ }, { key: "t", value: function t(word) { var translation = I18n.translations[I18n.lang]; if (translation) { var w = translation[word]; if (w) { word = w; } else { I18n._disableWarning && console.log("Translate: ".concat(word)); } } for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } for (var _i = 0, _args = args; _i < _args.length; _i++) { var arg = _args[_i]; word = word.replace('%s', arg); } return word; } /** * Disable warning about non-translated words * Required during development * @param {boolean} disable Do the warning should be disabled */ }, { key: "disableWarning", value: function disableWarning(disable) { I18n._disableWarning = !!disable; } }]); return I18n; }(); /*I18n.translations = { 'en': require('./i18n/en'), 'ru': require('./i18n/ru'), 'de': require('./i18n/de'), }; I18n.fallbacks = true; I18n.t = function () {};*/ (0, _defineProperty2["default"])(I18n, "translations", {}); (0, _defineProperty2["default"])(I18n, "lang", window.sysLang || 'en'); (0, _defineProperty2["default"])(I18n, "_disableWarning", false); var _default = I18n; exports["default"] = _default; //# sourceMappingURL=i18n.js.map