Skip to content

Commit 256ddd1

Browse files
committed
feat: translation function to support key replacements
1 parent 75a1223 commit 256ddd1

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

lib/build/genericComponentOverrideContext.js

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/translation/translationHelpers.d.ts

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/translation/translationHelpers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export async function getCurrentLanguageFromCookie(): Promise<string | null> {
7373
}
7474
}
7575

76-
export const getTranslationFunction = <T extends string>(...stores: TranslationStore[]): ((key: T) => string) => {
76+
export const getTranslationFunction = <T extends string>(
77+
...stores: TranslationStore[]
78+
): ((key: T, replacements?: Record<string, string>) => string) => {
7779
const { translationEventSource, userTranslationFunc, defaultLanguage, userTranslationStore } =
7880
SuperTokens.getInstanceOrThrow().languageTranslations;
7981

@@ -109,7 +111,7 @@ export const getTranslationFunction = <T extends string>(...stores: TranslationS
109111
if (cookieLanguage) setLanguage(cookieLanguage);
110112
});
111113

112-
const translate = (key: string) => {
114+
const translate = (key: string, replacements: Record<string, string> = {}) => {
113115
if (userTranslationFunc) {
114116
return userTranslationFunc(key);
115117
}
@@ -125,7 +127,7 @@ export const getTranslationFunction = <T extends string>(...stores: TranslationS
125127
return key;
126128
}
127129

128-
return res;
130+
return res.replace(/{(\w+)}/g, (match, p1) => replacements[p1] || match);
129131
}
130132

131133
throw new Error("Should never come here");

0 commit comments

Comments
 (0)