The xml files are added as project files. (Adding project files.)
Changing the Language
.
We will store the current language in a global variable called language. And we’ll add a function called “changeLanguage” which will change the language to whatever value we pass to the function.
eg:
f.Call(“changeLanguage”, “en”) will change the language to English.
The “changeLanguage” function will update the global variable and then load the appropriate file.
Loading the XML File
.
When the AJAX request has completed we load the file (AJAX.LastData) into an XML object. Then we call the “updateText” function to change the text on-screen.
Updating the Text
.
The “updateText” function simply sets the text for all text objects. We get the correct text for the current language by calling the “getText” function and passing it the string ID as a parameter.
You might also want to change the animations on some sprites to match the new language.
Getting the Correct String From the XML File
.
The “getText” function takes a string id (eg. “begin”) and returns the value in the XML file that matches. To get the value from the XML we use the XML.StringValue(XPath) expression.
There is more info about XPath on the XML manual page.
To get the value of the string node with id=’begin’ we use the XPath expression:
“/strings/string—/text()”
“/strings” chooses all the “strings” nodes (there’s only one)
“/strings/string” chooses all the “string” nodes that are children (sub-nodes) of “strings” (3 nodes in this example)
“/strings/string—” chooses the “string” node with attribute ‘id’ equal to ‘begin’
and finally
“/strings/string—/text()” gives us the value at that node
Adding More Languages
To add a new language eg. Dutch, you just copy one of the XML files to strings.nl.xml and change all the values to their Dutch equivalent. And then use
f.Call(“changeLanguage”, “nl”)