Internationalization
Learn how to localize and translate your API.
Since TurboStarter provides fully featured internationalization out of the box, you can easily localize not only the frontend but also the API layer. This can be useful when you need to fetch localized data from the database or send emails in different languages.
Let's explore possibilities of this feature.
Request-based localization
To get the locale for the current request, you can leverage the localize
middleware:
Inside it, we're setting the locale
variable in the current request context, making it available to the procedure.
Error handling
When handling errors in an internationalized API, you'll want to ensure error messages are properly translated for your users. TurboStarter provides built-in support for localizing error messages using error codes and a special onError
hook.
That's why it's recommended to use error codes instead of direct messages in your throw statements:
The error code will then be used to retrieve the localized message, and the returned response from your API will look like this:
Then, you can either use the returned code to get the localized message in your frontend, or simply use the returned message as is.
Last updated on