Skip to content

Commit ba6d7bd

Browse files
cofiemdimiketheman
authored
Allow translating from javascript files (#15612)
Co-authored-by: Dustin Ingram <[email protected]> Co-authored-by: Mike Fiedler <[email protected]>
1 parent f46c35f commit ba6d7bd

23 files changed

+1015
-73
lines changed

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ FROM static-deps AS static
2828
# small amount of copying when only `webpack.config.js` is modified.
2929
COPY warehouse/static/ /opt/warehouse/src/warehouse/static/
3030
COPY warehouse/admin/static/ /opt/warehouse/src/warehouse/admin/static/
31+
COPY warehouse/locale/ /opt/warehouse/src/warehouse/locale/
3132
COPY webpack.config.js /opt/warehouse/src/
33+
COPY webpack.plugin.localize.js /opt/warehouse/src/
3234

3335
RUN NODE_ENV=production npm run build
3436

babel.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
encoding = utf-8
44
extensions=warehouse.utils.html:ClientSideIncludeExtension,warehouse.i18n.extensions.TrimmedTranslatableTagsExtension
55
silent=False
6+
[javascript: **.js]
7+
encoding=utf-8
8+
silent=False

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ services:
221221
volumes:
222222
- ./warehouse:/opt/warehouse/src/warehouse:z
223223
- ./webpack.config.js:/opt/warehouse/src/webpack.config.js:z
224+
- ./webpack.plugin.localize.js:/opt/warehouse/src/webpack.plugin.localize.js:z
224225
- ./babel.config.js:/opt/warehouse/src/babel.config.js:z
225226
- ./.stylelintrc.json:/opt/warehouse/src/.stylelintrc.json:z
226227
- ./tests/frontend:/opt/warehouse/src/tests/frontend:z

docs/dev/development/frontend.rst

+22
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,25 @@ One of these blocks provides code syntax highlighting, which can be tested with
151151
reference project provided at `<http://localhost/project/pypi-code-highlighting-demo/>`_
152152
when using development database. Source reStructuredText file is available
153153
`here <https://github.com/evemorgen/pypi-code-highlighting-demo>`_.
154+
155+
156+
Javascript localization support
157+
-------------------------------
158+
159+
Strings in JS can be translated, see the see the :doc:`../translations` docs.
160+
161+
As part of the webpack build,
162+
the translation data for each locale in ``KNOWN_LOCALES``
163+
is placed in |warehouse/static/js/warehouse/utils/messages-access.js|_.
164+
165+
A separate js bundle is generated for each locale,
166+
named like this: ``warehouse.[locale].[contenthash].js``.
167+
168+
The JS bundle to include is selected in |warehouse/templates/base.html|_
169+
using the current :code:`request.localizer.locale_name`.
170+
171+
.. |warehouse/static/js/warehouse/utils/messages-access.js| replace:: ``warehouse/static/js/warehouse/utils/messages-access.js``
172+
.. _warehouse/static/js/warehouse/utils/messages-access.js: https://github.com/pypi/warehouse/blob/main/warehouse/static/js/warehouse/utils/messages-access.js
173+
174+
.. |warehouse/templates/base.html| replace:: ``warehouse/templates/base.html``
175+
.. _warehouse/templates/base.html: https://github.com/pypi/warehouse/blob/main/warehouse/templates/base.html

docs/dev/translations.rst

+27-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To add a new known locale:
2222
1. Check for `outstanding Weblate pull requests
2323
<https://github.com/pypi/warehouse/pulls/weblate>`_ and merge them if so.
2424
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|_.
2626
The value is the locale code, and corresponds to a directory in
2727
``warehouse/locale``.
2828
3. Commit these changes and make a new pull request to |pypi/warehouse|_.
@@ -45,6 +45,8 @@ To add a new known locale:
4545
.. _pypi/warehouse: https://github.com/pypi/warehouse
4646
.. |warehouse/i18n/__init__.py| replace:: ``warehouse/i18n/__init__.py``
4747
.. _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
4850
.. |pypi/infra| replace:: ``pypi/infra``
4951
.. _pypi/infra: https://github.com/pypi/infra
5052

@@ -62,11 +64,23 @@ In Python, given a request context, call :code:`request._(message)` to mark
6264
from warehouse.i18n import localize as _
6365
message = _("Your message here.")
6466
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+
6579
6680
Passing non-translatable values to translated strings
6781
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6882

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
7084
translated strings, define them inside the :code:`{% trans %}` tag.
7185
For example, to pass a non-translatable link
7286
:code:`request.route_path('classifiers')` into a string, instead of
@@ -86,6 +100,15 @@ Instead, define it inside the :code:`{% trans %}` tag:
86100
Filter by <a href="{{ href }}">classifier</a>
87101
{% endtrans %}
88102

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+
89112
90113
Marking new strings for pluralization
91114
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -105,6 +128,8 @@ variants of a string, for example:
105128

106129
This is not yet directly possible in Python for Warehouse.
107130

131+
In javascript, use :code:`ngettext()` as described above.
132+
108133
Marking views as translatable
109134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110135

package-lock.json

+130
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"scripts": {
66
"build": "webpack",
77
"watch": "webpack --watch",
8-
"lint": "eslint 'warehouse/static/js/**' 'warehouse/admin/static/js/**' 'tests/frontend/**' --ignore-pattern 'warehouse/static/js/vendor/**'",
9-
"lint:fix": "eslint 'warehouse/static/js/**' 'warehouse/admin/static/js/**' 'tests/frontend/**' --ignore-pattern 'warehouse/static/js/vendor/**' --fix",
8+
"lint": "eslint 'warehouse/static/js/**' 'warehouse/admin/static/js/**' 'tests/frontend/**' 'webpack.*.js' --ignore-pattern 'warehouse/static/js/vendor/**'",
9+
"lint:fix": "eslint 'warehouse/static/js/**' 'warehouse/admin/static/js/**' 'tests/frontend/**' 'webpack.*.js' --ignore-pattern 'warehouse/static/js/vendor/**' --fix",
1010
"stylelint": "stylelint '**/*.scss' --cache",
1111
"stylelint:fix": "stylelint '**/*.scss' --cache --fix",
1212
"test": "NODE_OPTIONS='$NODE_OPTIONS --experimental-vm-modules' jest --coverage"
@@ -45,6 +45,7 @@
4545
"css-loader": "^7.1.2",
4646
"css-minimizer-webpack-plugin": "^7.0.0",
4747
"eslint": "^8.36.0",
48+
"gettext-parser": "^7.0.1",
4849
"glob": "^10.2.2",
4950
"image-minimizer-webpack-plugin": "^4.0.2",
5051
"jest": "^29.5.0",

0 commit comments

Comments
 (0)