For about 1 month of my waitressing career, without noticing it, I found myself on a daily 16:8 Intermittent fasting diet, meaning I ate for an 8 hour interval during the day (typically from 5pm —…
Although having extra options allows for greater flexibility, maintaining and documenting them is a lot harder now. The documentation can be a nightmare if you are a new user without any prior experience.
The internationalization process in Pyramid mainly relies on translation strings (Unicode objects containing extra information related to translation). Instead of creating each of them individually in your Pyramid application, you should use GNU gettext — part of the Pyramid translation services. For your information, GNU gettext makes use of the following files internally:
The translation workflow usually goes as follows:
At the time of writing, Pyramid comes with support for three different template engines:
This tutorial will walk you through the process of internationalizing your Pyramid application easily using Jinja2. Let’s get it started!
Make sure you have created a new virtual environment. As soon as you activate it, and install the following modules:
Next, run the following command to create a new Jinja2-based Pyramid project. It will generate all the required files and folders for you. I am using myproject as the root folder for this tutorial. Modify it accordingly.
Once you are done with it, change your working directory to the root folder of your new project.
By default, it will include its own scaffold to set up your development files. Continue the setup by running either
or
It will install additional packages for serving your application automatically, based on the configuration in setup.py.
Next, run the following command to start your web application.
Head over to the following URL to access your Pyramid application.
You should see the following user interface.
Find the Jinaj2 template file at the following directory myproject/myproject/templates/mytemplate.jinja2. Open it up and you should notice that ‘Welcome to’ is marked as translatable based on Jinja2 i18n syntax.
Let us add a new paragraph below it. I am going to use the following code:
Save the file once you are happy with it, and go back to your terminal. Run the following command to extract translatable strings from your Jinja2 template.
It will write PO template file to your project. If your project is named myproject, you can find the file at myproject/myproject/locale/myproject.pot.
The next step is to initialize the message catalog files, based on the language supported by your application. Let us say our application is going to support the following languages:
Run the following command to complete the initialization:
The initialization process is meant to be done only once. Subsequently, you have to run the update catalog command instead after each message extraction as follows:
Let us have a look at the PO files and start editing them. It is highly recommended to use a specialized tool, a translation management tool like Phrase, to streamline the translation process. All the translatable strings in your Jinja2 template will be converted into msgid and msgstr pairs. The latter, by default, is an empty string, where you can fill in your translations. Here is an example for the German language.
Once you have completed the translation, run the following command to re-compile the PO files into MO files.
If you happen to see the following output
It will generate its corresponding MO files in the same folder.
Start your application normally with
and open the following URL in your browser:
For German translation, head over to the following URL
Jinja2 i18n has built-in support for pluralization via the following syntax:
Add the code above to your mytemplate.jinja2 file. We are going to pass a variable called day to Jinja2, and it will localize it for us based on the value. To do so, open up myproject/myproject/views.py and modify the function inside it as follows:
Run the following commands to update your message catalogs:
Next, update your PO files with your translations. The pluralized translatable string will have the following syntax instead:
You should place your singular translation in msgstr[0] while plural translation should be under msgstr[1]. Have a look at the following example for the fr locale.
Lastly, all you need to do is to compile your message catalogs.
Restart your application and you should see the following user interface for the fr locale.
It is by design that any Pyramid application must always know which language should be translatable, regardless of the translation files present in the disk. The reasons behind it are as follows:
Then, head over to myproject/myproject/views.py and add a new import at the top of the file.
When you print the language variables, you should get a list.
The easiest method for number formatting is via the Babel module as Pyramid itself does not come with support for number formatting based on different locales. Add the following import at the top of your views.py file.
To sum things up, you can easily internationalize your Pyramid application using Jinja2 Pyramid i18n. It comes with built-in templates that generate locale files automatically for you.
Last but not least, make sure you check out the following articles if you want to know even more about internationalization and localization in Python:
Changes happen in the labor market on a daily basis. There are so many new practices, ideas, strategies and techniques that emerge. It is reasonable to assume that the labor market constantly has to…
I was born in a small mining town called Zvishavane in Zimbabwe a couple of decades ago. I have so many memories of growing up, my fondest ones being about how safe things were back then. We’d sleep…
The living room is one of the most important spaces in any home. It’s a place where family and friends gather to relax, entertain, and socialize. As such, it’s important to create a living room that…