Skip to content

Commit

Permalink
feat: add ability to localize common characters
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart committed Nov 8, 2024
1 parent 2d7962d commit 5c5d537
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@
"rules": {
"no-console": 0
}
}]
},
{
"files": "./lang/*.js",
"rules": {
"quotes": 0
}
}]
}
9 changes: 9 additions & 0 deletions intl.serge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "intl",
"parser_plugin": {
"plugin": "parse_js"
},
"source_match": "en\\.js$",
"source_dir": "lang",
"output_file_path": "lang/%LANG%.js"
}
15 changes: 15 additions & 0 deletions lang/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
"intl-common:characters:apostrophe": "apostrophe",
"intl-common:characters:ampersand": "ampersand",
"intl-common:characters:asterisk": "asterisk",
"intl-common:characters:backslash": "backslash",
"intl-common:characters:colon": "colon",
"intl-common:characters:comma": "comma",
"intl-common:characters:greaterThan": "greater-than sign",
"intl-common:characters:lessThan": "less-than sign",
"intl-common:characters:numberSign": "number sign",
"intl-common:characters:percentSign": "percent sign",
"intl-common:characters:pipe": "pipe",
"intl-common:characters:questionMark": "question mark",
"intl-common:characters:quotationMark": "quotation mark",
};
26 changes: 26 additions & 0 deletions lib/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import IntlMessageFormat from 'intl-messageformat';

export const allowedTags = Object.freeze(['d2l-link', 'd2l-tooltip-help', 'p', 'br', 'b', 'strong', 'i', 'em', 'button']);

const characterMap = new Map([
['\'', 'apostrophe'],
['&', 'ampersand'],
['*', 'asterisk'],
['\\', 'backslash'],
[':', 'colon'],
[',', 'comma'],
['>', 'greaterThan'],
['<', 'lessThan'],
['#', 'numberSign'],
['%', 'percentSign'],
['|', 'pipe'],
['?', 'questionMark'],
['"', 'quotationMark']
]);

const getDisallowedTagsRegex = allowedTags => {
const validTerminators = '([>\\s/]|$)';
const allowedAfterTriangleBracket = `/?(${allowedTags.join('|')})?${validTerminators}`;
Expand Down Expand Up @@ -112,6 +128,13 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
return formattedMessage;
}

localizeCharacter(char) {
if (!characterMap.has(char)) {
throw new Error(`localizeCharacter() does not support character: "${char}"`);
}
return this.localize(`intl-common:characters:${characterMap.get(char)}`);
}

localizeHTML(name, replacements = {}) {

const { language, value } = this.localize.resources?.[name] ?? {};
Expand Down Expand Up @@ -166,6 +189,9 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
const possibleLanguages = this._generatePossibleLanguages(config);
const resourcesPromise = this.getLocalizeResources(possibleLanguages, config);
resourcesLoadedPromises.push(resourcesPromise);
resourcesLoadedPromises.push(this.getLocalizeResources(possibleLanguages, {
importFunc: async lang => (await import(`../lang/${lang}.js`)).default
}));
}
return Promise.all(resourcesLoadedPromises);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "npm run lint && npm run test:unit"
},
"files": [
"/lang",
"/lib",
"/helpers"
],
Expand Down
16 changes: 16 additions & 0 deletions test/localize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ describe('Localize', () => {

});

describe('localizeCharacter', () => {

it('should localize "&"', async() => {
await localizer.ready;
const localized = localizer.localizeCharacter('&');
expect(localized).to.equal('ampersand');
});

it('should throw an error for unknown characters', async() => {
await localizer.ready;
expect(() => localizer.localizeCharacter('$'))
.to.throw('localizeCharacter() does not support character: "$"');
});

});

describe('localizeHTML()', () => {

it('should localize, replacing tags with HTML', async() => {
Expand Down

0 comments on commit 5c5d537

Please sign in to comment.