sample code for implementing i18n in a fastapi/starlette (or whatever python framework (or whatever python software))
in this implementation we use python's gettext module and locale directories with .po .mo files (like how django does it)
https://docs.python.org/3.12/library/gettext.html
https://docs.python.org/3/library/contextvars.html
https://peps.python.org/pep-0567/
https://docs.djangoproject.com/en/4.2/topics/i18n/translation/
https://github.com/tomwojcik/starlette-context
- django also uses contextvars for translation stuff
https://github.com/django/asgiref/blob/main/asgiref/local.py
0- install gnu gettext to access msgfmt, xgettext, msgmerge tools
1- create/update language .po file
make LANG=fa makemessages2- translate messages in locales/fa/LC_MESSAGES/messages.po
3- compile translation to .mo file
make LANG=fa compilemessagespython app.pycurl -X 'GET' \
'http://127.0.0.1:8000/' \
-H 'accept: application/json' \
-H 'accept-language: fa'
- response should be like this
[
"سلام دنیا",
"بله",
"خیر",
"خاک بر سر اسرائیل از خدا بیخبر :هندونه:"
]