Skip to content

Commit ba7f83c

Browse files
FredrikWallstromadrianschmidt
authored andcommitted
feat(translations): add support for merge codes in translations
1 parent 786aeda commit ba7f83c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/global/translations.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,21 @@ const allTranslations = {
1818
sv: sv,
1919
};
2020

21+
const REGEX = /\{\s*(\w+)\s*\}/g;
22+
2123
export class Translations {
22-
public get(key, language = 'en') {
23-
return allTranslations[language][key];
24+
public get(key: string, language = 'en', params?: object): string {
25+
const translation = allTranslations[language][key];
26+
if (!translation) {
27+
return key;
28+
}
29+
30+
return translation.replace(
31+
REGEX,
32+
(match: string, mergeCodeKey: string) => {
33+
return params[mergeCodeKey] || match;
34+
},
35+
);
2436
}
2537
}
2638

0 commit comments

Comments
 (0)