Fix missing compilemessages step before deploying to prod (#4363)

* Update readme on internationalization

* Run compilemessages when building production image

* Run compilemessages when deploying to Heroku

* Always keep Heroku post-compile hooks

* Add empty po file for en-US language

* Update instructions for Docker

* Update default po file

* Convert locale readme to markdown and expand a bit the instructions

* Don't compile translations for 3rd party packages

* Use simplified settings & set env variables for compiling translations

* Update README for translations

* Improve metadata for Brazilian Portuguese translations

* Fix condition for django compressor on Heroku

* Fix condition for Django Compressor
This commit is contained in:
Bruno Alla 2023-06-09 11:12:44 +01:00 committed by GitHub
parent 932c7b6145
commit 2e561ed6c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 31 deletions

View File

@ -96,10 +96,6 @@ def remove_heroku_files():
# don't remove the file if we are using travisci but not using heroku # don't remove the file if we are using travisci but not using heroku
continue continue
os.remove(file_name) os.remove(file_name)
remove_heroku_build_hooks()
def remove_heroku_build_hooks():
shutil.rmtree("bin") shutil.rmtree("bin")
@ -444,8 +440,6 @@ def main():
if "{{ cookiecutter.use_heroku }}".lower() == "n": if "{{ cookiecutter.use_heroku }}".lower() == "n":
remove_heroku_files() remove_heroku_files()
elif "{{ cookiecutter.frontend_pipeline }}" != "Django Compressor":
remove_heroku_build_hooks()
if "{{ cookiecutter.use_docker }}".lower() == "n" and "{{ cookiecutter.use_heroku }}".lower() == "n": if "{{ cookiecutter.use_docker }}".lower() == "n" and "{{ cookiecutter.use_heroku }}".lower() == "n":
if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y": if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y":

View File

@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
{%- if cookiecutter.frontend_pipeline == "Django Compressor" %}
compress_enabled() { compress_enabled() {
python << END python << END
@ -19,4 +20,7 @@ if compress_enabled
then then
python manage.py compress python manage.py compress
fi fi
{%- endif %}
python manage.py collectstatic --noinput python manage.py collectstatic --noinput
python manage.py compilemessages -i site-packages

View File

@ -121,4 +121,8 @@ RUN chown django:django ${APP_HOME}
USER django USER django
RUN DATABASE_URL="" \
DJANGO_SETTINGS_MODULE="config.settings.test" \
python manage.py compilemessages
ENTRYPOINT ["/entrypoint"] ENTRYPOINT ["/entrypoint"]

View File

@ -0,0 +1,32 @@
# Translations
Start by configuring the `LANGUAGES` settings in `base.py`, by uncommenting languages you are willing to support. Then, translations strings will be placed in this folder when running:
```bash
{% if cookiecutter.use_docker == 'y' %}docker-compose -f local.yml run --rm django {% endif %}python manage.py makemessages -all --no-location
```
This should generate `django.po` (stands for Portable Object) files under each locale `<locale name>/LC_MESSAGES/django.po`. Each translatable string in the codebase is collected with its `msgid` and need to be translated as `msgstr`, for example:
```po
msgid "users"
msgstr "utilisateurs"
```
Once all translations are done, they need to be compiled into `.mo` files (stands for Machine Object), which are the actual binary files used by the application:
```bash
{% if cookiecutter.use_docker == 'y' %}docker-compose -f local.yml run --rm django {% endif %}python manage.py compilemessages
```
Note that the `.po` files are NOT used by the application directly, so if the `.mo` files are out of dates, the content won't appear as translated even if the `.po` files are up-to-date.
## Production
The production image runs `compilemessages` automatically at build time, so as long as your translated source files (PO) are up-to-date, you're good to go.
## Add a new language
1. Update the [`LANGUAGES` setting](https://docs.djangoproject.com/en/stable/ref/settings/#std-setting-LANGUAGES) to your project's base settings.
2. Create the locale folder for the language next to this file, e.g. `fr_FR` for French. Make sure the case is correct.
3. Run `makemessages` (as instructed above) to generate the PO files for the new language.

View File

@ -1,14 +0,0 @@
Translations
============
Start by configuring `LANGUAGES` at settings, by uncommenting languages you are willing to support.
Translations will be placed in this folder when running:
python manage.py makemessages --all
Then you should edit the .po files providing proper translations and then run the following for compiling the messages:
python manage.py compilemessages
Note: You may need to restart the django server for changes to take effect.

View File

@ -0,0 +1,12 @@
# Translations for the {{ cookiecutter.project_name }} project
# Copyright (C) {% now 'utc', '%Y' %} {{ cookiecutter.author_name }}
# {{ cookiecutter.author_name }} <{{ cookiecutter.email }}>, {% now 'utc', '%Y' %}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: {{ cookiecutter.version }}\n"
"Language: en-US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

View File

@ -1,18 +1,12 @@
# SOME DESCRIPTIVE TITLE. # Translations for the {{ cookiecutter.project_name }} project
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER # Copyright (C) {% now 'utc', '%Y' %} {{ cookiecutter.author_name }}
# This file is distributed under the same license as the PACKAGE package. # {{ cookiecutter.author_name }} <{{ cookiecutter.email }}>, {% now 'utc', '%Y' %}.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
# #
#, fuzzy #, fuzzy
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: {{ cookiecutter.version }}\n"
"Report-Msgid-Bugs-To: \n" "Language: pt-BR\n"
"POT-Creation-Date: 2023-06-04 21:42+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"