@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
31 lines • 3.5 kB
JavaScript
function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}import IntlMessageFormat from'intl-messageformat';import curry from'lodash/curry';// eslint-disable-next-line import/no-named-default
import{default as getPath}from'lodash/get';import moment from'moment';import messageCache from"./messageCache";/**
* Pure function to return original key whet Intl message not found
* @param {*} key origin intl key to return
* @returns {*}
*/var pureReturn=function pureReturn(key){return{format:function format(){return key;}};};/**
* Returns an instance of IntlMessageFormat from cache based on a hash.
* The hash is generated from given language code and translation key.
* If no instance exists yet, a new instance will be created and returned.
* @param {Object} locales A locales object.
* @param {string} langCode A language code.
* @param {string} key A translation key.
* @param {Object} options Additional options for the translator
* @param {boolean} [options.acceptPlainTextWithPlaceholders = false] When set to TRUE, the
* translator can also handle human readable texts that contain text replacement placeholders.
* @returns {IntlMessageFormat}
*/var getMessageFromCache=function getMessageFromCache(locales,langCode,key){var options=arguments.length>3&&arguments[3]!==undefined?arguments[3]:{};var _options$acceptPlainT=options.acceptPlainTextWithPlaceholders,acceptPlainTextWithPlaceholders=_options$acceptPlainT===void 0?false:_options$acceptPlainT;var hash="".concat(langCode,"_").concat(key,"_").concat(acceptPlainTextWithPlaceholders?1:0);// Check if a cached instance already exists.
if(messageCache[hash]){return messageCache[hash];}var message=getPath(locales,key,acceptPlainTextWithPlaceholders?key:undefined);if(typeof message!=='string'||message.length===0){return pureReturn(key);}// Prevent the app from crashing when strings (like product names) don't comply with the format
try{messageCache[hash]=new IntlMessageFormat(message,langCode,getPath(locales,'formats'));}catch(e){messageCache[hash]=pureReturn(key);}return messageCache[hash];};/**
* Get a translation for a given key.
* @param {Object} locales A locales object.
* @param {string} langCode A language code.
* @param {string} key A translation key.
* @param {Object} [args] Arguments for the translation.
* @param {Object} options Additional options for the translator
* @param {boolean} [options.acceptPlainTextWithPlaceholders = false] When set to TRUE, the
* translator can also handle human readable texts that contain text replacement placeholders.
* @returns {string}
*/var translate=function translate(locales,langCode,key){var args=arguments.length>3&&arguments[3]!==undefined?arguments[3]:{};var options=arguments.length>4&&arguments[4]!==undefined?arguments[4]:{};/**
* @returns {Object}
*/var sanitizeArgs=function sanitizeArgs(){if(_typeof(args)!=='object'||args===null){return args;}return Object.keys(args).reduce(function(acc,arg){var date=moment(args[arg],moment.ISO_8601,true);if(date.isValid()&&date.toISOString()===args[arg]){acc[arg]=new Date(args[arg]).getTime();}else{acc[arg]=args[arg];}return acc;},{});};return getMessageFromCache(locales,langCode,key,options).format(sanitizeArgs());};export default curry(translate);