Skip to main content
Reading a Unicode Multi Language Translation Table         A How To with code for creating, reading, and using a Unicode translation table

STEP 1 - Creating an UTF-16 data file

The first thing you need to do is to create a table in OpenOffice or Notepad. It must be tab-deliniated for this code to work. The table should contain a key value to use in your code in column one and as many languages as you want in the following columns. Each row will contain a different translation. For example:

Copy to clipboard
options_language English Español main_instructions INSTRUCTIONS INSTRUCCIONES main_options OPTIONS OPCIONES main_credits CREDITS CREDITOS


Important note: Save it as UNICODE (encoding = Unicode)!

STEP 2 - Setting up your class

Create a vector of arrays in a class to hold our information.

Create an int variable to hold the current language.
Copy to clipboard
short unsigned int currentLanguage; std::vector<std::vector<Ogre::UTFString>> languageMap; // vector array for each language


There is probably a better way to do this, but at least this works.

STEP 3 - Include several functions

Add these functions to the class:

Copy to clipboard
void loadLanguages(void) { //read in a UTF-16 tab-deliniated language file. ifstream is; is.open("../path/to/my/file.csv"); wstring buffer; Ogre::UTFString word; bool firstLine = true; short unsigned int column = 0; wostringstream woss; if (is.good()) { Ogre::LogManager::getSingleton().logMessage("Reading UTF16 language file."); is.get(); // skip the BOM - i know this is a crappy hack, but users shouldnt be messing with this file. is.get(); std::vector<Ogre::UTFString> firstLanguageVector; // start the first column, following columns will be triggered by tabs languageMap.push_back(firstLanguageVector); while (is.good()) { buffer = is.get(); if (buffer[0]== 0x0009) // this is a tab -> time to prepare for a new element/word. { is.get(); // purge the other half of the tab if (firstLine) // if this is the first line, this means we can use it as a column index so create a vector for each column { std::vector<Ogre::UTFString> newLanguageVector; languageMap.push_back(newLanguageVector); } word = woss.str(); languageMap[column].push_back(word); column ++; woss.str(L""); // clear the string stream } else if (buffer[0]== 0x000d) // this is a carriage return byte, we have reached the first byte of a new line! { word = woss.str(); languageMap[column].push_back(word); is.get(); // skip the other 8 bit half of the carriage return is.get(); // skip the new line byte (0x000A) which is its own 16bits is.get(); column = 0; firstLine = false; woss.str(L""); // clear the string stream } else woss << buffer.c_str(); // half of a character } } else Ogre::LogManager::getSingleton().logMessage("Could not open UTF16 language file."); }

Later you will need to get that information back!

Copy to clipboard
Ogre::UTFString translate(string key) { // convert the name to a wide character so we can look it up in the table (which is wide character format) // using this function override requires that NO elements in the key column (leftmost column) can have any special characters because we are basically just forcing the regular character wide // and not performing a real conversion from UTF8 std::wstring wideKey(key.length(), L' '); // make room for characters std::copy(key.begin(), key.end(), wideKey.begin());// copy string to wstring return translate(wideKey); } Ogre::UTFString translate(wstring key) { Ogre::UTFString output = "ERROR: no GUI key."; short unsigned int size = short unsigned int (languageMap[0].size()); short unsigned int x; for ( x = 0; x < size; x++ ) { if (languageMap[0][x] == key) { if (languageMap[currentLanguage][x].asWStr_c_str()) output = languageMap[currentLanguage][x]; else output = "ERROR: no entry."; } } return output; }

STEP 4 - Finally translate something

To translate something to whatever the current language is, make sure that currentLanguage contains the number of an existing column or it will try to read off the end of the vector!
Here is how to use it:

Copy to clipboard
// will return Ogre::UTFString with "English" if currentLanguage == 1 // will return Ogre::UTFString with "Español" if currentLanguage == 2 translate("options_language");


Now you can stick the return of translate() into a TextAreaOverlayElement or whatever you need!


Alias: Reading_a_Unicode_Multi_Language_Translation_Table