-
Considering a swith from @nuxtjs/i18n. I need to also translate API responses but couldn't find anything about this in the docs. https://i18n.nuxtjs.org/docs/guide/server-side-translations Would $t work out of the box in the server/api folder? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 2 replies
-
Hello! Currently, this functionality isn't available. I'm not entirely sure how it could be utilized or why it's needed in this context. If you could provide an example of how you're trying to use it and explain your specific requirements, I'd be happy to consider what might need to be implemented. |
Beta Was this translation helpful? Give feedback.
-
I'd like to return fully translated error messages from my API endpoints in Nuxt. Whether that's in response to the client making a request, or by third party clients utilising my API. If I'm displaying errors to the user I'd like them to be in their preferred language, and I'm not always in control of the client to be just sending back translation paths. Here is an example of sending back a translated error using Nuxt i18n server side translations: See their docs for more context about how it works: https://i18n.nuxtjs.org/docs/guide/server-side-translations Thanks |
Beta Was this translation helpful? Give feedback.
-
You can simply create an error page that dynamically displays the error description based on the user's locale and the error code. At the moment, I see this task as overly complex and not particularly necessary. Regarding the implementation of this functionality in nuxt-i18n: it seems that for every server request (and possibly any kind of request—this needs testing), a new Nuxt instance is created, and all translations are stored in memory. This behavior can be observed in the code: This explains why the application consumes so much RAM and experiences memory leaks. Such an implementation, created to support unnecessary functionality, results in serious performance issues. I would not recommend using this functionality, as it can cause significant performance problems and memory management issues in production environments. |
Beta Was this translation helpful? Give feedback.
-
Fair enough, I’ll stick with Nuxt i18n for now then as I have third party users that use my API and I don’t have to provide a large error code glossary for them currently. Also I have native apps that don’t update that often so adding new error codes on the backend only wouldn’t work. Good to know about potential performance issues though thanks! |
Beta Was this translation helpful? Give feedback.
-
I added Server-Side Translations in version 1.36.1. I should mention right away that the implementation is quite basic, but it will keep translation files in memory. Additionally, on the server, only translations from the root folder are used; subfolders are ignored. |
Beta Was this translation helpful? Give feedback.
-
Wow thank you so much it looks great! I'll give a migration to nuxt-i18n-micro a go tomorrow!
Edit: just seen that the cache is in a global variable so that will be re-used when it can in serverless environments, nice! 👌 |
Beta Was this translation helpful? Give feedback.
I added Server-Side Translations in version 1.36.1. I should mention right away that the implementation is quite basic, but it will keep translation files in memory. Additionally, on the server, only translations from the root folder are used; subfolders are ignored.
example