Localization
Gravity ships with localization support and two languages (English and Spanish) out of the box. This section will example how to use localization on the server side.
Refer to the client-side localisation section to learn how to translate the UI.
Localization is managed with the i18n package, which can be used to translate individual requests depending on the language provided by the client using the Accept-Language
header.
Locale Files
Locale files are stored inside the /locales
folder, and each language has its own folder of .json
files. The locales are split up based on entities in the application to match the same structure as the models, views and controllers.
All .json
files inside each locale folder are automatically imported and combined using the i18n helper.config()
method, so you can add more JSON files without explicitly importing them anywhere.
The locale files are simple JSON files containing the strings. The keys are always the same (in English) as these are referenced in the code. The string changes depending on the language.
Adding More Locales
Pro tip – ask ChatGPT to translate the JSON files to new languages.
Create a new folder inside /locales eg.
/fr
Add the new locale to the locales array in
/helper/i18n
Performing Translations
Translations can be performed in two ways:
Using the i18n package and setting the locale
Using the
res
object.
You can use the locale
column on the user table when neither option is available, for example, in a background job worker.
Translating Controllers
To translate inside a controller method with the res
object available, use the res.__
method.
Translating Models & Helpers
If the res
object is unavailable - i.e. you're translating a model or helper file, you can pass the req.locale
var to the method.
Handling Plurals
The i18nHelper
has a method for handling plurals for you. You just need to structure your JSON as follows, and pass the translation key and number to the helper.
Emails
Emails are handled slightly differently because the content is stored in the database, not the locale files. Each email in the email
table has a locale
column so the content can be localised. You pass the locale
as part of the data prop to the mail method.
Last updated