Skip to content

Commit

Permalink
Fix gettext imports in sample application, use keyword argument for s…
Browse files Browse the repository at this point in the history
…tring formatting (best practice)
  • Loading branch information
Maciej Olko committed Aug 16, 2021
1 parent 99e1658 commit be88d26
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ translation through attribute of no-context translation:
Format string syntax introduced in Python 3 allows accessing arguments'
attributes in format strings. Therefore following is possible:

>>> 'Wybierz {.accusative} do zmiany'.format(user)
>>> '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 {} to change"
msgstr "Wybierz {.accusative} do zmiany"
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
Expand All @@ -55,7 +55,7 @@ OK, but let's say we miss a context translation:
translation of the original English string:

>>> user = AttributiveTranslations(…).gettext('user')
>>> 'Wybierz {.accusative} do zmiany'.format(user)
>>> 'Wybierz {name.accusative} do zmiany'.format(name=user)
'Wybierz użytkownik do zmiany'

### Example installation
Expand All @@ -76,7 +76,7 @@ Code (installation of translation omitted):
list = []

for o in (user, group):
print(_('Select {} to change').format(o))
print(_('Select {name} to change').format(name=o))
list.append(input(f'{o.title()}: '))
With translation file:
Expand All @@ -95,8 +95,8 @@ With translation file:
msgid "group"
msgstr "grupę"

msgid "Select {} to change"
msgstr "Wybierz {.accusative} do zmiany"
msgid "Select {name} to change"
msgstr "Wybierz {name.accusative} do zmiany"

Will produce:

Expand Down
Binary file modified locale/pl/LC_MESSAGES/messages.mo
Binary file not shown.
5 changes: 3 additions & 2 deletions messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ msgid "group"
msgstr "grupę"

#: sample_application.py:17
msgid "Select {} to change"
msgstr "Wybierz {.accusative} do zmiany"
#, python-brace-format
msgid "Select {name} to change"
msgstr "Wybierz {name.accusative} do zmiany"
12 changes: 6 additions & 6 deletions sample_application.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from gettext import translation, gettext as _, pgettext
from gettext import translation
from translations import AttributiveTranslations

pl = translation('messages', 'locale', ['pl'], AttributiveTranslations)
pl.install(('pgettext',))

_ = pl.gettext
pgettext = pl.pgettext

user = _('user')
pgettext('accusative', 'user') # noop

group = _('group')
pgettext('accusative', 'group') # noop

list = []
selected = []

for o in (user, group):
print(_('Select {} to change').format(o))
list.append(input(f'{o.title()}: '))
print(_('Select {name} to change').format(name=o))
selected.append(input(f'{o.title()}: '))

# translation cycle:
# make changes
Expand Down

0 comments on commit be88d26

Please sign in to comment.