Internationalization

See the internationalization' concepts for details.

Because an API may need to send different data based on the language of the user, Strapi provides a built-in strategy to handle the internationalization (i18n).

Usage

The i18n method that will allow you to retrieve the right string based on the language is accessible through the request's context.

There are many strategies to define the language that the server should use to return the correct translation. It can be based on the locale query parameter, the cookie or the Accept-Language header.

  • Query: Add the locale parameter in the URL GET /hello/John?locale=en_US.
  • Cookie: Set the locale field in the cookie locale=en\-US;.
  • Header: Set the Accept-Language header with the value en_US.

Please refer to the language configuration

Example

Let's say we want to say Hello John in english and Bonjour Tom in french. We need to use the built-in i18n feature and replace the string based on the received name.

Path — ./api/hello/config/routes.json.

{
  "routes": [
    {
      "method": "GET",
      "path": "/hello/:name",
      "handler": "Hello.sayHello"
    }
  ]
}

Path — ./api/hello/controllers/Hello.js.

module.exports = {
  // GET /hello/:name
  sayHello: async (ctx) => {
    ctx.send(ctx.i18n.__('Hello %s', ctx.params.name));
  }
};

You need to define the english and french translation for this key.

Path — ./config/locales/en_US.json.

{
  "Hello %s": "Hello %s"
}

Path — ./config/locales/fr_FR.json.

{
  "Hello %s": "Bonjour %s"
}

That's all! The request GET /hello/John?locale=en_US will return Hello John and GET /hello/Tom?locale=fr_FR will return Bonjour Tom.

Content Internationalization

Translating content from a language to another has been requested by many of you. As you may have seen on our website, the Internationalization plugin is not available yet because we need to go out of alpha before developing new plugins.

But, no worries, we have a good work around to help you internationalize your content!

The solution is simple: suffix your fields.

For example if you are building a blog with posts, you may have a Content Type post with two fields: title and content. To make them available in english and french for example, simply replace them by title_en, title_fr, content_en and content_fr.

Then, when you request your API, you will get all these fields in your response payload. If you want to select only some of them (in a specific language) we recommend you to use the GraphQL plugin.

Content Internationalization Strapi