S
SolidJSโ€ข15mo ago
oneiro

Best way to dynamically access i18n-translations with typescript

Hey folks, I am currently using @solid-primitives/i18n (https://github.com/solidjs-community/solid-primitives/tree/main/packages/i18n#readme) for translation purposes. However there's one thing i struggle with: Some of the keys I would like to access are coming from an api, where each key is typed as generic string. For example imagine that my dict looks like this:
const en_dict = {
foo: {
bar: 'Bar',
baz: 'Baz'
}
}
const en_dict = {
foo: {
bar: 'Bar',
baz: 'Baz'
}
}
And I want to translate like this:
const translated = t(`foo.${apiResponse.key}`) // key could be any string here
const translated = t(`foo.${apiResponse.key}`) // key could be any string here
This of course makes typescript complain, as we can't be sure here that our key does actually exist inside the dict. What would be the best way to go about this here? I imagine I would need some form of wrapper around t() that still retains its api, but e.g. returns an empty string or throws an error if the key is not part of the dict. Are there any best practices how to deal with that? Thanks in advance ๐Ÿ™‚
GitHub
solid-primitives/packages/i18n at main ยท solidjs-community/solid-pr...
A library of high-quality primitives that extend SolidJS reactivity. - solidjs-community/solid-primitives
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server