OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Reading a Unicode Multi Language Translation Table
View page
Source of version: 5
(current)
{maketoc} !!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: {CODE(wrap="1", colors="c++")}options_language English Español main_instructions INSTRUCTIONS INSTRUCCIONES main_options OPTIONS OPCIONES main_credits CREDITS CREDITOS{CODE} __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.{CODE(wrap="1", colors="c++")}short unsigned int currentLanguage; std::vector<std::vector<Ogre::UTFString>> languageMap; // vector array for each language{CODE} 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: {CODE(wrap="1", colors="c++")}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."); }{CODE} Later you will need to get that information back! {CODE(wrap="1", colors="c++")}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; }{CODE} !!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: {CODE(wrap="1", colors="c++")}// will return Ogre::UTFString with "English" if currentLanguage == 1 // will return Ogre::UTFString with "Español" if currentLanguage == 2 translate("options_language");{CODE} Now you can stick the return of translate() into a TextAreaOverlayElement or whatever you need! --- Alias: (alias(Reading_a_Unicode_Multi_Language_Translation_Table))
Search by Tags
Search Wiki by Freetags
Latest Changes
One Function Ogre
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
...more
Search
Find
Advanced
Search Help
Online Users
162 online users