UNPKG

@ecomplus/utils

Version:

JS utility functions to E-Com Plus (not only) related apps

72 lines (71 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/es.object.keys.js"); require("core-js/modules/es.regexp.exec.js"); require("core-js/modules/es.symbol.js"); require("core-js/modules/es.symbol.description.js"); require("core-js/modules/es.object.to-string.js"); require("core-js/modules/es.symbol.iterator.js"); require("core-js/modules/es.array.iterator.js"); require("core-js/modules/es.string.iterator.js"); require("core-js/modules/web.dom-collections.iterator.js"); var _constants = require("./../lib/constants"); var _config2 = _interopRequireDefault(require("./../lib/config")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } /** * @method * @memberof ecomUtils * @name i18n * @description Get translated string by lang code from dictionary object. * @param {Object.<string, *>} dictionary - Dictionary object containing string in multiple langs * @param {string} [lang=$ecomConfig.get('lang')] - Snake case language code, eg.: 'en_us', 'pt_br' * @returns {string|Object.<string, *>} * * @example * // With simple dictionary objects * ecomUtils.i18n({ en_us: 'Hello', pt_br: 'Olá' }) * // => 'Hello' * ecomUtils.i18n({ en_us: 'Hello', pt_br: 'Olá' }, 'pt_br') * // => 'Olá' * * @example * // With nested objects * ecomUtils.i18n({ hello: { en_us: 'Hello', pt_br: 'Olá' }}) * // => { hello: 'Hello' } * ecomUtils.i18n({ hello: { en_us: 'Hello', pt_br: 'Olá' }, world: { en_us: 'World' }}, 'pt_br') * // => { hello: 'Olá', world: 'World' } * ecomUtils.i18n({ en_us: { hello: 'Hello', world: 'World' }, pt_br: { hello: 'Olá' }}, 'pt_br') * // => { hello: 'Olá' } * * @example * // You can also set the configured lang first * $ecomConfig.set('lang', 'pt_br') * // Then call `i18n` without expliciting lang * ecomUtils.i18n({ en_us: 'Hello', pt_br: 'Olá' }) * // => Olá */ var i18n = function i18n(dictionary) { var lang = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _config2.default.get('lang'); if (_typeof(dictionary) === 'object' && dictionary !== null) { var prop = Object.keys(dictionary)[0]; if (/^[a-z]{2}(_[a-z]{2})?$/.test(prop)) { // supposed to be object of languages options return dictionary[lang] || dictionary[_constants.DEFAULT_LANG] || dictionary[prop]; } else { // recursive var localDictionary = Array.isArray(dictionary) ? [] : {}; for (var _prop in dictionary) { if (dictionary[_prop] !== undefined) { localDictionary[_prop] = i18n(dictionary[_prop], lang); } } return localDictionary; } } return dictionary; }; var _default = exports.default = i18n;