@@ -158,37 +158,37 @@ Library ${CURDIR}/PluginLib.py plugins=${CURDIR}/MyPlugin.py
158158
159159# Translation
160160
161- PLC supports translation of keywords names and documentation, but arguments names, tags and types
162- can not be currently translated. Translation is provided as a file containing
163- [ Json] ( https://www.json.org/json-en.html ) and as a
164- [ Path] ( https://docs.python.org/3/library/pathlib.html ) object. Translation is provided in
165- ` translation ` argument in the ` HybridCore ` or ` DynamicCore ` ` __init__ ` . Providing translation
166- file is optional, also it is not mandatory to provide translation to all keyword.
167-
168- The keys of json are the methods names, not the keyword names, which implements keyword. Value
169- of key is json object which contains two keys: ` name ` and ` doc ` . ` name ` key contains the keyword
161+ PLC supports translation of keywords names and documentation. Translations must be provided in
162+ the ` translation ` argument in the ` HybridCore ` or ` DynamicCore ` ` __init__ ` , either as a
163+ dictionary or through a [ Path] ( https://docs.python.org/3/library/pathlib.html ) to a
164+ [ JSON] ( https://www.json.org/json-en.html ) file. Providing translation data is optional, also it
165+ is not mandatory to provide translation to all keyword.
166+
167+ The keys of the dictionary are the methods names, not the keyword names, which implements keyword.
168+ Values are objects which contains two keys: ` name ` and ` doc ` . ` name ` key contains the keyword
170169translated name and ` doc ` contains keyword translated documentation. Providing
171- ` doc ` and ` name ` is optional, example translation json file can only provide translations only
172- to keyword names or only to documentatin . But it is always recomended to provide translation to
170+ ` doc ` and ` name ` is optional, i.e. translations data can also provide translations only
171+ to keyword names or only to documentation . But it is always recommended to provide translation to
173172both ` name ` and ` doc ` .
174173
175- Library class documentation and instance documetation has special keys, ` __init__ ` key will
176- replace instance documentation and ` __intro__ ` will replace libary class documentation.
174+ Library class documentation and instance documentation has special keys, ` __init__ ` key will
175+ replace instance documentation and ` __intro__ ` will replace library class documentation.
176+
177+ > [ !NOTE]
178+ > Arguments names, tags and types can not be currently translated.
177179
178180## Example
179181
180182If there is library like this:
181183``` python
182- from pathlib import Path
183-
184184from robotlibcore import DynamicCore, keyword
185185
186186class SmallLibrary (DynamicCore ):
187187 """ Library documentation."""
188188
189- def __init__ (self , translation : Path ):
189+ def __init__ (self ):
190190 """ __init__ documentation."""
191- DynamicCore.__init__ (self , [], translation.absolute() )
191+ DynamicCore.__init__ (self , [])
192192
193193 @keyword (tags = [" tag1" , " tag2" ])
194194 def normal_keyword (self , arg : int , other : str ) -> str :
@@ -212,8 +212,22 @@ class SmallLibrary(DynamicCore):
212212 return some + other
213213```
214214
215- And when there is translation file like:
216- ``` json
215+ And we want to translate it as follows:
216+
217+ - keyword ` normal_keyword ` to ` other_name `
218+ - its documentation to ` This is new doc `
219+ - keyword ` name_changed ` to ` name_changed_again `
220+ - its documentation to ` This is also replaced.\n\nnew line. ` .
221+ - the library constructor documentation to ` Replaces init docs with this one. `
222+ - the library documentation to ` New __intro__ documentation is here. `
223+
224+
225+ ### Provide Translation As File
226+
227+ To provide the translation as a file, simply pass the path to a JSON file containing the translations:
228+
229+ ``` jsonc
230+ // my_translation.json
217231{
218232 " normal_keyword" : {
219233 " name" : " other_name" ,
@@ -230,12 +244,76 @@ And when there is translation file like:
230244 " __intro__" : {
231245 " name" : " __intro__" ,
232246 " doc" : " New __intro__ documentation is here."
233- },
247+ }
234248}
235249```
236- Then ` normal_keyword ` is translated to ` other_name ` . Also this keyword documentions is
237- translted to ` This is new doc ` . The keyword is ` name_changed ` is translted to
238- ` name_changed_again ` keyword and keyword documentation is translted to
239- ` This is also replaced.\n\nnew line. ` . The library class documentation is translated
240- to ` Replaces init docs with this one. ` and class documentation is translted to
241- ` New __intro__ documentation is here. `
250+
251+ ``` python
252+ from pathlib import Path
253+
254+ class SmallLibrary (DynamicCore ):
255+ """ Library documentation."""
256+
257+ def __init__ (self ):
258+ """ __init__ documentation."""
259+ DynamicCore.__init__ (self , [], translation = Path(" /path/to/my_translation.json" ))
260+
261+ # ...
262+ ```
263+
264+ > [ !IMPORTANT]
265+ > Translation files passed as paths must always be in JSON format.
266+
267+ ### Provide Translation As Dictionary
268+
269+ You can also pass the translation data as a dictionary:
270+
271+ ``` python
272+ import json
273+ from pathlib import Path
274+
275+ class SmallLibrary (DynamicCore ):
276+ """ Library documentation."""
277+
278+ def __init__ (self ):
279+ """ __init__ documentation."""
280+ translation_data = json.loads(Path(" /path/to/my_translation.json" ).read_text(encoding = " utf-8" ))
281+ DynamicCore.__init__ (self , [], translation = translation_data)
282+
283+ # ...
284+ ```
285+
286+ This also allows you to use other data formats such as YAML:
287+
288+ ``` yaml
289+ normal_keyword :
290+ name : other_name
291+ doc : This is new doc
292+ name_changed :
293+ name : name_changed_again
294+ doc : |
295+ This is also replaced.
296+
297+ new line.
298+ __init__ :
299+ name : __init__
300+ doc : Replaces init docs with this one.
301+ __intro__ :
302+ name : __intro__
303+ doc : New __intro__ documentation is here.
304+ ` ` `
305+
306+ ` ` ` python
307+ import yaml
308+ from pathlib import Path
309+
310+ class SmallLibrary(DynamicCore) :
311+ " " " Library documentation." " "
312+
313+ def __init__(self, translation_file : Path):
314+ " " " __init__ documentation." " "
315+ translation_data = yaml.safe_load(translation_file.read_text(encoding="utf-8"))
316+ DynamicCore.__init__(self, [], translation=translation_data)
317+
318+ # ...
319+ ```
0 commit comments