Navigation:  ThinBASIC Modules > I18N >

cAppConfig

Previous pageReturn to chapter overviewNext page

 

cI18N class

 

cI18N class is used to create an object variable able to load translations from an XML file.

 

To create a new cI18N object, in you script add something like:

 

Dim MyI18N As New cI18N

 

MyI18N object variable is now ready be be used.

 

 

How to load an XML translation file or buffer

 

Once a MyI18N object variable is created, programmer needs to load XML translation files in this way:

 

MyI18N.Load(sPathToLoadFrom, XMLFilePathTemplate)

 

This will load and parse all XML files loading all keys/value pairs inside an internal hash table structure.

 

Format of the XML configuration file

 

To be used by I18N module, a configuration XML file must be created following few mandatory rules:
 

1.XML file MUST adhere to XML rules. More info at (for example: https://www.w3schools.com/xml/xml_whatis.asp
IMPORTANT: XML files content is case sensitive.
 
2.XML file MUST have a root node called <I18N></I18N>
 
3.inside <I18N></I18N> there can be any sequence of sibling nodes up to 3 levels

 

Example of valid XML configuration file:

 

<?xml version="1.0" encoding="utf-8"?>

<I18N>

 

   <AppVersion>1.0.0</AppVersion>

 

   <AppData>

      <Version>1.0</Version>

      <Author_eMail>support@thinbasic.com</Author_eMail>

      <Author_WebSite>http://www.thinbasic.com</Author_WebSite>

   </AppData>

 

   <AppFiles>

      <File1>NameFile1.txt</File1>

      <File2>NameFile2.txt</File2>

   </AppFiles>

 

   <Buffers>

      <Buffer1><![CDATA[some stuff]]></Buffer1> <!-- Comment -->

   </Buffers>

</I18N>

 

How to check if parsing of XML file generated errors

 

Once file is loaded, it is better to check if any error occurred during parsing.

To achieve this, just use something like the following:

 

if MyI18N.ErrorPresent Then

  '---Some error occurred

  printl "Error code", MyI18N.ErrorCode

  printl "Error description", MyI18N.ErrorDescription

Else

  '---No errors, we can go on

end if

 

How to get values from parsed XML configuration file

 

Once file is loaded and parsed, all data inside XML file is stored into an hash table with key/data pairs.

The key under which data is stored is composed by a concatenation of nodes and sibling nodes separated by \

To get values use GetParam method passing the concatenation of the nodes separated by \

Like the following example:

 

if MyI18N.ErrorPresent Then

  '---Some error occurred

  printl "Error code", MyI18N.ErrorCode

  printl "Error description", MyI18N.ErrorDescription

Else

  '---No errors, we can go on

  printl MyI18N.GetKey("AppVersion")

  printl MyI18N.GetKey("AppData\Version")

  printl MyI18N.GetKey("AppFiles\File1")

  printl MyI18N.GetKey("AppFiles\File2")

  printl MyI18N.GetKey("Buffers\Buffer1")

end if