Skip to content

Commit

Permalink
feat: add POEditor option to ignore empty definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Feb 9, 2024
1 parent d7a0843 commit 3dae93a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -6,6 +6,8 @@ v3.1.4

*Release date: In development*

- Add `ignore_empty` optional parameter to POEditor configuration to ignore empty translations

v3.1.3
------

Expand Down
49 changes: 49 additions & 0 deletions toolium/test/utils/test_dataset_map_param.py
Expand Up @@ -212,6 +212,22 @@ def test_a_poe_param_single_result():
assert result == expected


def test_a_poe_param_with_empty_definition_single_result():
"""
Verification of a POE mapped parameter with empty definition
"""
dataset.poeditor_terms = [
{
"term": "Poniendo mute",
"definition": "",
"reference": "home:home.tv.mute",
}
]
result = map_param('[POE:home.tv.mute]')
expected = ""
assert result == expected


def test_a_poe_param_no_result_assertion():
"""
Verification of a POE mapped parameter without result
Expand All @@ -228,6 +244,39 @@ def test_a_poe_param_no_result_assertion():
assert "No translations found in POEditor for reference home.tv.off" in str(excinfo.value)


def test_a_poe_param_with_no_definition_no_result_assertion_():
"""
Verification of a POE mapped parameter without definition and without result
"""
dataset.poeditor_terms = [
{
"term": "Poniendo mute",
"definition": None,
"reference": "home:home.tv.mute",
}
]
with pytest.raises(Exception) as excinfo:
map_param('[POE:home.tv.mute]')
assert "No translations found in POEditor for reference home.tv.mute" in str(excinfo.value)


def test_a_poe_param_with_empty_definition_no_result_assertion():
"""
Verification of a POE mapped parameter with empty definition and without result (configured ignore_empty)
"""
dataset.project_config = {'poeditor': {'key_field': 'reference', 'search_type': 'contains', 'ignore_empty': True}}
dataset.poeditor_terms = [
{
"term": "Poniendo mute",
"definition": "",
"reference": "home:home.tv.mute",
}
]
with pytest.raises(Exception) as excinfo:
map_param('[POE:home.tv.mute]')
assert "No translations found in POEditor for reference home.tv.mute" in str(excinfo.value)


def test_a_poe_param_prefix_with_no_definition():
"""
Verification of a POE mapped parameter with a single result for a reference
Expand Down
6 changes: 4 additions & 2 deletions toolium/utils/dataset.py
Expand Up @@ -690,6 +690,8 @@ def get_translation_by_poeditor_reference(reference, poeditor_terms):
poeditor_config = project_config['poeditor'] if project_config and 'poeditor' in project_config else {}
key = poeditor_config['key_field'] if 'key_field' in poeditor_config else 'reference'
search_type = poeditor_config['search_type'] if 'search_type' in poeditor_config else 'contains'
ignore_empty = poeditor_config['ignore_empty'] if 'ignore_empty' in poeditor_config else False
ignored_definitions = [None, ''] if ignore_empty else [None]
# Get POEditor prefixes and add no prefix option
poeditor_prefixes = poeditor_config['prefixes'] if 'prefixes' in poeditor_config else []
poeditor_prefixes.append('')
Expand All @@ -702,10 +704,10 @@ def get_translation_by_poeditor_reference(reference, poeditor_terms):
complete_reference = '%s%s' % (prefix, reference)
if search_type == 'exact':
translation = [term['definition'] for term in poeditor_terms
if complete_reference == term[key] and term['definition'] is not None]
if complete_reference == term[key] and term['definition'] not in ignored_definitions]
else:
translation = [term['definition'] for term in poeditor_terms
if complete_reference in term[key] and term['definition'] is not None]
if complete_reference in term[key] and term['definition'] not in ignored_definitions]
if len(translation) > 0:
break
assert len(translation) > 0, 'No translations found in POEditor for reference %s' % reference
Expand Down
1 change: 1 addition & 0 deletions toolium/utils/poeditor.py
Expand Up @@ -46,6 +46,7 @@
"prefixes": [],
"key_field": "reference",
"search_type": "contains",
"ignore_empty": False,
"file_path": "output/poeditor_terms.json",
"mode": "online"
}
Expand Down

0 comments on commit 3dae93a

Please sign in to comment.