Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

bug 1525719: Extract strings from React, use existing translations #5302

Merged
merged 4 commits into from
Mar 18, 2019

Conversation

jwhitlock
Copy link
Contributor

@jwhitlock jwhitlock commented Mar 14, 2019

This builds on PR #5279, creating a new translations domain react that includes all of the javascript strings, as well as the React strings that are very likely to appear in django.

This requires the changes in mozilla-l10n/mdn-l10n@bfc4155, which include an updated .gitignore. This was merged to master with the latest push to production.

make localerefresh now calls the new target make locale-populate-react, which collects existing translations into compendium files, which are used to populate react.po from existing translations, without sending them to Pontoon.

make build-static now calls the new target make compile-react-i18n, which works the same as make compilejsi18n, but for the new react domain. A new Jinja2 command react_i18n returns the URL of these react-domain JS files.

To get things started, run this in the web container:

make locale-populate-react localecompile compile-react-i18n collectstatic
# make localerefresh # Alternatively, run everything

You can then see the translated strings in the React app, such as the French Learn page:

react-fr

@jwhitlock
Copy link
Contributor Author

I fixed a docker-only path in the Makefile, and added the missing make localecompile to the instructions. This is now working locally, and with the latest commit, we'll get translations on staging.

David Flanagan and others added 4 commits March 14, 2019 18:12
This PR changes the Puente settings in kuma/settings/common.py so
that Puente can find and extract gettext() strings in the React-based
code in kuma/javascript/src/

Note that if we actually deploy this, we will cause work for localizers
for things like the React header components that we've been using as
test cases.
During development, the React views need all the strings from the
'javascript' domain, as well as some string that are currently in the
'django' domain. As we move toward production, strings may move as well,
but for now there is a lot of duplication.

Create a new 'react' domain that includes all the 'javascript' domain
strings, as well as React-specific strings, most or all of which are
repeated in the 'django' domain. Combine existing translations into a
compendium to populate the 'react' domain. Load the strings JS using a
new react_i18n function.

This is paired with a change to the locale repo to .gitignore react.po
and compendium files, until we're ready for independent translations.
* Removed ""Try it live!" string
* Add new react.pot for front-end React code, should not appear as
  strings in Pontoon
React translations are currently populated from the Django and
JavaScript translations. Grab the latest when the production image is
built.
Copy link

@davidflanagan davidflanagan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thank you, John. My comments are some questions and musings. Nothing to block you from merging it as it is.

@@ -81,10 +86,19 @@ localeextract:
python manage.py extract
python manage.py merge

locale-populate-react:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to figure out what this code is doing. Consider adding a comment here to explain the concatenation and extraction going on with msgcat and msgmerge.

It would also be useful to explain (because I'm not entirely clear) what the bottom line is here. I'm pretty sure I understand that this means that React components can use any string that is already in use by python or the existing static javascript files, and no new work will be created for translators. I'm not sure what happens if our React code starts using new strings... Will those end up on Pontoon for translation? Or will they just remain untranslated for now?

@@ -81,10 +86,19 @@ localeextract:
python manage.py extract
python manage.py merge

locale-populate-react:
@ mkdir -p locale/compendia

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add an rmdir -rf at the end of this command, then you won't have to add that in the Dockerfile below... It doesn't look like the compendium files get used after they are created, so it is unclear to me that it is worth keeping them around. I suppose you could also use temp files to hold the compendia... But creating a special-purpose directory as you do here seems simpler to me.

for localename in `find locale -mindepth 1 -maxdepth 1 -type d | cut -d/ -f2 | grep -v templates | grep -v compendia`; do \
rm -f locale/compendia/$$localename.compendium; \
msgcat --use-first -o locale/compendia/$$localename.compendium locale/$$localename/LC_MESSAGES/django.po locale/$$localename/LC_MESSAGES/javascript.po; \
rm -f locale/$$localename/LC_MESSAGES/react.po; \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a React component had used a new string, is it possible that this react.po file being deleted here could contain a translation of that new string that does not appear in the compendium? If so, then this deletion might be a problem... Would it work to use this react.po (or a copy of it) instead of /dev/null in the msgmerge line below so that any React-only translations are not lost?

Copy link
Contributor

@escattone escattone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was more of a lesson for me than a review. It forced me to understand much more of our localization set-up/workflow than I did before. This looks great and worked for me locally. I have only one nit, mentioned below. Awesome work, thanks @jwhitlock!

@@ -81,10 +86,19 @@ localeextract:
python manage.py extract
python manage.py merge

locale-populate-react:
@ mkdir -p locale/compendia
for localename in `find locale -mindepth 1 -maxdepth 1 -type d | cut -d/ -f2 | grep -v templates | grep -v compendia`; do \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, this find command returns the locale/.git directory as well, which in turn causes:

msgcat: error while opening "locale/.git/LC_MESSAGES/django.po" for reading: No such file or directory
msgmerge: error while opening "locale/compendia/.git.compendium" for reading: No such file or directory

when I run make locale-populate-react.

I modified the find command like this:

find locale -mindepth 1 -maxdepth 1 -type d -not -path locale/.git

and that got rid of the issue, but you may know a more elegant way to exclude the locale/.git directory.

@jwhitlock
Copy link
Contributor Author

I can add a comment explaining more about this code, and I'll try out moving the directory removal into the command. It may be next week.

I intend this to be a temporary step until the React-based code is used in production. During this beta period, react.po files are omitted in .gitignore, which is how they are omitted from Pontoon. I expect new strings will not be translated, unless we duplicate them in another file used by Django or JavaScript. It shouldn't impact our development much.

When we're ready to ship this in production, I expect that strings will move from the django domain to the react domain, so they won't be duplicated anymore. We may even get to a point where webpack is processing all t.e files, so javascript goes away as well. The future is fuzzy, and I think the transition will be messy. This just gives us some translations to play with during beta,

When we're ready to go live, I expect to run this command one last time to populate react.po files, then check them into Pontoon. Translators may see that there are new strings, but many are already populated. Until then, there won't be any translations specific to the react domain, just borrowed translations.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants