Supporting Multiple Languages

  • 67 favourites

Attached Files

The following files have been attached to this tutorial:

.capx

localisation.capx

Download now 132.85 KB
.capx

localisation-json.capx

Download now 301.54 KB

Stats

3,868 visits, 6,727 views

Translations

This tutorial hasn't been translated.

Tools

Here is a method for supporting multiple languages using XML and project files.

[Note: XML doesn't work on CocoonJS - there is a capx using JSON instead on the left]

XML

Each language will have it’s own file called strings.xx.xml where xx is the two letter ISO 639-1 code for that language.

eg.

strings.en.xml

strings.de.xml

strings.fr.xml

The English xml file (strings.en.xml) looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <strings>
       <string id='world'>World</string>
       <string id='level'>Level</string>
       <string id='begin'>Press 'Enter' to begin.</string>
    </strings>

Each piece of text has an ID (world/level/begin) and a value (World/Level/Press ‘Enter’ to begin.)

Other languages will have the same file structure with the same IDs but different values

eg. German (strings.de.xml):

    <?xml version="1.0" encoding="UTF-8"?>
    <strings>
       <string id='world'>Welt</string>
       <string id='level'>Stufe</string>
       <string id='begin'>Drücken Sie die Eingabetaste, um zu beginnen.</string>
    </strings>

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”)

Simple as that.

.CAPX

localisation.capx

Download now 132.85 KB
.CAPX

localisation-json.capx

Download now 301.54 KB
  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!