Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m-aciek/attributivegettext
Extension of Python gettext that lets you use contexts as attributes in translation strings.
https://github.com/m-aciek/attributivegettext
context-translation languages nouns python-gettext translation-strings
Last synced: 5 days ago
JSON representation
Extension of Python gettext that lets you use contexts as attributes in translation strings.
- Host: GitHub
- URL: https://github.com/m-aciek/attributivegettext
- Owner: m-aciek
- License: mit
- Created: 2020-04-20T18:30:43.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-02-16T00:32:51.000Z (9 months ago)
- Last Synced: 2024-10-08T18:42:41.502Z (about 1 month ago)
- Topics: context-translation, languages, nouns, python-gettext, translation-strings
- Language: Python
- Homepage:
- Size: 22.5 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Extension of Python Gettext that gives the power to describe and use conjunctions to translators.
It makes `gettext()` function return object which
behave as string with translation, except you can access [context translations](https://docs.python.org/3/library/gettext.html#gettext.pgettext)
through its attributes.Let's say your translation file looks as below:
msgid "user"
msgstr "użytkownik"msgctxt "accusative"
msgid "user"
msgstr "użytkownika"With ``AttributiveTranslation`` class you can access ``accusative`` context
translation through attribute of no-context translation:>>> user = AttributiveTranslations(…).gettext('user')
>>> user
'użytkownik'
>>> user.accusative
'użytkownika'
"OK, and what's cool about that?"Format string syntax introduced in Python 3 allows accessing arguments'
attributes in format strings. Therefore following is possible:>>> 'Wybierz {name.accusative} do zmiany'.format(name=user)
'Wybierz użytkownika do zmiany'We are able to parametrize translation strings with e.g. nouns, which then can
change grammatical cases in translation. Above example being a gettext
translation:msgid "Select {name} to change"
msgstr "Wybierz {name.accusative} do zmiany"Some of the languages that use grammatical cases for nouns are: Armenian,
Assamese, most Balto-Slavic languages, Basque, most Caucasian languages, most
Dravidian languages, German, Icelandic, Japanese, Korean, Latin, Sanskrit,
Tibetan, the Turkic languages and the Uralic languages.
#### FallbackOK, but let's say we miss a context translation:
msgid "user"
msgstr "użytkownik"msgctxt "accusative"
msgid "user"
msgstr "" # <-- missing translation``AttributiveTranslation`` class by default falls back to no-context
translation of the original English string:>>> user = AttributiveTranslations(…).gettext('user')
>>> 'Wybierz {name.accusative} do zmiany'.format(name=user)
'Wybierz użytkownik do zmiany'### Example installation
from gettext import translation
from translations import AttributiveTranslationspl = translation('messages', 'locale', ['pl'], AttributiveTranslations)
pl.install(('pgettext',))### Example usage
Code (installation of translation omitted):
user = _('user')
group = _('group')selected = []
for o in (user, group):
print(_('Select {name} to change').format(name=o))
selected.append(input(f'{o.title()}: '))
With translation file:msgid "user"
msgstr "użytkownik"msgctxt "accusative"
msgid "user"
msgstr "użytkownika"msgid "group"
msgstr "grupa"msgctxt "accusative"
msgid "group"
msgstr "grupę"msgid "Select {name} to change"
msgstr "Wybierz {name.accusative} do zmiany"Will produce:
Wybierz użytkownika do zmiany
Użytkownik: …
Wybierz grupę do zmiany
Grupa: …