Questions regarding i18next

Hello, so i recently started developing with sapphire and i am currently trying to implement internationalization, but i ran into two issues i couldn't yet find a solution for. 1. Fallback for "invalid languages" Since i only plan on translating the bot in certain languages i need the bot to handle whenever a user has a different locale set. I thought i could achieve that with i18n.i18next.fallbackLng, but that didn't work and i still get "ReferenceError: Invalid language provided", whenever a language that has no translations is being provided in i18n.fetchLanguage(). 2. Setting the namespace for the translate function If we take the ping command as an example: I was planning on first fetching the t function and then using it to translate all the messages in the command, in this case the ping and pong. But from what i've seen fetchT() doesn't take a namespace as a parameter, so instead of const t = await fetchT(interactionOrMessage, { ns: "ping" }); i have to specify the namespace in every following useage with t("ping:pong") for example, which obviously is not a nice solution. Is there a different function that is better suited for such usecases? I would really appreciate if someone could point me in the right direction. Thank you for your help in advance. :thumbsup:
4 Replies
Favna
Favna2mo ago
1. https://www.i18next.com/principles/fallback. As you can see, fallback languages only apply to t-function, not to fetching a language 2. if you want to get all your keys in 1 then use an object structure and get the parent key, that said, the i18next standard is to call t-function for every translated string.
Julian
JulianOP2mo ago
Ahh i see, then i'll just write my own handler for this. Okay, so something like this: https://react.i18next.com/latest/usetranslation-hook#loading-namespaces is not easily possible with sapphire and the standard is to have the whole path in every call?
Favna
Favna2mo ago
it should be since react-i18next is just a wrapper around i18next idk the syntax equivalent though either way the structure is folder/filename:keyname and that should suffice. File names are technically namespaces here and that's how they are registered at i18next and how i18next handles namespaces I would recommend actually not reading the react specific docs when not writing react specific code
Julian
JulianOP2mo ago
Okay, after looking into code examples i think i'll orient it a bit on how skyra does it. Even tho it's quite a bit more code it seems to provide more type safety. The reason i was referencing the react i18next docs is just because i am working with this in a different project and i like the way they handle the namespace loading with the useTranslation hook, but yeah i agree it doesn't make sense to read docs in the context of a discord bot. Thank you for your help :GO_owoYay:

Did you find this page helpful?