add support to replace tokens

This commit is contained in:
Jacob Overgaard
2023-08-01 13:28:21 +02:00
parent 3350e4a95a
commit ddb81576cf
2 changed files with 17 additions and 0 deletions

View File

@@ -23,6 +23,8 @@ const english = {
general: {
close: 'Close',
logout: 'Log out',
withInlineToken: '{0} {1}',
withInlineTokenLegacy: '%0% %1%',
numUsersSelected: (count: number) => {
if (count === 0) return 'No users selected';
if (count === 1) return 'One user selected';
@@ -132,6 +134,11 @@ describe('UmbLocalizeController', () => {
expect(element.localize.term('general_numUsersSelected', 1)).to.equal('One user selected');
expect(element.localize.term('general_numUsersSelected', 2)).to.equal('2 users selected');
});
it('should return a term with a custom format with inline tokens', async () => {
expect(element.localize.term('general_withInlineToken', 'Hello', 'World')).to.equal('Hello World');
expect(element.localize.term('general_withInlineTokenLegacy', 'Hello', 'World')).to.equal('Hello World');
});
});
describe('date', () => {

View File

@@ -113,6 +113,16 @@ export class UmbLocalizeController<UserTranslation extends Translation = Default
return term(...args) as string;
}
if (typeof term === 'string') {
if (args.length > 0) {
// Replace placeholders of format "%index%" and "{index}" with provided values
term = term.replace(/(%(\d+)%|\{(\d+)\})/g, (match, _p1, p2, p3): string => {
const index = p2 || p3;
return String(args[index] || match);
});
}
}
return term;
}