UNPKG

@ecomplus/utils

Version:

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

57 lines (54 loc) 1.97 kB
import { DEFAULT_LANG } from './../lib/constants.mjs' import _config from './../lib/config.mjs' /** * @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á */ const i18n = (dictionary, lang = _config.get('lang')) => { if (typeof dictionary === 'object' && dictionary !== null) { const 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[DEFAULT_LANG] || dictionary[prop] } else { // recursive const localDictionary = Array.isArray(dictionary) ? [] : {} for (const prop in dictionary) { if (dictionary[prop] !== undefined) { localDictionary[prop] = i18n(dictionary[prop], lang) } } return localDictionary } } return dictionary } export default i18n