@@ -22,7 +22,7 @@ To add a new known locale:
22
22
1. Check for `outstanding Weblate pull requests
23
23
<https://github.com/pypi/warehouse/pulls/weblate> `_ and merge them if so.
24
24
2. In a new branch for |pypi/warehouse |_, add the new language identifier to
25
- ``KNOWN_LOCALES `` in |warehouse/i18n/__init__.py |_.
25
+ ``KNOWN_LOCALES `` in |warehouse/i18n/__init__.py |_ and | webpack.plugin.localize.js |_ .
26
26
The value is the locale code, and corresponds to a directory in
27
27
``warehouse/locale ``.
28
28
3. Commit these changes and make a new pull request to |pypi/warehouse |_.
@@ -45,6 +45,8 @@ To add a new known locale:
45
45
.. _pypi/warehouse : https://github.com/pypi/warehouse
46
46
.. |warehouse/i18n/__init__.py | replace :: ``warehouse/i18n/__init__.py ``
47
47
.. _warehouse/i18n/__init__.py : https://github.com/pypi/warehouse/blob/main/warehouse/i18n/__init__.py
48
+ .. |webpack.plugin.localize.js | replace :: ``webpack.plugin.localize.js ``
49
+ .. _webpack.plugin.localize.js : https://github.com/pypi/warehouse/blob/main/webpack.plugin.localize.js
48
50
.. |pypi/infra | replace :: ``pypi/infra ``
49
51
.. _pypi/infra : https://github.com/pypi/infra
50
52
@@ -62,11 +64,23 @@ In Python, given a request context, call :code:`request._(message)` to mark
62
64
from warehouse.i18n import localize as _
63
65
message = _(" Your message here." )
64
66
67
+ In javascript, use :code: `gettext("singular", ...placeholder_values) ` and
68
+ :code: `ngettext("singular", "plural", count, ...placeholder_values) `.
69
+ The function names are important because they need to be recognised by pybabel.
70
+
71
+ .. code-block :: javascript
72
+
73
+ import { gettext , ngettext } from " ../utils/messages-access" ;
74
+ gettext (" Get some fruit" );
75
+ // -> (en) "Get some fruit"
76
+ ngettext (" Yesterday" , " In the past" , numDays);
77
+ // -> (en) numDays is 1: "Yesterday"; numDays is 3: "In the past"
78
+
65
79
66
80
Passing non-translatable values to translated strings
67
81
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68
82
69
- To pass values you don't want to be translated into
83
+ In html, to pass values you don't want to be translated into
70
84
translated strings, define them inside the :code: `{% trans %} ` tag.
71
85
For example, to pass a non-translatable link
72
86
:code: `request.route_path('classifiers') ` into a string, instead of
@@ -86,6 +100,15 @@ Instead, define it inside the :code:`{% trans %}` tag:
86
100
Filter by <a href =" {{ href }}" >classifier</a >
87
101
{% endtrans %}
88
102
103
+ In javascript, use :code: `%1 `, :code: `%2 `, etc as
104
+ placeholders and provide the placeholder values:
105
+
106
+ .. code-block :: javascript
107
+
108
+ import { ngettext } from " ../utils/messages-access" ;
109
+ ngettext (" Yesterday" , " About %1 days ago" , numDays, numDays);
110
+ // -> (en) numDays is 1: "Yesterday"; numDays is 3: "About 3 days ago"
111
+
89
112
90
113
Marking new strings for pluralization
91
114
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -105,6 +128,8 @@ variants of a string, for example:
105
128
106
129
This is not yet directly possible in Python for Warehouse.
107
130
131
+ In javascript, use :code: `ngettext() ` as described above.
132
+
108
133
Marking views as translatable
109
134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
135
0 commit comments