| 1 |
# Test message extraction |
| 2 |
from gettext import ( |
| 3 |
gettext, |
| 4 |
ngettext, |
| 5 |
pgettext, |
| 6 |
npgettext, |
| 7 |
dgettext, |
| 8 |
dngettext, |
| 9 |
dpgettext, |
| 10 |
dnpgettext |
| 11 |
) |
| 12 |
|
| 13 |
_ = gettext |
| 14 |
|
| 15 |
# Empty string |
| 16 |
_("") |
| 17 |
|
| 18 |
# Extra parentheses |
| 19 |
(_("parentheses")) |
| 20 |
((_("parentheses"))) |
| 21 |
_(("parentheses")) |
| 22 |
|
| 23 |
# Multiline strings |
| 24 |
_("Hello, " |
| 25 |
"world!") |
| 26 |
|
| 27 |
_("""Hello, |
| 28 |
multiline! |
| 29 |
""") |
| 30 |
|
| 31 |
# Invalid arguments |
| 32 |
_() |
| 33 |
_(None) |
| 34 |
_(1) |
| 35 |
_(False) |
| 36 |
_(["invalid"]) |
| 37 |
_({"invalid"}) |
| 38 |
_("string"[3]) |
| 39 |
_("string"[:3]) |
| 40 |
_({"string": "foo"}) |
| 41 |
|
| 42 |
# pygettext does not allow keyword arguments, but both xgettext and pybabel do |
| 43 |
_(x="kwargs are not allowed!") |
| 44 |
|
| 45 |
# Unusual, but valid arguments |
| 46 |
_("foo", "bar") |
| 47 |
_("something", x="something else") |
| 48 |
|
| 49 |
# .format() |
| 50 |
_("Hello, {}!").format("world") # valid |
| 51 |
_("Hello, {}!".format("world")) # invalid, but xgettext extracts the first string |
| 52 |
|
| 53 |
# Nested structures |
| 54 |
_("1"), _("2") |
| 55 |
arr = [_("A"), _("B")] |
| 56 |
obj = {'a': _("A"), 'b': _("B")} |
| 57 |
{{{_('set')}}} |
| 58 |
|
| 59 |
|
| 60 |
# Nested functions and classes |
| 61 |
def test(): |
| 62 |
_("nested string") |
| 63 |
[_("nested string")] |
| 64 |
|
| 65 |
|
| 66 |
class Foo: |
| 67 |
def bar(self): |
| 68 |
return _("baz") |
| 69 |
|
| 70 |
|
| 71 |
def bar(x=_('default value')): |
| 72 |
pass |
| 73 |
|
| 74 |
|
| 75 |
def baz(x=[_('default value')]): |
| 76 |
pass |
| 77 |
|
| 78 |
|
| 79 |
# Shadowing _() |
| 80 |
def _(x): |
| 81 |
pass |
| 82 |
|
| 83 |
|
| 84 |
def _(x="don't extract me"): |
| 85 |
pass |
| 86 |
|
| 87 |
|
| 88 |
# Other gettext functions |
| 89 |
gettext("foo") |
| 90 |
ngettext("foo", "foos", 1) |
| 91 |
pgettext("context", "foo") |
| 92 |
npgettext("context", "foo", "foos", 1) |
| 93 |
dgettext("domain", "foo") |
| 94 |
dngettext("domain", "foo", "foos", 1) |
| 95 |
dpgettext("domain", "context", "foo") |
| 96 |
dnpgettext("domain", "context", "foo", "foos", 1) |
| 97 |
|
| 98 |
# Complex arguments |
| 99 |
ngettext("foo", "foos", 42 + (10 - 20)) |
| 100 |
ngettext("foo", "foos", *args) |
| 101 |
ngettext("foo", "foos", **kwargs) |
| 102 |
dgettext(["some", {"complex"}, ("argument",)], "domain foo") |
| 103 |
|
| 104 |
# Invalid calls which are not extracted |
| 105 |
gettext() |
| 106 |
ngettext('foo') |
| 107 |
pgettext('context') |
| 108 |
npgettext('context', 'foo') |
| 109 |
dgettext('domain') |
| 110 |
dngettext('domain', 'foo') |
| 111 |
dpgettext('domain', 'context') |
| 112 |
dnpgettext('domain', 'context', 'foo') |
| 113 |
dgettext(*args, 'foo') |
| 114 |
dpgettext(*args, 'context', 'foo') |
| 115 |
dnpgettext(*args, 'context', 'foo', 'foos') |
| 116 |
|
| 117 |
# f-strings |
| 118 |
f"Hello, {_('world')}!" |
| 119 |
f"Hello, {ngettext('world', 'worlds', 3)}!" |