Skip to content

Commit 4a52482

Browse files
committed
Add gettext support
1 parent 72a8764 commit 4a52482

File tree

6 files changed

+145
-22
lines changed

6 files changed

+145
-22
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ python:
66
- "3.6"
77
- "3.7"
88

9-
install: pip install tox tox-travis
9+
install:
10+
- make translate
11+
- pip install tox tox-travis
1012

1113
script: tox --recreate
1214

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ include *.rst
22
include *.txt
33
include setup.py
44
include pytest_bdd/templates/*.mak
5+
include pytest_bdd/locales/*/LC_MESSAGES/*.mo

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ PATH := .env/bin:$(PATH)
88
develop: .env
99
pip install -e . -r requirements-testing.txt tox python-coveralls
1010

11-
coverage: develop
11+
translate:
12+
msgfmt pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.po \
13+
-o pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.mo
14+
msgfmt pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.po \
15+
-o pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.mo
16+
17+
coverage: develop translate
1218
coverage run --source=pytest_bdd .env/bin/py.test tests
1319
coverage report -m
1420

pytest_bdd/feature.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,65 @@
2424
"""
2525

2626
from collections import OrderedDict
27-
from os import path as op
27+
from os import path as op, environ
2828
import codecs
2929
import re
3030
import sys
3131
import textwrap
3232

33+
import gettext
3334
import glob2
3435
import six
3536

3637
from . import types
3738
from . import exceptions
3839

40+
BASE_DIR = op.dirname(op.realpath(__file__))
41+
STEP_PARAM_RE = re.compile(r"\<(.+?)\>")
42+
COMMENT_RE = re.compile(r'(^|(?<=\s))#')
3943

4044
# Global features dictionary
4145
features = {}
46+
_step_prefixes = None
4247

4348

44-
STEP_PREFIXES = [
45-
("Feature: ", types.FEATURE),
46-
("Scenario Outline: ", types.SCENARIO_OUTLINE),
47-
("Examples: Vertical", types.EXAMPLES_VERTICAL),
48-
("Examples:", types.EXAMPLES),
49-
("Scenario: ", types.SCENARIO),
50-
("Background:", types.BACKGROUND),
51-
("Given ", types.GIVEN),
52-
("When ", types.WHEN),
53-
("Then ", types.THEN),
54-
("@", types.TAG),
55-
# Continuation of the previously mentioned step type
56-
("And ", None),
57-
("But ", None),
58-
]
49+
def set_step_prefixes(languages=None):
50+
global _step_prefixes
5951

60-
STEP_PARAM_RE = re.compile(r"\<(.+?)\>")
61-
COMMENT_RE = re.compile(r'(^|(?<=\s))#')
52+
if _step_prefixes is None or languages:
53+
if languages is None:
54+
languages = environ.get('PYTEST_BDD_LANGS', 'en')
55+
56+
_ = gettext.translation(
57+
'step-prefixes',
58+
localedir=op.join(BASE_DIR, 'locales'),
59+
languages=languages.split(','),
60+
).gettext
61+
62+
_step_prefixes = [
63+
(_("Feature: "), types.FEATURE),
64+
(_("Scenario Outline: "), types.SCENARIO_OUTLINE),
65+
(_("Examples: Vertical"), types.EXAMPLES_VERTICAL),
66+
(_("Examples:"), types.EXAMPLES),
67+
(_("Scenario: "), types.SCENARIO),
68+
(_("Background:"), types.BACKGROUND),
69+
(_("Given "), types.GIVEN),
70+
(_("When "), types.WHEN),
71+
(_("Then "), types.THEN),
72+
("@", types.TAG),
73+
# Continuation of the previously mentioned step type
74+
(_("And "), None),
75+
(_("But "), None),
76+
]
77+
78+
return _step_prefixes
79+
80+
81+
set_step_prefixes()
82+
83+
84+
def get_step_prefixes():
85+
return _step_prefixes
6286

6387

6488
def get_step_type(line):
@@ -68,7 +92,7 @@ def get_step_type(line):
6892
6993
:return: SCENARIO, GIVEN, WHEN, THEN, or `None` if can't be detected.
7094
"""
71-
for prefix, _type in STEP_PREFIXES:
95+
for prefix, _type in get_step_prefixes():
7296
if line.startswith(prefix):
7397
return _type
7498

@@ -93,7 +117,7 @@ def parse_line(line):
93117
94118
:return: `tuple` in form ("<prefix>", "<Line without the prefix>").
95119
"""
96-
for prefix, _ in STEP_PREFIXES:
120+
for prefix, _ in get_step_prefixes():
97121
if line.startswith(prefix):
98122
return prefix.strip(), line[len(prefix):].strip()
99123
return "", line
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: pytest_bdd 3.2.0\n"
4+
"POT-Creation-Date: 2019-06-27 14:54+-03\n"
5+
"PO-Revision-Date: 2019-06-27 15:00-0300\n"
6+
"Last-Translator: <[email protected]>\n"
7+
"Language-Team: \n"
8+
"MIME-Version: 1.0\n"
9+
"Content-Type: text/plain; charset=UTF-8\n"
10+
"Content-Transfer-Encoding: 8bit\n"
11+
"Generated-By: pygettext.py 1.5\n"
12+
"Language: en\n"
13+
14+
msgid "Feature: "
15+
msgstr ""
16+
17+
msgid "Scenario Outline: "
18+
msgstr ""
19+
20+
msgid "Examples: Vertical"
21+
msgstr ""
22+
23+
msgid "Examples:"
24+
msgstr ""
25+
26+
msgid "Scenario: "
27+
msgstr ""
28+
29+
msgid "Background:"
30+
msgstr ""
31+
32+
msgid "Given "
33+
msgstr ""
34+
35+
msgid "When "
36+
msgstr ""
37+
38+
msgid "Then "
39+
msgstr ""
40+
41+
msgid "And "
42+
msgstr ""
43+
44+
msgid "But "
45+
msgstr ""
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: pytest_bdd 3.2.0\n"
4+
"POT-Creation-Date: 2019-06-27 14:54+-03\n"
5+
"PO-Revision-Date: 2019-06-27 15:00-0300\n"
6+
"Last-Translator: <[email protected]>\n"
7+
"Language-Team: \n"
8+
"MIME-Version: 1.0\n"
9+
"Content-Type: text/plain; charset=UTF-8\n"
10+
"Content-Transfer-Encoding: 8bit\n"
11+
"Generated-By: pygettext.py 1.5\n"
12+
"Language: pt_BR\n"
13+
14+
msgid "Feature: "
15+
msgstr "Funcionalidade: "
16+
17+
msgid "Scenario Outline: "
18+
msgstr "Esquema do Cenário: "
19+
20+
msgid "Examples: Vertical"
21+
msgstr "Exemplos: Vertical"
22+
23+
msgid "Examples:"
24+
msgstr "Exemplos:"
25+
26+
msgid "Scenario: "
27+
msgstr "Cenário: "
28+
29+
msgid "Background:"
30+
msgstr "Descrição:"
31+
32+
msgid "Given "
33+
msgstr "Dado "
34+
35+
msgid "When "
36+
msgstr "Quando "
37+
38+
msgid "Then "
39+
msgstr "Então "
40+
41+
msgid "And "
42+
msgstr "E "
43+
44+
msgid "But "
45+
msgstr "Mas "

0 commit comments

Comments
 (0)