UNPKG

@shopgate/engage

Version:
25 lines 3 kB
import get from'lodash/get';import{getTranslator,getPriceFormatter,getDateFormatter,getTimeFormatter,getNumberFormatter}from'@shopgate/pwa-common/helpers/i18n';import{logger}from'@shopgate/pwa-core/helpers';/** * @typedef {Object} I18nHelpers * @property {Function} init - Inits locales. * @property {Function} text - Text translator. * @property {Function} textWithDefault - Text translator with default supplier. * @property {Function} price - Price translator. * @property {Function} date - Date translator. * @property {Function} time - Time translator. * @property {Function} number - Number translator * @property {Function} getSupplementalData - Get Supplemental Data * @property {Function} getPath - Get locale data by path * @property {Function} getLang - Get locale lang code, eg de-DE */ /** * I18n helpers. * @returns {I18nHelpers} */var I18n=function I18n(){var didInit=false;/** * Function that throws when called. Used in a default instance of i18n to notify developer * when the module is used without initialization. */var notReadyCb=function notReadyCb(){logger.error('i18n is not yet ready. Please postpone translations until App is bootstrapped.');};return{init:function init(_ref){var _this=this;var locales=_ref.locales,lang=_ref.lang,_ref$currencyLocale=_ref.currencyLocale,currencyLocale=_ref$currencyLocale===void 0?null:_ref$currencyLocale;if(didInit){logger.warn('Looks like i18n locales are already initialized. Changing locales now may lead to inconsistent translations.');}didInit=true;this.text=getTranslator(locales,lang);this.textWithDefault=function(key,defaultText){var text=_this.text(key);return text===key?defaultText:text;};this.price=getPriceFormatter(currencyLocale||lang);this.date=getDateFormatter(lang);this.time=getTimeFormatter(lang);this.number=getNumberFormatter(lang);this.getSupplementalData=function(){return locales.supplementalData||{};};this.getPath=function(path){return get(locales,path);};this.getLang=function(){return lang;};// If component decides to act accordingly this information should be exposed. this.ready=true;},text:notReadyCb,textWithDefault:notReadyCb,price:notReadyCb,date:notReadyCb,time:notReadyCb,number:notReadyCb,getSupplementalData:notReadyCb,getPath:notReadyCb,getLang:notReadyCb,ready:false};};/** * @type {I18nHelpers} */export var i18n=I18n();/** * Return order of week days, based on firstDay of week locale settings * @returns {string[]} */export var getWeekDaysOrder=function getWeekDaysOrder(){var _i18n$getSupplemental=i18n.getSupplementalData(),_i18n$getSupplemental2=_i18n$getSupplemental.weekData,_i18n$getSupplemental3=_i18n$getSupplemental2===void 0?{}:_i18n$getSupplemental2,_i18n$getSupplemental4=_i18n$getSupplemental3.firstDay,firstDay=_i18n$getSupplemental4===void 0?'sun':_i18n$getSupplemental4;var days=['sun','mon','tue','wed','thu','fri','sat'];var dayIndex=days.findIndex(function(d){return d===firstDay;});return days.slice(dayIndex).concat(days.slice(0,dayIndex));};