Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

piral-translate: Do not replace falsy variables with an empty string #651

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/plugins/piral-translate/src/localize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const messages = {
en: {
hi: 'hello',
greeting: 'Hi {{name}}, welcome back',
secretNumber: 'The secret number is {{number}}.',
header: {
title: 'Hello world'
}
Expand Down Expand Up @@ -80,6 +81,12 @@ describe('Localize Module', () => {
expect(result).toBe('Hi , welcome back');
});

it('localizeGlobal does not replace falsy variables with an empty string', () => {
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('secretNumber', { number: 0 });
expect(result).toBe('The secret number is 0.');
});

it('localizeGlobal translates from global translations using passed nested translations', () => {
const localizer = new Localizer(messages, 'en');
const result = localizer.localizeGlobal('header.title');
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/piral-translate/src/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function defaultFallback(key: string, language: string): string {

function formatMessage<T extends object>(message: string, variables: T): string {
return message.replace(/{{\s*([A-Za-z0-9_.]+)\s*}}/g, (_match: string, p1: string) => {
return p1 in variables ? variables[p1] || '' : `{{${p1}}}`;
return p1 in variables ? variables[p1] ?? '' : `{{${p1}}}`;
});
}

Expand Down
Loading