-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5d7566
commit 27e8a46
Showing
6 changed files
with
40 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { i18nPlugin } from '@/src/i18nPlugin.ts' | ||
export { createTranslator } from '@/src/createTranslator.ts' | ||
|
||
export type { TranslationState } from '@/src/types.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Retrieves a translation value from a nested translation object. | ||
* | ||
* @param translations - The translations object (e.g., ctx.state.translationData). | ||
* @returns A function `t` that takes a translation key in dot notation and returns the translated string. | ||
*/ | ||
export function createTranslator( | ||
translations: Record<string, Record<string, string>>, | ||
) { | ||
/** | ||
* Translates a key string like 'common.title' or 'common.titlerow.title.example' | ||
* by traversing the nested structure of `translations`. | ||
* | ||
* @param key - The translation key in dot notation (e.g., 'common.title'). | ||
* @returns The translated string, or an empty string if the key is not found. | ||
*/ | ||
const t = (key: string): string => { | ||
const keys = key.split('.') | ||
let result: unknown = translations | ||
|
||
for (const k of keys) { | ||
if (typeof result === 'object' && result !== null && k in result) { | ||
result = (result as Record<string, unknown>)[k] | ||
} else { | ||
return '' // Return empty string if key is not found | ||
} | ||
} | ||
|
||
return typeof result === 'string' ? result : '' | ||
} | ||
|
||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters