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 language 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 language translation file or buffer

 

Once a MyI18N object variable is created, programmer needs to load XML language 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, an XML language translation 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 nodes like the following example

 

Example of valid XML language translation file:

 

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

<i18n>

 

   <!-->Example using Locale node without any Lang attribute<-->

   <Locale lang="en-GB">

      <Sentence key="Good_Morning">Good morning</Sentence>

      <Sentence key="What_Is_Your_Name">What is your name?</Sentence>

      <Sentence key="My_Name_Is">{1} is my name.</Sentence>

      <Sentence key="HiThere">Hi there</Sentence>

   </Locale>

 

   <Locale lang="fr-FR">

      <Sentence key="Good_Morning">Bonjour</Sentence>

      <Sentence key="What_Is_Your_Name">Quel est votre nom?</Sentence>

      <Sentence key="My_Name_Is">Je m'appelle {1}.</Sentence>

      <Sentence key="HiThere">Salut</Sentence>

   </Locale>

 

   <Locale lang="it-IT">

      <Sentence key="Good_Morning">Buon giorno</Sentence>

      <Sentence key="What_Is_Your_Name">Come ti chiami?</Sentence>

      <Sentence key="My_Name_Is">Mi chiamo {1}.</Sentence>

      <Sentence key="HiThere">Ciao</Sentence>

   </Locale>

 

 

   <!-->Example using direct Sentence node with lang attribute<-->

   <Sentence key="thinBasic" lang="en-GB">thinBasic Programming Language</Sentence>

   <Sentence key="thinBasic" lang="fr-FR">Langage de programmation thinBasic</Sentence>

   <Sentence key="thinBasic" lang="it-IT">Linguaggio di programmazione thinBasic</Sentence>

 

 

   <Sentence key="thinAir">thinAir</Sentence>

 

</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 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 \

 

if MyI18N.ErrorPresent Then

  '---Some error occurred

  printl "Error code", MyI18N.ErrorCode

  printl "Error description", MyI18N.ErrorDescription

Else

  printl MyI18N.GetKey("en-GB\HiThere")

  printl MyI18N.GetKey("it-IT\Good_Morning")

  printl MyI18N.GetKey("fr-FR\What_Is_Your_Name")

  printl StrFormat$(MyI18N.GetKey("en-GB\My_Name_Is"),"thinBasic"

  printl MyI18N.GetKey("thinBasic\en-GB")

  printl MyI18N.GetKey("thinAir")

end if