Skip to content

Commit

Permalink
Merge pull request #731 from kchu93/MERC-7830
Browse files Browse the repository at this point in the history
feat(localization): merc-7830 support for region translations
  • Loading branch information
kchu93 authored Jul 23, 2021
2 parents d9c0f3d + 9e66d6b commit a6bc312
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 267 deletions.
44 changes: 39 additions & 5 deletions lib/regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,56 @@ function buildPageTemplate(templates, page) {
return pageTemplate;
}

/**
* Returns a boolean if there are two regions with the same name
* @param {Object} regions
* @param {Object} regionData
* @returns {boolean}
*/
function checkDuplicateRegionName(regions, regionData) {
let result = false;
const regionsArray = Array.from(regions);
for (let i = 0; i < regionsArray.length; i += 1) {
if (regionsArray[i].name === regionData.name) {
result = true;
break;
}
}

return result;
}

/**
* Parse a template content and return an array of unique region objects
* @param {string} content
* @returns {Array}
*/
function parseRegions(content) {
const regionRegex = /{{3}\s*region\s+.*?name=["|']([\w-]+)["|'].*?}{3}/g;
const regionRegex = /{{3}\s*region\s+.*?name=["|']([\w-]+)["|'].*?}{3}?/g;
const translationRegex = /{{3}.*?\stranslation=["|'](i18n.RegionName.([\w-])+)["|'].*?}{3}?/g;
const regions = new Set();
let match;
let regionMatch;
const translationMatch = translationRegex.exec(content);

// eslint-disable-next-line
while ((match = regionRegex.exec(content))) {
regions.add(match[1]);
while ((regionMatch = regionRegex.exec(content))) {
const regionData = {};
// eslint-disable-next-line prefer-destructuring
regionData.name = regionMatch[1];

// If there is a translation match and regionName match is valid, add data to region
if (translationMatch && regionMatch[1]) {
// eslint-disable-next-line prefer-destructuring
regionData.translation = translationMatch[1];
}

if (!checkDuplicateRegionName(regions, regionData)) {
regions.add(regionData);
}
}
const regionArray = Array.from(regions);

return Array.from(regions).map((name) => ({ name }));
return regionArray.map(({ name, translation }) => ({ name, translation }));
}

/**
Expand Down
14 changes: 10 additions & 4 deletions lib/regions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ describe('Stencil Bundle', () => {
describe('Regions', () => {
describe('parseRegions', () => {
const map = {
'{{{region name="_foobar"}}}': [{ name: '_foobar' }],
'{{{ region name="foo-bar"}}}': [{ name: 'foo-bar' }],
'{{{ region name="foobar__"}}}': [{ name: 'foobar__' }],
'{{{region translation="i18n.RegionName.TestingTranslation" name="_foobar"}}}': [
{ name: '_foobar', translation: 'i18n.RegionName.TestingTranslation' },
],
'{{{ region name="foo-bar" translation="i18n.RegionName.Testing-translations" }}}': [
{ name: 'foo-bar', translation: 'i18n.RegionName.Testing-translations' },
],
'{{{ region name="foobar__" translation="testing-without-i18n-prefix"}}}': [
{ name: 'foobar__' },
],
'{{{ region name="foo_bar" }}}': [{ name: 'foo_bar' }],
'{{{ region name="foo-_bar" }}}': [{ name: 'foo-_bar' }],
'{{{ region name="foobar1" }}}': [{ name: 'foobar1' }],
'{{{ region name=" " }}}': [],
'{{{ region name="invalid name" }}}': [],
'{{{ region name="invalid name" translation="i18n.RegionName.ValidTranslation"}}}': [],
'{{ region name="two_brackets" }}': [],
"{{{ region name='foobar' }}}": [{ name: 'foobar' }],
'{{{region name="foobar" type="widget"}}}': [{ name: 'foobar' }],
Expand Down
10 changes: 9 additions & 1 deletion lib/validator/schema-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ class ValidatorSchemaTranslations {
* @returns {array}
*/
findUnusedKeys() {
return this.translationsKeys.filter((key) => !this.schemaKeys.includes(key));
return this.translationsKeys.filter((key) => {
const translationRegex = /^i18n.RegionName.*?/g;
const translationMatch = translationRegex.exec(key);
if (translationMatch) {
return false;
}

return !this.schemaKeys.includes(key);
});
}

/**
Expand Down
Loading

0 comments on commit a6bc312

Please sign in to comment.