Skip to content

Instantly share code, notes, and snippets.

@icorbrey
Created August 23, 2021 19:41
Show Gist options
  • Save icorbrey/78bfd29c867c7a0d65dc8a80ad16ad80 to your computer and use it in GitHub Desktop.
Save icorbrey/78bfd29c867c7a0d65dc8a80ad16ad80 to your computer and use it in GitHub Desktop.
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