diff --git a/.travis.yml b/.travis.yml index 26107204..4838f0a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/MANIFEST.in b/MANIFEST.in index 0c280e4d..7e55a08e 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2,3 +2,4 @@ include *.rst include *.txt include setup.py include pytest_bdd/templates/*.mak +include pytest_bdd/locales/*/LC_MESSAGES/*.mo diff --git a/Makefile b/Makefile index 6d1dc196..21b386a1 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pytest_bdd/feature.py b/pytest_bdd/feature.py index e09a4dd3..50abf048 100644 --- a/pytest_bdd/feature.py +++ b/pytest_bdd/feature.py @@ -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): @@ -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 @@ -93,7 +117,7 @@ def parse_line(line): :return: `tuple` in form ("", ""). """ - for prefix, _ in STEP_PREFIXES: + for prefix, _ in get_step_prefixes(): if line.startswith(prefix): return prefix.strip(), line[len(prefix):].strip() return "", line diff --git a/pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.po b/pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.po new file mode 100644 index 00000000..1598d2cc --- /dev/null +++ b/pytest_bdd/locales/en/LC_MESSAGES/step-prefixes.po @@ -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: \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 "" diff --git a/pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.po b/pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.po new file mode 100644 index 00000000..e44ade98 --- /dev/null +++ b/pytest_bdd/locales/pt_BR/LC_MESSAGES/step-prefixes.po @@ -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: \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 "