Skip to content

Commit 6a338e7

Browse files
committed
fix/style/docs: new version, improve docs, fix codestyle
1 parent afc2300 commit 6a338e7

File tree

8 files changed

+2271
-244
lines changed

8 files changed

+2271
-244
lines changed

docs/en/i18n_locales.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# pyEchoNext / i18n, l10n - localization and internationalization
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+
l10n - localization, that is, the process of taking into account culture and the rules for writing dates, monetary amounts, and numbers.
9+
10+
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.
11+
12+
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.
13+
14+
The 18 in i18n stands for the number of letters between the first letter i and the last letter n in the word "internationalization".
15+
16+
In pyEchoNext this is implemented like this: a Response is created, if you need to use i18n for localization, use the use_i18n flag:
17+
18+
```python
19+
from pyechonext.response import Request, Response
20+
21+
# create a request...
22+
# request = ...
23+
24+
# your view or route
25+
response = Response(request, body='title', use_i18n=True)
26+
```
27+
28+
This will signal to the application that i18n should be used in this Response. And the application will translate your phrase.
29+
30+
You also don't have to return response:
31+
32+
```python
33+
return echonext.i18n_loader.get_string('title')
34+
```
35+
36+
And for l10n you can only return content (not Response):
37+
38+
```python
39+
return echonext.l10n_loader.format_currency(1305.50)
40+
```
41+
42+
Echonext has a public object i18n_loader, which is the localization loader. The get_string method gets a string from the localization dictionary. You can see how to create and read them in the “Creating localizations” section below this one.
43+
44+
You can also use formatting via Response:
45+
46+
```python
47+
return Response(request, body="title %{name}", use_i18n=True, name='Localization site')
48+
```
49+
50+
> The following arguments are NOT ALLOWED (they are taken): `request: Request, use_i18n: bool = False, status_code: Optional[int] = 200, body: Optional[str] = None, headers: Optional[Dict[str, str]] = {}, content_type: Optional[str] = None, charset: Optional[str] = None, **kwargs`
51+
52+
But for localization, we recommend returning Content rather than Response, especially if you need to use the titles above.
53+
54+
And you can just use formatting directly:
55+
56+
```python
57+
return echonext.i18n_loader.get_string('title %{name}', name='Localization Site')
58+
```
59+
60+
## Creating localizations
61+
You pass the required Settings parameter to the application. It has two fields related to localization:
62+
63+
+ `LOCALE: str = "DEFAULT"`
64+
+ `LOCALE_DIR: str = None`
65+
66+
By default they create the default locale. It looks like this for i18n:
67+
68+
```python
69+
DEFAULT_LOCALE = {
70+
"title": "pyEchoNext Example Website",
71+
"description": "This web application is an example of the pyEchonext web framework.",
72+
}
73+
```
74+
75+
And so for l10n:
76+
77+
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.”.
78+
79+
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:
80+
81+
```json
82+
{
83+
"i18n": {
84+
"title": "pyEchoNext Web application with locale",
85+
"example one": "example one"
86+
},
87+
"l10n": {
88+
"date_format": "%Y-%m-%d",
89+
"time_format": "%H:%M",
90+
"date_time_fromat": "%Y-%m-%d %H:%M",
91+
"thousands_separator": ",",
92+
"decimal_separator": ".",
93+
"currency_symbol": "$",
94+
"currency_format": "{symbol}{amount}"
95+
}
96+
}
97+
```
98+
99+
And already in Settings we specify the following settings:
100+
101+
+ `LOCAL = "RU_RU"`
102+
+ `LOCALE_DIR = "locales"`
103+
104+
Or through the settings loader (in this example, through the python module, you can see [how to use the settings loader](./webapp_creation.md)):
105+
106+
```python
107+
from pyechonext.config import SettingsLoader, SettingsConfigType
108+
109+
config_loader = SettingsLoader(SettingsConfigType.PYMODULE, 'el_config.py')
110+
settings = config_loader.get_settings()
111+
echonext = EchoNext(
112+
__name__,
113+
settings,
114+
middlewares,
115+
urls=url_patterns,
116+
application_type=ApplicationType.HTML,
117+
)
118+
```
119+
120+
el_config.py:
121+
122+
```python
123+
import os
124+
125+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
126+
TEMPLATES_DIR = 'templates'
127+
SECRET_KEY = 'secret-key'
128+
LOCAL = 'RU_RU'
129+
LOCALE_DIR = 'locales'
130+
```
131+
132+
The LOCALE value must be the same as the file name. If RU_RU, then the file should be RU_RU.json.
133+
134+
And now you can introduce internationalization to your application!
135+
136+
> 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.
137+
138+
---
139+
140+
[Contents](./index.md)

docs/en/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010

1111
## Additional materials
1212

13+
+ [How to create your own web framework in Python](./article1.md)
1314
+ [ASGI Documentation](https://asgi.readthedocs.io/_/downloads/en/stable/pdf/)
1415
+ [PEP 333](https://peps.python.org/pep-0333/#the-application-framework-side)

0 commit comments

Comments
 (0)