Skip to content

Commit b5986f4

Browse files
committed
Enhance transtion to discover multiple translation from same Python plugin
Fixes #1917
1 parent c55cb9c commit b5986f4

File tree

6 files changed

+1872
-5
lines changed

6 files changed

+1872
-5
lines changed

src/SeleniumLibrary/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,13 +856,20 @@ def _get_translation(language: Union[str, None]) -> Union[Path, None]:
856856
for _, name, _ in pkgutil.iter_modules()
857857
if name.startswith("robotframework_seleniumlibrary_translation")
858858
}
859+
lang = language.lower()
859860
for plugin in discovered_plugins.values():
860861
try:
861862
data = plugin.get_language()
862863
except AttributeError:
863864
continue
864-
if data.get("language", "").lower() == language.lower() and data.get(
865-
"path"
865+
if (
866+
isinstance(data, dict)
867+
and data.get("language", "").lower() == lang
868+
and data.get("path")
866869
):
867870
return Path(data.get("path")).absolute()
871+
if isinstance(data, list):
872+
for item in data:
873+
if item.get("language", "").lower() == lang and item.get("path"):
874+
return Path(item.get("path")).absolute()
868875
return None

utest/test/robotframework_seleniumlibrary_translation_fi/translate.json

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pathlib import Path
2+
3+
4+
def get_language() -> list:
5+
curr_dir = Path(__file__).parent.absolute()
6+
return [
7+
{
8+
"language": "eng",
9+
"path": curr_dir / "translate1.json"
10+
},
11+
{
12+
"language": "swe",
13+
"path": curr_dir / "translate2.json"
14+
}
15+
]

utest/test/robotframework_seleniumlibrary_translation_list/translate1.json

Lines changed: 917 additions & 0 deletions
Large diffs are not rendered by default.

utest/test/robotframework_seleniumlibrary_translation_list/translate2.json

Lines changed: 917 additions & 0 deletions
Large diffs are not rendered by default.

utest/test/translation/test_translation.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@ def test_translation(sl: SeleniumLibrary):
1717
assert spec.argument_specification
1818
doc: str = spec.documentation
1919
assert doc.startswith(
20-
"1 SeleniumLibrary is a web testing library for Robot Framework"
20+
"00 SeleniumLibrary is a web testing library for Robot Framework"
2121
)
2222

2323
spec = sl.keywords_spec["hallinnoi_hälytys"]
2424
doc: str = spec.documentation
2525
assert doc == "Hallinnoi hälytyksen uusi dokkari\n\nToinen rivi"
26+
27+
28+
def test_provide_translation_as_list(sl: SeleniumLibrary):
29+
lang_plugin = "robotframework_seleniumlibrary_translation_list"
30+
file_path = Path(__file__).parent.parent / lang_plugin / "translate2.json"
31+
received_path = sl._get_translation("swe")
32+
assert received_path == file_path, received_path.relative_to(file_path)
33+
assert sl._get_translation("wrong") is None
34+
received_path = sl._get_translation("Eng")
35+
file_path = Path(__file__).parent.parent / lang_plugin / "translate1.json"
36+
assert received_path == file_path, received_path.relative_to(file_path)

0 commit comments

Comments
 (0)