-
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.
Update i18n: Support custom translation dict as argument (#12)
* Update i18n to take dict as input * Remove defaultLocaleKey * Update i18n config type * Add list of locale codes * Only keep primary in list of locale codes * Add locales & default locale types * Export i18n config with const type * Fix locales list type to generalize * Use Locales type for i18n function * Add tranlations for "or" * Refactor Tail * Refactor recursive type functions * Fix types to support function overloads type * Use explicit dict for some translations in index
- Loading branch information
Showing
7 changed files
with
144 additions
and
30 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
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ const defaultLocale = | |
// Global | ||
'Back to home page', | ||
'and', | ||
'or', | ||
|
||
// Footer | ||
'Open-source repository', | ||
|
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,21 +1,71 @@ | ||
import i18nConfig from '/config/i18n' | ||
import type { I18nConfig } from '/config/i18n' | ||
|
||
import defaultLocaleData from './locales/en' | ||
|
||
const defaultLocale = i18nConfig.defaultLocale | ||
|
||
const defaultLocaleKey = 'en' as const | ||
|
||
type I18n = Readonly<Record<keyof typeof defaultLocaleData, string>> | ||
|
||
type Diff<T, U> = T extends U ? never : T | ||
|
||
type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> } | ||
|
||
// Inspired from `ToTuple<Union>`: https://stackoverflow.com/a/70061272 | ||
type LocalesToUnion< | ||
Locales extends any[] | readonly any[], | ||
Rslt extends any = never, | ||
> = | ||
DeepWriteable<Locales> extends [infer Head, ...infer Tail] | ||
? LocalesToUnion< | ||
Tail, | ||
Rslt | ( | ||
DeepWriteable<Head> extends { codes: string[] } | ||
? DeepWriteable<Head>['codes'][0] | ||
: Head | ||
) | ||
> | ||
: Rslt | ||
|
||
type Locales = LocalesToUnion<typeof i18nConfig.locales> | ||
|
||
type LocalesToList< | ||
Locales extends any[] | readonly any[], | ||
Rslt extends readonly any[] = [], | ||
> = | ||
DeepWriteable<Locales> extends [infer Head, ...infer Tail] | ||
? LocalesToList< | ||
Tail, | ||
[ | ||
...Rslt, | ||
( | ||
DeepWriteable<Head> extends { codes: string[] } | ||
? DeepWriteable<Head>['codes'][0] | ||
: Head | ||
), | ||
] | ||
> | ||
: Rslt | ||
|
||
type LocalesList = LocalesToList<typeof i18nConfig.locales> | ||
|
||
const locales: LocalesList = | ||
(i18nConfig.locales satisfies I18nConfig['locales'] as I18nConfig['locales']) | ||
.map( | ||
locale => typeof locale === 'string' ? locale : locale.codes[0] | ||
) satisfies string[] as LocalesList | ||
|
||
type DefaultLocale = Locales & typeof i18nConfig.defaultLocale | ||
|
||
const defaultLocale: DefaultLocale = i18nConfig.defaultLocale | ||
|
||
export { | ||
defaultLocale, | ||
defaultLocaleKey, | ||
locales, | ||
} | ||
|
||
export type { | ||
I18n, | ||
Diff, | ||
Locales, | ||
LocalesList, | ||
DefaultLocale, | ||
} |
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