Repository: robotframework/PythonLibCore
Pull request #142 (2024-03-21 19:42:39)

Make transltaiton more flaxible
#139


Patch:
diff --git a/src/robotlibcore.py b/src/robotlibcore.py
index b42f8e6..f0fe0b3 100644
--- a/src/robotlibcore.py
+++ b/src/robotlibcore.py
@@ -83,8 +83,9 @@ class HybridCore:
                     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):
@@ -235,8 +236,9 @@ class KeywordBuilder:
 
     @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


Test patch:
diff --git a/atest/SmallLibrary.py b/atest/SmallLibrary.py
index 55a9540..e576368 100644
--- a/atest/SmallLibrary.py
+++ b/atest/SmallLibrary.py
@@ -14,26 +14,44 @@ class SmallLibrary(DynamicCore):
             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
diff --git a/atest/translation.json b/atest/translation.json
index 36795c5..dbdab73 100644
--- a/atest/translation.json
+++ b/atest/translation.json
@@ -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"
     }
 }
diff --git a/utest/test_translations.py b/utest/test_translations.py
index a482a52..2d009b0 100644
--- a/utest/test_translations.py
+++ b/utest/test_translations.py
@@ -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"