Created
August 23, 2021 19:41
-
-
Save icorbrey/78bfd29c867c7a0d65dc8a80ad16ad80 to your computer and use it in GitHub Desktop.
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
type LocaleStrings<K extends string> = { | |
[key in K]: string | |
} | |
type LocaleArea<A extends string, S extends string | number> = { | |
[K in `@${A}/${S}`]: string | |
} | |
const area = <A extends string, K extends string>(area: A, strings: LocaleStrings<K>): LocaleArea<A, K> => | |
Object.keys(strings) | |
.map(key => ({ [`@${area}/${key}`]: strings[key] })) | |
.reduce((x, y) => ({ ...x, ...y }), {}) as LocaleArea<A, K> | |
const en_US = { | |
...area('Contact', { | |
'GitHub': 'Visit @icorbrey on GitHub', | |
'LinkedIn': 'Visit /in/isaaccorbrey on LinkedIn', | |
'Twitter': 'Visit @icorbrey on Twitter', | |
}) | |
} | |
type Locale = typeof en_US | |
type LocaleKeys = keyof Locale | |
const locale = en_US | |
const localized = (key: LocaleKeys) => | |
en_US[key] ?? `[[${key}]]` | |
const mockLocalized = (key: LocaleKeys) => | |
key as string | |
console.log(localized('@Contact/GitHub')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment