| 1 |
from io import BytesIO |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from babel.messages import extract |
| 6 |
|
| 7 |
|
| 8 |
def test_simple_extract(): |
| 9 |
buf = BytesIO(b"""\ |
| 10 |
msg1 = _('simple') |
| 11 |
msg2 = gettext('simple') |
| 12 |
msg3 = ngettext('s', 'p', 42) |
| 13 |
""") |
| 14 |
messages = \ |
| 15 |
list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS, |
| 16 |
[], {})) |
| 17 |
|
| 18 |
assert messages == [(1, 'simple', [], None), |
| 19 |
(2, 'simple', [], None), |
| 20 |
(3, ('s', 'p'), [], None)] |
| 21 |
|
| 22 |
|
| 23 |
def test_various_calls(): |
| 24 |
buf = BytesIO(b"""\ |
| 25 |
msg1 = _(i18n_arg.replace(/"/, '"')) |
| 26 |
msg2 = ungettext(i18n_arg.replace(/"/, '"'), multi_arg.replace(/"/, '"'), 2) |
| 27 |
msg3 = ungettext("Babel", multi_arg.replace(/"/, '"'), 2) |
| 28 |
msg4 = ungettext(i18n_arg.replace(/"/, '"'), "Babels", 2) |
| 29 |
msg5 = ungettext('bunny', 'bunnies', parseInt(Math.random() * 2 + 1)) |
| 30 |
msg6 = ungettext(arg0, 'bunnies', rparseInt(Math.random() * 2 + 1)) |
| 31 |
msg7 = _(hello.there) |
| 32 |
msg8 = gettext('Rabbit') |
| 33 |
msg9 = dgettext('wiki', model.addPage()) |
| 34 |
msg10 = dngettext(domain, 'Page', 'Pages', 3) |
| 35 |
""") |
| 36 |
messages = \ |
| 37 |
list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS, [], |
| 38 |
{})) |
| 39 |
assert messages == [ |
| 40 |
(5, ('bunny', 'bunnies'), [], None), |
| 41 |
(8, 'Rabbit', [], None), |
| 42 |
(10, ('Page', 'Pages'), [], None), |
| 43 |
] |
| 44 |
|
| 45 |
|
| 46 |
def test_message_with_line_comment(): |
| 47 |
buf = BytesIO("""\ |
| 48 |
// NOTE: hello |
| 49 |
msg = _('Bonjour à tous') |
| 50 |
""".encode('utf-8')) |
| 51 |
messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) |
| 52 |
assert messages[0][2] == 'Bonjour à tous' |
| 53 |
assert messages[0][3] == ['NOTE: hello'] |
| 54 |
|
| 55 |
|
| 56 |
def test_message_with_multiline_comment(): |
| 57 |
buf = BytesIO("""\ |
| 58 |
/* NOTE: hello |
| 59 |
and bonjour |
| 60 |
and servus */ |
| 61 |
msg = _('Bonjour à tous') |
| 62 |
""".encode('utf-8')) |
| 63 |
messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) |
| 64 |
assert messages[0][2] == 'Bonjour à tous' |
| 65 |
assert messages[0][3] == ['NOTE: hello', 'and bonjour', ' and servus'] |
| 66 |
|
| 67 |
|
| 68 |
def test_ignore_function_definitions(): |
| 69 |
buf = BytesIO(b"""\ |
| 70 |
function gettext(value) { |
| 71 |
return translations[language][value] || value; |
| 72 |
}""") |
| 73 |
|
| 74 |
messages = list(extract.extract_javascript(buf, ('gettext',), [], {})) |
| 75 |
assert not messages |
| 76 |
|
| 77 |
|
| 78 |
def test_misplaced_comments(): |
| 79 |
buf = BytesIO(b"""\ |
| 80 |
/* NOTE: this won't show up */ |
| 81 |
foo() |
| 82 |
|
| 83 |
/* NOTE: this will */ |
| 84 |
msg = _('Something') |
| 85 |
|
| 86 |
// NOTE: this will show up |
| 87 |
// too. |
| 88 |
msg = _('Something else') |
| 89 |
|
| 90 |
// NOTE: but this won't |
| 91 |
bar() |
| 92 |
|
| 93 |
_('no comment here') |
| 94 |
""") |
| 95 |
messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) |
| 96 |
assert messages[0][2] == 'Something' |
| 97 |
assert messages[0][3] == ['NOTE: this will'] |
| 98 |
assert messages[1][2] == 'Something else' |
| 99 |
assert messages[1][3] == ['NOTE: this will show up', 'too.'] |
| 100 |
assert messages[2][2] == 'no comment here' |
| 101 |
assert messages[2][3] == [] |
| 102 |
|
| 103 |
|
| 104 |
JSX_SOURCE = b""" |
| 105 |
class Foo { |
| 106 |
render() { |
| 107 |
const value = gettext("hello"); |
| 108 |
return ( |
| 109 |
<option value="val1">{ i18n._('String1') }</option> |
| 110 |
<option value="val2">{ i18n._('String 2') }</option> |
| 111 |
<option value="val3">{ i18n._('String 3') }</option> |
| 112 |
<option value="val4">{ _('String 4') }</option> |
| 113 |
<option>{ _('String 5') }</option> |
| 114 |
); |
| 115 |
} |
| 116 |
""" |
| 117 |
EXPECTED_JSX_MESSAGES = ["hello", "String1", "String 2", "String 3", "String 4", "String 5"] |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.parametrize("jsx_enabled", (False, True)) |
| 121 |
def test_jsx_extraction(jsx_enabled): |
| 122 |
buf = BytesIO(JSX_SOURCE) |
| 123 |
messages = [m[2] for m in extract.extract_javascript(buf, ('_', 'gettext'), [], {"jsx": jsx_enabled})] |
| 124 |
if jsx_enabled: |
| 125 |
assert messages == EXPECTED_JSX_MESSAGES |
| 126 |
else: |
| 127 |
assert messages != EXPECTED_JSX_MESSAGES |
| 128 |
|
| 129 |
|
| 130 |
def test_dotted_keyword_extract(): |
| 131 |
buf = BytesIO(b"msg1 = com.corporate.i18n.formatMessage('Insert coin to continue')") |
| 132 |
messages = list( |
| 133 |
extract.extract('javascript', buf, {"com.corporate.i18n.formatMessage": None}, [], {}), |
| 134 |
) |
| 135 |
|
| 136 |
assert messages == [(1, 'Insert coin to continue', [], None)] |
| 137 |
|
| 138 |
|
| 139 |
def test_template_string_standard_usage(): |
| 140 |
buf = BytesIO(b"msg1 = gettext(`Very template, wow`)") |
| 141 |
messages = list( |
| 142 |
extract.extract('javascript', buf, {"gettext": None}, [], {}), |
| 143 |
) |
| 144 |
|
| 145 |
assert messages == [(1, 'Very template, wow', [], None)] |
| 146 |
|
| 147 |
|
| 148 |
def test_template_string_tag_usage(): |
| 149 |
buf = BytesIO(b"function() { if(foo) msg1 = i18n`Tag template, wow`; }") |
| 150 |
messages = list( |
| 151 |
extract.extract('javascript', buf, {"i18n": None}, [], {}), |
| 152 |
) |
| 153 |
|
| 154 |
assert messages == [(1, 'Tag template, wow', [], None)] |
| 155 |
|
| 156 |
|
| 157 |
def test_inside_template_string(): |
| 158 |
buf = BytesIO(b"const msg = `${gettext('Hello')} ${user.name}`") |
| 159 |
messages = list( |
| 160 |
extract.extract('javascript', buf, {"gettext": None}, [], {'parse_template_string': True}), |
| 161 |
) |
| 162 |
|
| 163 |
assert messages == [(1, 'Hello', [], None)] |
| 164 |
|
| 165 |
|
| 166 |
def test_inside_template_string_with_linebreaks(): |
| 167 |
buf = BytesIO(b"""\ |
| 168 |
const userName = gettext('Username') |
| 169 |
const msg = `${ |
| 170 |
gettext('Hello') |
| 171 |
} ${userName} ${ |
| 172 |
gettext('Are you having a nice day?') |
| 173 |
}` |
| 174 |
const msg2 = `${ |
| 175 |
gettext('Howdy') |
| 176 |
} ${userName} ${ |
| 177 |
gettext('Are you doing ok?') |
| 178 |
}` |
| 179 |
""") |
| 180 |
messages = list( |
| 181 |
extract.extract('javascript', buf, {"gettext": None}, [], {'parse_template_string': True}), |
| 182 |
) |
| 183 |
|
| 184 |
assert messages == [(1, 'Username', [], None), (3, 'Hello', [], None), (5, 'Are you having a nice day?', [], None), (8, 'Howdy', [], None), (10, 'Are you doing ok?', [], None)] |
| 185 |
|
| 186 |
|
| 187 |
def test_inside_nested_template_string(): |
| 188 |
buf = BytesIO(b"const msg = `${gettext('Greetings!')} ${ evening ? `${user.name}: ${gettext('This is a lovely evening.')}` : `${gettext('The day is really nice!')} ${user.name}`}`") |
| 189 |
messages = list( |
| 190 |
extract.extract('javascript', buf, {"gettext": None}, [], {'parse_template_string': True}), |
| 191 |
) |
| 192 |
|
| 193 |
assert messages == [(1, 'Greetings!', [], None), (1, 'This is a lovely evening.', [], None), (1, 'The day is really nice!', [], None)] |