Translate Strings

This tutorial will show you how to get translated string using TerraCool.

1.- Create the xml locale file
First you need a xml locale file where translated strings will be located.
You can do that by hand (see xml file howto) or you can use TerraCoolUtil.exe that creates a template for you from your source files (see create, update and merge xml file howto).
Once created, this file must be located under locale/locale_code directory (e.g.: ./locale/es-PA) in order to TerraCool can create the locale catalog.

2.- Use TerraCool in your code
The only library you need in order to get translated strings from TerraCool is TerraCool.dll, so a reference to this library must be set first.

Using namespace statement:

using Pcw.TerraCool;

 TerraCool provides two functions to get translated strings, GetString and GetPluralString. These functions can be called from a created Pcw.TerraCool.Catalog class object or using the static class TerraCool.

Catalog catalog = new Catalog("TerraCoolTest", Environment.CurrentDirectory);
Console.WriteLine(catalog.GetString("Hello world!"));

   This creates a Catalog object called "TerraCoolTest", which is the TerraCoolTest.xml file located in  "Environment.CurrentDirectory/locale/es/". If the string is translated, GetString() will return "Hola mundo!" (Spanish locale 'es')

   Same result can be get using the static implementation:

TerraCool.Init("TerraCoolTest", Environment.CurrentDirectory);
Console.WriteLine(TerraCool.GetString("Hello wordld!"));

   Note that Init() must be called first in order to create the catalog.

   In a same way, TerraCool can get translated plural strings:


void RunPlural()
  int n = 1;
  while(n < 3) 
    string result;
    result = TerraCool.GetPluralString("Hello world!", "Hello worlds!", n);


Hola mundo! // n=1
Hola mundos! // n=2

Last edited Sep 5, 2012 at 7:23 PM by formateli, version 25


No comments yet.