TranslationModules - Let CEGUI translate your strings
Posted: Wed Aug 31, 2011 10:39
hay i had the idea to add translation modules into cegui for translating the strings in the Windows and Widgets
Example usage without TranslationModule:
if you want to make your menu multi language, you must change the setted text every time you change the language
this could be very annoying
Example usage with TranslationModule:
you change the language and the Windows look after translations for the new language in the TranslationModule which is setted to the system,
so you must not change the Text values of the windows.
Code examples:
latest branch on bitbucket
Example usage without TranslationModule:
if you want to make your menu multi language, you must change the setted text every time you change the language
this could be very annoying
Example usage with TranslationModule:
you change the language and the Windows look after translations for the new language in the TranslationModule which is setted to the system,
so you must not change the Text values of the windows.
Code examples:
- gettext - which reads the po files, for setting the language, uselocale is used for tread-safe
Code: Select all
TranslationSystem* trans = new GettextTranslationSystem;
trans->setProperty("Domain","cegui");
trans->setProperty("DomainDirectory",".");
std::cout << trans->translate("hello how are you?","de"); //"Wie geht es dir?" - TMX xml-files - an file that holds multible translations you can see the spec there
Code: Select all
TranslationSystem* trans = new TmxTranslationSystem;
static_cast<TmxTranslationSystem*>(trans)->handleFile("translations.tmx");
std::cout << trans->translate("hello how are you?","de"); //"Wie geht es dir?" - when using with system you could do that (after you use setTranslationSystem)
Code: Select all
std::cout << System::getSingleton().getTranslationSystem().translate("hello how are you?","de"); //"Wie geht es dir?"
latest branch on bitbucket
Code: Select all
/***********************************************************************
filename: CEGUITranslationModule.h
created: 8/6/2011
author: Hans 'Hanmac' Mackowiak
purpose: Defines abstract base class for translation modules
*************************************************************************/
/***************************************************************************
* Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
***************************************************************************/
#ifndef _CEGUITranslationModule_h_
#define _CEGUITranslationModule_h_
#include "CEGUIBase.h"
#include "CEGUIPropertyHelper.h"
#include "CEGUIString.h"
#include <map>
// Start of CEGUI namespace section
namespace CEGUI
{
/*!
\brief
Abstract class that defines the required interface for all resource provider sub-classes.
*/
class CEGUIEXPORT TranslationModule :
public AllocatedObject<TranslationModule>
{
public:
/*************************************************************************
Construction and Destruction
*************************************************************************/
/*!
\brief
Constructor for the TranslationModule class
*/
TranslationModule() { }
/*!
\brief
Destructor for the TranslationModule class
*/
virtual ~TranslationModule(void) { }
typedef std::map<String,String> Param;
virtual const String translate(const String &key,const String &lang,Param param = Param()) = 0;
template<typename T>
const String translate(const String &key,const String &lang,const String ¶mkey,const T paramvalue)
{
Param param;
param.insert(std::make_pair(paramkey,PropertyHelper<T>::toString(paramvalue)));
return translate(key,lang,param);
}
template<typename T1,typename T2>
const String translate(const String &key,const String &lang,const String ¶mkey1,const T1 paramvalue1,const String ¶mkey2,const T2 paramvalue2)
{
Param param;
param.insert(std::make_pair(paramkey1,PropertyHelper<T1>::toString(paramvalue1)));
param.insert(std::make_pair(paramkey2,PropertyHelper<T2>::toString(paramvalue2)));
return translate(key,lang,param);
}
template<typename T1,typename T2,typename T3>
const String translate(const String &key,const String &lang,const String ¶mkey1,const T1 paramvalue1,const String ¶mkey2,const T2 paramvalue2,const String ¶mkey3,const T3 paramvalue3)
{
Param param;
param.insert(std::make_pair(paramkey1,PropertyHelper<T1>::toString(paramvalue1)));
param.insert(std::make_pair(paramkey2,PropertyHelper<T2>::toString(paramvalue2)));
param.insert(std::make_pair(paramkey3,PropertyHelper<T3>::toString(paramvalue3)));
return translate(key,lang,param);
}
virtual bool isTranslatable(const String &key,const String &lang) const = 0;
protected:
};
} // End of CEGUI namespace section
#endif // end of guard _CEGUITranslationModule_h_