Update Internationalization.md

This commit is contained in:
JohniFi 2025-03-29 15:03:55 +01:00 committed by GitHub
parent 5b2963fc46
commit c7981a7986
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -117,21 +117,21 @@ find there. You can edit this with a normal text editor but it is easiest if
you use a special po-file editor from the web (search the web for "po editor"
for many free alternatives), for example:
- [gtranslator](https://wiki.gnome.org/Apps/Gtranslator)
- [poeditor](https://poeditor.com/)
- [gtranslator](https://wiki.gnome.org/Apps/Gtranslator)
- [poeditor](https://poeditor.com/)
The concept of translating is simple, it's just a matter of taking the english
strings you find in the `**.po` file and add your language's translation best
strings you find in the `django.po` file and add your language's translation best
you can. Once you are done, run
`evennia compilemessages`
evennia compilemessages
This will compile all languages. Check your language and also check back to your
`.po` file in case the process updated it - you may need to fill in some missing
header fields and should usually note who did the translation.
When you are done, make sure that everyone can benefit from your translation!
Make a PR against Evennia with the updated `**.po` file. Less ideally (if git is
Make a PR against Evennia with the updated `django.po` file. Less ideally (if git is
not your thing) you can also attach it to a new post in our forums.
### Hints on translation
@ -161,3 +161,37 @@ English anyway.
\n(Traceback was logged {timestamp})"
Swedish: "Fel medan cmdset laddades: Ingen cmdset-klass med namn '{classname}' i {path}.
\n(Traceback loggades {timestamp})"
## Marking Strings in Code for Translation
If you modify the Python module code, you can mark strings for translation by passing them to the `gettext()` method. In Evennia, this is usually imported as `_()` for convenience:
```python
from django.utils.translation import gettext as _
string = _("Text to translate")
```
### Formatting Considerations
When using formatted strings, ensure that you pass the "raw" string to `gettext` for translation first and then format the output. Otherwise, placeholders will be replaced before translation occurs, preventing the correct string from being found in the `.po` file.
```python
# incorrect:
string2 = _("Hello {char}!".format(char=caller.name))
# correct:
string2 = _("Hello {char}!").format(char=caller.name)
```
This is also why f-strings don't work with `gettext`:
```python
# will not work
string = _(f"Hello {char}!")
```
However, you can use %-formatting. Its recommended to use named placeholders because the order of placeholders may vary in different translations.
```python
_("Today is %(month)s %(day)s.") % {"month": m, "day": d}
```