|
| 1 | +# pyEchoNext / i18n - localization |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +pyEchoNext since version 0.5.3 supports i18n (so far in its basic form). |
| 6 | + |
| 7 | +i18n is an abbreviation for internationalization process. |
| 8 | + |
| 9 | +Internationalization is the process of developing an application in which its code is independent of any linguistic and cultural characteristics of the region or country. As a result, the application becomes flexible and can easily adapt to different language and cultural settings. |
| 10 | + |
| 11 | +Internationalization implementation usually begins in the early stages of a project to prepare the product for future localization. During this process, they determine what will change for future locales (for example, text, images) and export this data to external files. |
| 12 | + |
| 13 | +The 18 in i18n stands for the number of letters between the first letter i and the last letter n in the word "internationalization". |
| 14 | + |
| 15 | +In pyEchoNext this is implemented like this: a Response is created, if you need to use i18n for localization, use the use_i18n flag: |
| 16 | + |
| 17 | +```python |
| 18 | +from pyechonext.response import Request, Response |
| 19 | + |
| 20 | +# create a request... |
| 21 | +# request = ... |
| 22 | + |
| 23 | +response = Response(request, body='title', use_i18n=True) |
| 24 | +``` |
| 25 | + |
| 26 | +This will signal to the application that i18n should be used in this Response. And the application will translate your phrase. |
| 27 | + |
| 28 | +## Creating localizations |
| 29 | +You pass the required Settings parameter to the application. It has two fields related to localization: |
| 30 | + |
| 31 | ++ `LOCALE: str = "DEFAULT"` |
| 32 | ++ `LOCALE_DIR: str = None` |
| 33 | + |
| 34 | +By default they create the default locale. It looks like this: |
| 35 | + |
| 36 | +```python |
| 37 | +DEFAULT_LOCALE = { |
| 38 | +"title": "pyEchoNext Example Website", |
| 39 | +"description": "This web application is an example of the pyEchonext web framework.", |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +That is, if we enter only title or only description in response.body, we will end up with the phrase “pyEchoNext Example Website” or “This web application is an example of the pyEchoNext web framework.”. |
| 44 | + |
| 45 | +But how to create your own locales? It's simple. Create a directory with locale files, we recommend locales, and localization json files in it. Let's say RU_RU.json: |
| 46 | + |
| 47 | +```json |
| 48 | +{ |
| 49 | +"title": "pyEchoNext Web application with locale", |
| 50 | +"example one": "example one" |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +And already in Settings we specify the following settings: |
| 55 | + |
| 56 | ++ `LOCAL = "RU_RU"` |
| 57 | ++ `LOCALE_DIR = "locales"` |
| 58 | + |
| 59 | +Or through the settings loader (in this example, through the python module, you can see [how to use the settings loader](./webapp_creation.md)): |
| 60 | + |
| 61 | +```python |
| 62 | +from pyechonext.config import SettingsLoader, SettingsConfigType |
| 63 | + |
| 64 | +config_loader = SettingsLoader(SettingsConfigType.PYMODULE, 'el_config.py') |
| 65 | +settings = config_loader.get_settings() |
| 66 | +echonext = EchoNext( |
| 67 | +__name__, |
| 68 | +settings, |
| 69 | +middlewares, |
| 70 | +urls=url_patterns, |
| 71 | +application_type=ApplicationType.HTML, |
| 72 | +) |
| 73 | +``` |
| 74 | + |
| 75 | +el_config.py: |
| 76 | + |
| 77 | +```python |
| 78 | +import os |
| 79 | + |
| 80 | +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 81 | +TEMPLATES_DIR = 'templates' |
| 82 | +SECRET_KEY = 'secret-key' |
| 83 | +LOCAL = 'RU_RU' |
| 84 | +LOCALE_DIR = 'locales' |
| 85 | +``` |
| 86 | + |
| 87 | +The LOCALE value must be the same as the file name. If RU_RU, then the file should be RU_RU.json. |
| 88 | + |
| 89 | +And now you can introduce internationalization to your application! |
| 90 | + |
| 91 | +> At the time of version 0.5.3 i18n is under development, many features will be added later. The plans include: dividing the site localization into several files, more convenient handling of i18n and the ability to change localization on the fly. We plan to be inspired by [this documentation](https://developer.mozilla.org/ru/docs/Mozilla/Add-ons/WebExtensions/Internationalization), reworking it for our web framework. |
| 92 | +
|
| 93 | +--- |
| 94 | + |
| 95 | +[Contents](./index.md) |
0 commit comments