Skip to content

Commit

Permalink
Add gettext support
Browse files Browse the repository at this point in the history
  • Loading branch information
dutradda committed Jul 10, 2019
1 parent 72a8764 commit 4a52482
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ python:
- "3.6"
- "3.7"

install: pip install tox tox-travis
install:
- make translate
- pip install tox tox-travis

script: tox --recreate

Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include *.rst
include *.txt
include setup.py
include pytest_bdd/templates/*.mak
include pytest_bdd/locales/*/LC_MESSAGES/*.mo
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ PATH := .env/bin:$(PATH)
develop: .env
pip install -e . -r requirements-testing.txt tox python-coveralls

coverage: develop
translate:
msgfmt pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.po \
-o pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.mo
msgfmt pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.po \
-o pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.mo

coverage: develop translate
coverage run --source=pytest_bdd .env/bin/py.test tests
coverage report -m

Expand Down
64 changes: 44 additions & 20 deletions pytest_bdd/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,65 @@
"""

from collections import OrderedDict
from os import path as op
from os import path as op, environ
import codecs
import re
import sys
import textwrap

import gettext
import glob2
import six

from . import types
from . import exceptions

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

# Global features dictionary
features = {}
_step_prefixes = None


STEP_PREFIXES = [
("Feature: ", types.FEATURE),
("Scenario Outline: ", types.SCENARIO_OUTLINE),
("Examples: Vertical", types.EXAMPLES_VERTICAL),
("Examples:", types.EXAMPLES),
("Scenario: ", types.SCENARIO),
("Background:", types.BACKGROUND),
("Given ", types.GIVEN),
("When ", types.WHEN),
("Then ", types.THEN),
("@", types.TAG),
# Continuation of the previously mentioned step type
("And ", None),
("But ", None),
]
def set_step_prefixes(languages=None):
global _step_prefixes

STEP_PARAM_RE = re.compile(r"\<(.+?)\>")
COMMENT_RE = re.compile(r'(^|(?<=\s))#')
if _step_prefixes is None or languages:
if languages is None:
languages = environ.get('PYTEST_BDD_LANGS', 'en')

_ = gettext.translation(
'step-prefixes',
localedir=op.join(BASE_DIR, 'locales'),
languages=languages.split(','),
).gettext

_step_prefixes = [
(_("Feature: "), types.FEATURE),
(_("Scenario Outline: "), types.SCENARIO_OUTLINE),
(_("Examples: Vertical"), types.EXAMPLES_VERTICAL),
(_("Examples:"), types.EXAMPLES),
(_("Scenario: "), types.SCENARIO),
(_("Background:"), types.BACKGROUND),
(_("Given "), types.GIVEN),
(_("When "), types.WHEN),
(_("Then "), types.THEN),
("@", types.TAG),
# Continuation of the previously mentioned step type
(_("And "), None),
(_("But "), None),
]

return _step_prefixes


set_step_prefixes()


def get_step_prefixes():
return _step_prefixes


def get_step_type(line):
Expand All @@ -68,7 +92,7 @@ def get_step_type(line):
:return: SCENARIO, GIVEN, WHEN, THEN, or `None` if can't be detected.
"""
for prefix, _type in STEP_PREFIXES:
for prefix, _type in get_step_prefixes():
if line.startswith(prefix):
return _type

Expand All @@ -93,7 +117,7 @@ def parse_line(line):
:return: `tuple` in form ("<prefix>", "<Line without the prefix>").
"""
for prefix, _ in STEP_PREFIXES:
for prefix, _ in get_step_prefixes():
if line.startswith(prefix):
return prefix.strip(), line[len(prefix):].strip()
return "", line
Expand Down
45 changes: 45 additions & 0 deletions pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
msgid ""
msgstr ""
"Project-Id-Version: pytest_bdd 3.2.0\n"
"POT-Creation-Date: 2019-06-27 14:54+-03\n"
"PO-Revision-Date: 2019-06-27 15:00-0300\n"
"Last-Translator: <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"Language: en\n"

msgid "Feature: "
msgstr ""

msgid "Scenario Outline: "
msgstr ""

msgid "Examples: Vertical"
msgstr ""

msgid "Examples:"
msgstr ""

msgid "Scenario: "
msgstr ""

msgid "Background:"
msgstr ""

msgid "Given "
msgstr ""

msgid "When "
msgstr ""

msgid "Then "
msgstr ""

msgid "And "
msgstr ""

msgid "But "
msgstr ""
45 changes: 45 additions & 0 deletions pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
msgid ""
msgstr ""
"Project-Id-Version: pytest_bdd 3.2.0\n"
"POT-Creation-Date: 2019-06-27 14:54+-03\n"
"PO-Revision-Date: 2019-06-27 15:00-0300\n"
"Last-Translator: <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"Language: pt_BR\n"

msgid "Feature: "
msgstr "Funcionalidade: "

msgid "Scenario Outline: "
msgstr "Esquema do Cenário: "

msgid "Examples: Vertical"
msgstr "Exemplos: Vertical"

msgid "Examples:"
msgstr "Exemplos:"

msgid "Scenario: "
msgstr "Cenário: "

msgid "Background:"
msgstr "Descrição:"

msgid "Given "
msgstr "Dado "

msgid "When "
msgstr "Quando "

msgid "Then "
msgstr "Então "

msgid "And "
msgstr "E "

msgid "But "
msgstr "Mas "

0 comments on commit 4a52482

Please sign in to comment.