Laravel vs Django Translation Handling
Content Index
Let's talk a little about how the translation process works in each of these frameworks.
The first thing I want to say is that, for example, translations in PHP are generally more or less the same as those we do in Laravel. In Django, the logic is also similar: we'll always have a file where we store the key and the corresponding translation.
Differences in syntax
In PHP, the process is a little more straightforward because it's less jargon-intensive. It's basically a JSON file containing the key and value:
resources\lang\es.json
{
"Light": "Claro",
"Dark": "Oscuro",
***
}
resources\lang\en.json
{
"Light": "Light",
"Dark": "Dark",
***
}
In Django, as is often the case in Python, everything is a bit more verbose. It's not complicated, but it is more detailed.
The idea here is to discuss the steps we need to follow in each case to correctly generate translations.
Translations in Django (Python web in general)
In Django, it all starts by scanning the files where we use translation keys.
This applies to both templates and views (which in PHP would be controllers).
- Functions like gettext (which is native to Python) or tags like {% trans %} are used.
In the code, we can also use _('text') to indicate what we want to translate. - The process, however, is more laborious:
- Install the gettext utility.
- Create specific configurations to specify the translation folder.
- Run commands that generate .po files.
- Manually create the locale folder in each application.
- Configure exceptions (for example, ignore files like requirements.txt).
The big advantage here is that Django automatically scans all files and generates the keys for us.
Additionally, the .po files include comments indicating where each key is used, which is very useful for reference.
Translations in Laravel (PHP in general)
In Laravel, the process is much simpler:
- Manually create the resources/lang folder.
- Create the language files (en.json, es.json, etc.).
- Write the keys and values one by one.
Here, the work is manual and somewhat tedious.
We can rely on AI tools like Copilot, Claude, or Gemini to automate it, but in the end, it's still a process of writing translations.
Changing languages is done with simple functions such as:
App::setLocale('es');
Comparison
Laravel (PHP):
- Advantage → Simple and straightforward process.
- Disadvantage → It's manual; you have to enter all the keys by hand.
- Django (Python):
- Advantage → The system automatically scans and generates the keys.
- Disadvantage → It's more laborious, requiring installation of utilities and additional configuration.
Conclusion
Although the process is faster in Laravel, I find Django more robust because it automatically scans and adds references to where each translation is used.
That's why, in this case, I give the point to Django (Python web in general).
I find it a more reliable solution, even if it's a bit more work at first.
I agree to receive announcements of interest about this Blog.
We compare the process for translating applications in Django (Python Web) vs. those we do in Laravel (PHP).