UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

21 lines 2.71 kB
function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import isPlainObject from'lodash/isPlainObject';import cloneDeep from'lodash/cloneDeep';import merge from'lodash/merge';/** * Checks if an extension locale matches the theme locale. * @param {string} themeLocale The locale of the theme. * @param {string} extensionLocale The locale of an extension. * @return {boolean} */var localesMatch=function localesMatch(themeLocale,extensionLocale){// Extract the actual locale string from the extension locale. var _extensionLocale$spli=extensionLocale.split('/').reverse(),_extensionLocale$spli2=_slicedToArray(_extensionLocale$spli,1),locale=_extensionLocale$spli2[0];// Extract the language from the theme locale. var _themeLocale$split=themeLocale.split('-'),_themeLocale$split2=_slicedToArray(_themeLocale$split,1),themeLanguage=_themeLocale$split2[0];/** * Theme locales always contain language and region (en-US). But extension locales * might only scoped for a language (en). To be sure that all extension translations * are considered for the merge, both variants are checked. */return locale.toLowerCase()===themeLocale.toLowerCase()||locale.toLowerCase()===themeLanguage;};/** * Merges extension translations into the theme translations. * @param {Object} theme The translations object of a theme. * @param {Object} extensions The extension translations. * @param {string} [locale=process.env.LOCALE_FILE] The current active locale of the theme. * @return {Object} */var mergeTranslations=function mergeTranslations(theme,extensions){var locale=arguments.length>2&&arguments[2]!==undefined?arguments[2]:process.env.LOCALE_FILE;if(!isPlainObject(extensions)||Object.keys(extensions).length===0){// No extension translations provided. return cloneDeep(theme);}return Object.keys(extensions).reduce(function(result,extensionLocale){if(!localesMatch(locale,extensionLocale)){// Continue if the current extension locale doesn't match the theme locale. return result;}// Merge the current extensions into the result. return merge(result,extensions[extensionLocale]);},cloneDeep(theme));};export default mergeTranslations;