DEV Community

Discussion on: Using i18Next with Svelte

Collapse
 
nishugoel profile image
Nishu Goel

Hey! Well put, how do I specify the namespace in consuming space and get the translated string from the namespace?

Collapse
 
sbelzile profile image
Sébastien Belzile

(before you read: I did not test this answer nor did I spend a lot of time thinking about it, but this is the direction I would take)

Either directly:

t("namespace:key")
Enter fullscreen mode Exit fullscreen mode

Or you could expose (instead of t) a function that creates a derived store using i18next's getFixedT:

const getT = (ns: string | string[]) => derived([languageStore], $language => (key) => i18n.getFixedT($language, ns)(key));
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sbelzile profile image
Sébastien Belzile

You would then use it this way:

<script>
/// ...
const t = getT('myNamespace')
</script>

{$t("my-key")}
Enter fullscreen mode Exit fullscreen mode