From 79ebc406a4c848c55aeb46fb679267c415bf2aed Mon Sep 17 00:00:00 2001 From: Duncan Robertson Date: Fri, 18 Jul 2025 16:29:41 +0100 Subject: [PATCH] feat(i18n): add support for i18next pluralization keys Introduced types for plural suffixes and a helper to strip them from keys. This enhances the translation key management in the i18n module. --- boilerplate/app/i18n/index.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/boilerplate/app/i18n/index.ts b/boilerplate/app/i18n/index.ts index 77d02108b..4047c0140 100644 --- a/boilerplate/app/i18n/index.ts +++ b/boilerplate/app/i18n/index.ts @@ -60,10 +60,17 @@ export const initI18n = async () => { /** * Builds up valid keypaths for translations. + * Includes support for i18next pluralization keys by stripping suffixes. */ export type TxKeyPath = RecursiveKeyOf +// i18next pluralization suffixes +type PluralSuffixes = "_zero" | "_one" | "_two" | "_few" | "_many" | "_other" + +// Helper to strip plural suffixes from keys +type StripPluralSuffix = T extends `${infer Base}${PluralSuffixes}` ? Base : T + // via: https://stackoverflow.com/a/65333050 type RecursiveKeyOf = { [TKey in keyof TObj & (string | number)]: RecursiveKeyOfHandleValue @@ -81,6 +88,14 @@ type RecursiveKeyOfHandleValue< ? Text : TValue extends object ? IsFirstLevel extends true - ? Text | `${Text}:${RecursiveKeyOfInner}` - : Text | `${Text}.${RecursiveKeyOfInner}` - : Text + ? + | Text + | `${Text}:${RecursiveKeyOfInner}` + | StripPluralSuffix + | `${StripPluralSuffix}:${RecursiveKeyOfInner}` + : + | Text + | `${Text}.${RecursiveKeyOfInner}` + | StripPluralSuffix + | `${StripPluralSuffix}.${RecursiveKeyOfInner}` + : Text | StripPluralSuffix