Example #1
0
def test_translate_save(filename):
    filepath_in = os.path.join(EXAMPLES_DIR, filename)
    filepath_out = filepath_in + '.expect.md'
    po_filepath = os.path.join(
        os.path.dirname(filepath_in),
        os.path.splitext(os.path.basename(filepath_in))[0] + '.po',
    )

    with tempfile.NamedTemporaryFile(suffix='.po') as save_file:

        pofile_to_markdown(filepath_in, po_filepath, save=save_file.name)

        with open(filepath_out) as expect_file:
            assert save_file.read().decode('utf-8') == expect_file.read()
Example #2
0
def test_context(command, command_aliases, tmp_file):
    markdown_input = f'''<!-- {command} month -->
May

<!-- mdpo-context might -->
May
'''

    markdown_output = 'Mayo\n\nQuizás\n'

    pofile_content = '''#
msgid ""
msgstr ""

msgctxt "month"
msgid "May"
msgstr "Mayo"

msgctxt "might"
msgid "May"
msgstr "Quizás"
'''

    with tmp_file(pofile_content, '.po') as po_filepath:
        output = pofile_to_markdown(
            markdown_input,
            po_filepath,
            command_aliases=command_aliases,
        )
    assert output == markdown_output
Example #3
0
def test_wrapwidth(filename, wrapwidth):
    filepath_in = os.path.join(EXAMPLES_DIR, filename)
    basename = 'inf' if not wrapwidth else wrapwidth
    filepath_out = f'{filepath_in}.{basename}.expect.md'
    po_filepath = os.path.join(
        os.path.dirname(filepath_in),
        os.path.splitext(os.path.basename(filepath_in))[0] + '.po',
    )

    output = pofile_to_markdown(filepath_in, po_filepath, wrapwidth=wrapwidth)

    with open(filepath_out) as f:
        expected_output = f.read()
    assert output == expected_output
Example #4
0
def test_multiple_pofiles_glob():
    pofiles = [
        (
            uuid4().hex + '.po',
            '''#
msgid ""
msgstr ""

msgid "Beyond good intentions, a dictatorship is a dictatorship."
msgstr "Más allá de las buenas intenciones, una dictadura es una dictadura."
''',
        ),
        (
            uuid4().hex + '.po',
            '''#
msgid ""
msgstr ""

msgid "How is it that you think beautiful nerd? Gaaaaaay"
msgstr "¿Cómo es que te parece nerd lo bello? Gaaaaaay"
''',
        ),
    ]

    markdown_content = """
Beyond good intentions, a dictatorship is a dictatorship.

How is it that you think beautiful nerd? Gaaaaaay
"""

    expected_output = """Más allá de las buenas intenciones, una dictadura es una dictadura.

¿Cómo es que te parece nerd lo bello? Gaaaaaay
"""

    with tempfile.TemporaryDirectory() as filesdir:
        for pofile in pofiles:
            with open(os.path.join(filesdir, pofile[0]), 'w') as f:
                f.write(pofile[1])

        pofiles_glob = os.path.join(filesdir, '*.po')

        output = pofile_to_markdown(markdown_content, pofiles_glob)
    assert output == expected_output
Example #5
0
def test_include_fenced_codeblock(tmp_file):
    markdown_input = '''```javascript
var hello = "world";
var this;
```

This must be translated.

```javascript
var thisCodeMustNotBeEdited = undefined;
```
'''

    markdown_output = '''```javascript
var hola = "mundo";
var esto;
```

Esto debe ser traducido.

```javascript
var thisCodeMustNotBeEdited = undefined;
```
'''

    pofile_content = '''#
msgid ""
msgstr ""

msgid ""
"var hello = \\"world\\";\\n"
"var this;\\n"
msgstr ""
"var hola = \\"mundo\\";\\n"
"var esto;\\n"

msgid "This must be translated."
msgstr "Esto debe ser traducido."
'''

    with tmp_file(pofile_content, '.po') as po_filepath:
        output = pofile_to_markdown(markdown_input, po_filepath)
    assert output == markdown_output
Example #6
0
def test_disable_enable(commands, command_aliases, tmp_file):
    disable_command, enable_command = (commands['disable'], commands['enable'])

    markdown_input = f'''This must be translated.

<!-- {disable_command} -->
This must be ignored.

<!-- {enable_command} -->
This must be translated also.
'''

    markdown_output = '''Esto debe ser traducido.

This must be ignored.

Esto también debe ser traducido.
'''

    pofile_content = '''#
msgid ""
msgstr ""

msgid "This must be translated."
msgstr "Esto debe ser traducido."

msgid "This must be ignored."
msgstr "Esto debe ser ignorado."

msgid "This must be translated also."
msgstr "Esto también debe ser traducido."
'''

    with tmp_file(pofile_content, '.po') as po_filepath:
        output = pofile_to_markdown(
            markdown_input,
            po_filepath,
            command_aliases=command_aliases,
        )
    assert output == markdown_output
Example #7
0
def test_translate_markuptext(filename):
    filepath_in = os.path.join(EXAMPLES_DIR, filename)
    filepath_out = filepath_in + '.expect.md'
    po_filepath = os.path.join(
        os.path.dirname(filepath_in),
        os.path.splitext(os.path.basename(filepath_in))[0] + '.po',
    )

    # assert reference PO file content
    pofile = markdown_to_pofile(
        filepath_in,
        location=False,
        po_filepath=po_filepath,
    )

    with open(po_filepath) as f:
        pofile_content = f.read()
    assert str(pofile) == pofile_content

    # assert translation
    output = pofile_to_markdown(filepath_in, po_filepath)
    with open(filepath_out) as f:
        expected_output = f.read()
    assert output == expected_output
Example #8
0
def test_enable_next_block(command, command_aliases, tmp_file):
    markdown_input = f'''This must be translated.

<!-- mdpo-disable -->

This must be ignored.

<!-- {command} -->
This must be translated also.

This must be ignored also.

<!-- mdpo-enable-next-line -->
# This header must be translated

Other line that must be ignored.

<!-- mdpo-enable -->

The last line also must be translated.
'''

    markdown_output = '''Esto debe ser traducido.

This must be ignored.

Esto también debe ser traducido.

This must be ignored also.

# Este encabezado debe ser traducido

Other line that must be ignored.

La última línea también debe ser traducida.
'''

    pofile_content = '''#
msgid ""
msgstr ""

msgid "This must be translated."
msgstr "Esto debe ser traducido."

msgid "This must be ignored."
msgstr "Esto debe ser ignorado."

msgid "This must be translated also."
msgstr "Esto también debe ser traducido."

msgid "This header must be translated"
msgstr "Este encabezado debe ser traducido"

msgid "Other line that must be ignored."
msgstr "Otra línea que debe ser ignorada."

msgid "The last line also must be translated."
msgstr "La última línea también debe ser traducida."
'''

    with tmp_file(pofile_content, '.po') as po_filepath:
        output = pofile_to_markdown(
            markdown_input,
            po_filepath,
            command_aliases=command_aliases,
        )
    assert output == markdown_output
Example #9
0
def test_context_without_value(command, command_aliases):
    # does not raises error, is ignored
    assert pofile_to_markdown(f'<!-- {command} -->', '') == ''
Example #10
0
def test_link_reference_footnotes(tmp_file):
    def process_footnote_references(self, target, href, title):
        if re.match(r'^\^\d', target):
            # footnotes are treated as text blocks, so we don't need to
            # translate them here
            return False

    markdown_content = '''# Hello

Here is a [reference link][foo].

This is a footnote[^1]. This is another[^2].

[^1]: This is a footnote content.

[^2]: This is another footnote content.

[foo]: https://github.com/mondeja/mdpo
'''

    pofile_content = '''#
msgid ""
msgstr ""

msgid "Hello"
msgstr "Hola"

msgid "Here is a [reference link][foo]."
msgstr "Aquí hay un [link referencia][foo]."

msgid "This is a footnote[^1]. This is another[^2]."
msgstr "Esto es una nota al pie[^1]. Esto es otra[^2]."

msgid "[^1]: This is a footnote content."
msgstr "[^1]: Este es un contenido de nota al pie."

msgid "[^2]: This is another footnote content."
msgstr "[^2]: Este es otro contenido de nota al pie."
'''

    with tmp_file(pofile_content, '.po') as po_filepath:
        output = pofile_to_markdown(
            markdown_content,
            po_filepath,
            events={'link_reference': process_footnote_references},
        )

    expected_output = '''# Hola

Aquí hay un [link referencia][foo].

Esto es una nota al pie[^1]. Esto es otra[^2].

[^1]: Este es un contenido de nota al pie.

[^2]: Este es otro contenido de nota al pie.

[foo]: https://github.com/mondeja/mdpo
'''

    assert output == expected_output