| 1 |
# Internationalization |
| 2 |
|
| 3 |
> Supporting internationalization is not optional. It must be a core feature. |
| 4 |
> |
| 5 |
> — [Jannis Leidel, speaking at Django Under the Hood, 2015][cite]. |
| 6 |
|
| 7 |
REST framework ships with translatable error messages. You can make these appear in your language enabling [Django's standard translation mechanisms][django-translation]. |
| 8 |
|
| 9 |
Doing so will allow you to: |
| 10 |
|
| 11 |
* Select a language other than English as the default, using the standard `LANGUAGE_CODE` Django setting. |
| 12 |
* Allow clients to choose a language themselves, using the `LocaleMiddleware` included with Django. A typical usage for API clients would be to include an `Accept-Language` request header. |
| 13 |
|
| 14 |
## Enabling internationalized APIs |
| 15 |
|
| 16 |
You can change the default language by using the standard Django `LANGUAGE_CODE` setting: |
| 17 |
|
| 18 |
LANGUAGE_CODE = "es-es" |
| 19 |
|
| 20 |
You can turn on per-request language requests by adding `LocalMiddleware` to your `MIDDLEWARE` setting: |
| 21 |
|
| 22 |
MIDDLEWARE = [ |
| 23 |
... |
| 24 |
'django.middleware.locale.LocaleMiddleware' |
| 25 |
] |
| 26 |
|
| 27 |
When per-request internationalization is enabled, client requests will respect the `Accept-Language` header where possible. For example, let's make a request for an unsupported media type: |
| 28 |
|
| 29 |
**Request** |
| 30 |
|
| 31 |
GET /api/users HTTP/1.1 |
| 32 |
Accept: application/xml |
| 33 |
Accept-Language: es-es |
| 34 |
Host: example.org |
| 35 |
|
| 36 |
**Response** |
| 37 |
|
| 38 |
HTTP/1.0 406 NOT ACCEPTABLE |
| 39 |
|
| 40 |
{"detail": "No se ha podido satisfacer la solicitud de cabecera de Accept."} |
| 41 |
|
| 42 |
REST framework includes these built-in translations both for standard exception cases, and for serializer validation errors. |
| 43 |
|
| 44 |
Note that the translations only apply to the error strings themselves. The format of error messages, and the keys of field names will remain the same. An example `400 Bad Request` response body might look like this: |
| 45 |
|
| 46 |
{"detail": {"username": ["Esse campo deve ser único."]}} |
| 47 |
|
| 48 |
If you want to use different string for parts of the response such as `detail` and `non_field_errors` then you can modify this behavior by using a [custom exception handler][custom-exception-handler]. |
| 49 |
|
| 50 |
#### Specifying the set of supported languages. |
| 51 |
|
| 52 |
By default all available languages will be supported. |
| 53 |
|
| 54 |
If you only wish to support a subset of the available languages, use Django's standard `LANGUAGES` setting: |
| 55 |
|
| 56 |
LANGUAGES = [ |
| 57 |
('de', _('German')), |
| 58 |
('en', _('English')), |
| 59 |
] |
| 60 |
|
| 61 |
## Adding new translations |
| 62 |
|
| 63 |
REST framework translations are managed on GitHub. You can contribute new translation languages or update existing ones |
| 64 |
by following the guidelines in the [Contributing to REST Framework] section and submitting a pull request. |
| 65 |
|
| 66 |
Sometimes you may need to add translation strings to your project locally. You may need to do this if: |
| 67 |
|
| 68 |
* You want to use REST Framework in a language which is not supported by the project. |
| 69 |
* Your project includes custom error messages, which are not part of REST framework's default translation strings. |
| 70 |
|
| 71 |
#### Translating a new language locally |
| 72 |
|
| 73 |
This guide assumes you are already familiar with how to translate a Django app. If you're not, start by reading [Django's translation docs][django-translation]. |
| 74 |
|
| 75 |
If you're translating a new language you'll need to translate the existing REST framework error messages: |
| 76 |
|
| 77 |
1. Make a new folder where you want to store the internationalization resources. Add this path to your [`LOCALE_PATHS`][django-locale-paths] setting. |
| 78 |
|
| 79 |
2. Now create a subfolder for the language you want to translate. The folder should be named using [locale name][django-locale-name] notation. For example: `de`, `pt_BR`, `es_AR`. |
| 80 |
|
| 81 |
3. Now copy the [base translations file][django-po-source] from the REST framework source code into your translations folder. |
| 82 |
|
| 83 |
4. Edit the `django.po` file you've just copied, translating all the error messages. |
| 84 |
|
| 85 |
5. Run `manage.py compilemessages -l pt_BR` to make the translations |
| 86 |
available for Django to use. You should see a message like `processing file django.po in <...>/locale/pt_BR/LC_MESSAGES`. |
| 87 |
|
| 88 |
6. Restart your development server to see the changes take effect. |
| 89 |
|
| 90 |
If you're only translating custom error messages that exist inside your project codebase you don't need to copy the REST framework source `django.po` file into a `LOCALE_PATHS` folder, and can instead simply run Django's standard `makemessages` process. |
| 91 |
|
| 92 |
## How the language is determined |
| 93 |
|
| 94 |
If you want to allow per-request language preferences you'll need to include `django.middleware.locale.LocaleMiddleware` in your `MIDDLEWARE` setting. |
| 95 |
|
| 96 |
You can find more information on how the language preference is determined in the [Django documentation][django-language-preference]. For reference, the method is: |
| 97 |
|
| 98 |
1. First, it looks for the language prefix in the requested URL. |
| 99 |
2. Failing that, it looks for the `LANGUAGE_SESSION_KEY` key in the current user’s session. |
| 100 |
3. Failing that, it looks for a cookie. |
| 101 |
4. Failing that, it looks at the `Accept-Language` HTTP header. |
| 102 |
5. Failing that, it uses the global `LANGUAGE_CODE` setting. |
| 103 |
|
| 104 |
For API clients the most appropriate of these will typically be to use the `Accept-Language` header; Sessions and cookies will not be available unless using session authentication, and generally better practice to prefer an `Accept-Language` header for API clients rather than using language URL prefixes. |
| 105 |
|
| 106 |
[cite]: https://youtu.be/Wa0VfS2q94Y |
| 107 |
[Contributing to REST Framework]: ../community/contributing.md#development |
| 108 |
[django-translation]: https://docs.djangoproject.com/en/stable/topics/i18n/translation |
| 109 |
[custom-exception-handler]: ../api-guide/exceptions.md#custom-exception-handling |
| 110 |
[django-po-source]: https://raw.githubusercontent.com/encode/django-rest-framework/main/rest_framework/locale/en_US/LC_MESSAGES/django.po |
| 111 |
[django-language-preference]: https://docs.djangoproject.com/en/stable/topics/i18n/translation/#how-django-discovers-language-preference |
| 112 |
[django-locale-paths]: https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-LOCALE_PATHS |
| 113 |
[django-locale-name]: https://docs.djangoproject.com/en/stable/topics/i18n/#term-locale-name |