String resources
A PythonPart can optionally be localized. If that should be the case, an additional tag <TextId>
must
be provided in .pyp file (which is explained here).
Besides that, an .xml file with string resources must be build. In this chapter
we are going to explain, how is this file structured and how to access the string resources from a python script.
Local string resources
Each PythonPart can be assigned a file containing the string resources used for the ID's
from the <TextId>
tag. By default, the file name is xxx_xyz.xml, where:
- xxx is the name of the PythonPart .pyp file,
- xyz is the code of the language, which the file is intended for (e.g., deu for the file with German string resources)
If a file with the current country code is not available, the xxx_eng.xml file is used instead. Information about missing string resource files and string resources are displayed in the Allplan trace window.
The .xml file must be located relative to the xxx.pyp file. However, it is also possible
to use another file e.g., a common resource file for several PythonParts. In this case the file name and path must
be defined in the .pyp file in the <LanguageFile>
tag (see here).
File structure
<?xml version="1.0"?>
<Element>
<StringTable>Allplan PythonParts stringtable </StringTable>
<Item>
<TextId>1001</TextId><!--(1)!-->
<Text>rot</Text>
</Item>
<Item>
<TextId>1002</TextId>
<Text>grün</Text>
</Item>
</Element>
- The
<TextId>
must start with 1001!
Include language file
If several PythonParts use the same texts, it might be more effective to define these texts only once
in a general language file and then include this file in the language file of the
individual PythonParts. This can be done using the tag <IncludeLanguageFile>
<?xml version="1.0"?>
<Element>
<StringTable>Allplan PythonParts stringtable</StringTable>
<IncludeLanguageFile>GeneralLanguageFile</IncludeLanguageFile>
<Item>
<TextId>1001</TextId>
<Text>Hallo</Text>
</Item>
...
</Element>
Global string resources
In addition to the local resource file of the PythonPart there is a global resource file with common strings located here:
..\etc\PythonPartsFramework\GeneralScripts\Stringtable\BuildingElement_lang.xml
The existing common string resources can be taken from this file. Unlike local resources, their IDs are strings, and have the format e_XXX e.g., e_LENGTH.
Accessing string resources from script
It is possible to access both local and global string resources directly in the script using the member function get_string_tables(), which returns a tuple containing the local and the global string table as a BuildingElementStringTable object. A string resource can be then accessed using the member function get_string().
graph LR
A[build_ele] --> B{{get_string_tables}};
B --> C[local_str_table];
B --> D[global_str_table];
C --> E{{get_string}};
D --> E;
E --> F[String];
(local_str_table, global_str_table) = build_ele.get_string_tables()
msg_text = local_str_table.get_string("1001", "This is a message") #(1)!
- The second argument is the default string, used in case if the ID is missing in the resource file. In this case a warning would also be printed in the Allplan trace window.
Example
The implementation of the string table usage is described in the example ComboboxLocalisedStringValues located in:
- …\etc\Examples\PythonParts\PaletteExamples\ComboboxLocalisedStringValues.pyp
- …\etc\PythonPartsExampleScripts\PaletteExamples\ComboboxLocalisedStringValues.py