Skip to content

Make transltaiton more flaxible #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions atest/SmallLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,44 @@ def __init__(self, translation: Optional[Path] = None):
translation = Path(translation)
logger.warn(translation.absolute())
logger.warn(type(translation))

DynamicCore.__init__(self, [], translation.absolute())

@keyword(tags=["tag1", "tag2"])
def normal_keyword(self, arg: int, other: str) -> str:
"""I have doc

Multiple lines.
Other line.
"""
data = f"{arg} {other}"
print(data)
return data

def not_keyword(self, data: str) -> str:
print(data)
return data

@keyword(name="This Is New Name", tags=["tag1", "tag2"])
def name_changed(self, some: int, other: int) -> int:
"""This one too"""
print(f"{some} {type(some)}, {other} {type(other)}")
return some + other

@keyword
def not_translated(seld, a: int) -> int:
"""This is not replaced."""
print(f"{a} {type(a)}")
return a + 1

@keyword
def doc_not_translated(seld, a: int) -> int:
"""This is not replaced also."""
print(f"{a} {type(a)}")
return a + 1

@keyword
def kw_not_translated(seld, a: int) -> int:
"""This is replaced too but name is not."""
print(f"{a} {type(a)}")
return a + 1
7 changes: 7 additions & 0 deletions atest/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@
"__intro__": {
"name": "__intro__",
"doc": "New __intro__ documentation is here."
},
"doc_not_translated": {
"name": "this_is_replaced"
}
,
"kw_not_translated": {
"doc": "Here is new doc"
}
}
10 changes: 6 additions & 4 deletions src/robotlibcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ def add_library_components(self, library_components: List, translation: Optional
self.attributes[name] = self.attributes[kw_name] = kw

def __get_keyword_name(self, func: Callable, name: str, translation: dict):
if name in translation:
return translation[name]["name"]
if name in translation: # noqa: SIM102
if new_name := translation[name].get("name"):
return new_name
return func.robot_name or name

def __replace_intro_doc(self, translation: dict):
Expand Down Expand Up @@ -235,8 +236,9 @@ def build(cls, function, translation: Optional[dict] = None):

@classmethod
def get_doc(cls, function, translation: dict):
if kw := cls._get_kw_transtation(function, translation):
return kw["doc"]
if kw := cls._get_kw_transtation(function, translation): # noqa: SIM102
if "doc" in kw:
return kw["doc"]
return inspect.getdoc(function) or ""

@classmethod
Expand Down
23 changes: 23 additions & 0 deletions utest/test_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,32 @@ def test_translations_docs(lib: SmallLibrary):
kw = keywords["name_changed_again"]
assert kw.documentation == "This is also replaced.\n\nnew line."


def test_init_and_lib_docs(lib: SmallLibrary):
keywords = lib.keywords_spec
init = keywords["__init__"]
assert init.documentation == "Replaces init docs with this one."
doc = lib.get_keyword_documentation("__intro__")
assert doc == "New __intro__ documentation is here."


def test_not_translated(lib: SmallLibrary):
keywords = lib.keywords_spec
assert "not_translated" in keywords
doc = lib.get_keyword_documentation("not_translated")
assert doc == "This is not replaced."


def test_doc_not_translated(lib: SmallLibrary):
keywords = lib.keywords_spec
assert "doc_not_translated" not in keywords
assert "this_is_replaced" in keywords
doc = lib.get_keyword_documentation("this_is_replaced")
assert doc == "This is not replaced also."


def test_kw_not_translated_but_doc_is(lib: SmallLibrary):
keywords = lib.keywords_spec
assert "kw_not_translated" in keywords
doc = lib.get_keyword_documentation("kw_not_translated")
assert doc == "Here is new doc"