mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-14 02:27:40 -04:00
Initial Commit of CyanWorlds.com Engine Open Source Client/Plugin
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,141 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// pfLocalizationDataMgr - singleton class for managing the
|
||||
// localization XML data tree
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _pfLocalizationDataMgr_h
|
||||
#define _pfLocalizationDataMgr_h
|
||||
|
||||
#include "hsTypes.h"
|
||||
#include "hsStlUtils.h"
|
||||
|
||||
#include "pfLocalizedString.h"
|
||||
|
||||
class plStatusLog;
|
||||
|
||||
// Helper classes/structs that are only used in this main class
|
||||
class LocalizationDatabase;
|
||||
struct LocElementInfo;
|
||||
struct LocSetInfo;
|
||||
struct LocAgeInfo;
|
||||
|
||||
class pfLocalizationDataMgr
|
||||
{
|
||||
private:
|
||||
static pfLocalizationDataMgr* fInstance;
|
||||
static plStatusLog* fLog;
|
||||
|
||||
protected:
|
||||
// This is a special case map class that will deconstruct the "Age.Set.Name" key into component parts
|
||||
// and store them so that a list of each part given it's parent part is easy to grab. I.e. I can grab
|
||||
// a list of all age names, a list of all set names (given age name) and a set of all names (given
|
||||
// age and set names)
|
||||
template<class mapT>
|
||||
class pf3PartMap
|
||||
{
|
||||
protected:
|
||||
// Outer map is Age, then Set, finally Name
|
||||
typedef std::map<std::wstring, std::map<std::wstring, std::map<std::wstring, mapT> > > ThreePartMap;
|
||||
ThreePartMap fData;
|
||||
|
||||
void ISplitString(std::wstring key, std::wstring &age, std::wstring &set, std::wstring &name);
|
||||
public:
|
||||
// We will just have very basic functionality
|
||||
bool exists(const std::wstring & key); // returns true if the key exists
|
||||
bool setExists(const std::wstring & key); // returns true if the age.set exists (ignores name if passed in)
|
||||
void erase(const std::wstring & key); // erases the key from the map
|
||||
|
||||
mapT &operator[](const std::wstring &key); // returns the item referenced by the key (and creates if necessary)
|
||||
|
||||
std::vector<std::wstring> getAgeList(); // returns a list of all ages in this map
|
||||
std::vector<std::wstring> getSetList(const std::wstring & age); // returns a list of all sets in the specified age
|
||||
std::vector<std::wstring> getNameList(const std::wstring & age, const std::wstring & set);
|
||||
};
|
||||
|
||||
LocalizationDatabase *fDatabase;
|
||||
|
||||
typedef std::map<std::wstring, pfLocalizedString> localizedElement;
|
||||
|
||||
// Contains all localized strings, the key is the Age.Set.Name specified by XML, in localizedElement, the key is the language string
|
||||
pf3PartMap<localizedElement> fLocalizedElements;
|
||||
|
||||
std::string fDataPath;
|
||||
|
||||
localizedElement ICreateLocalizedElement(); // ease of use function that creates a basic localized element object
|
||||
|
||||
std::wstring IGetCurrentLanguageName(); // get the name of the current language
|
||||
std::vector<std::wstring> IGetAllLanguageNames();
|
||||
|
||||
void IConvertElement(LocElementInfo *elementInfo, const std::wstring & curPath);
|
||||
void IConvertSet(LocSetInfo *setInfo, const std::wstring & curPath);
|
||||
void IConvertAge(LocAgeInfo *ageInfo, const std::wstring & curPath);
|
||||
|
||||
char *IConvertToByteStream(const std::wstring & data, UInt32 &len); // converts the wstring data to a string of bytes for file writing
|
||||
void IWriteText(const std::string & filename, const std::wstring & ageName, const std::wstring & languageName); // Write localization text to the specified file
|
||||
|
||||
pfLocalizationDataMgr(const std::string & path);
|
||||
public:
|
||||
virtual ~pfLocalizationDataMgr();
|
||||
|
||||
static void Initialize(const std::string & path);
|
||||
static void Shutdown();
|
||||
static pfLocalizationDataMgr &Instance(void) {return *fInstance;}
|
||||
static bool InstanceValid(void) {return fInstance != nil;}
|
||||
|
||||
void SetupData();
|
||||
|
||||
pfLocalizedString GetElement(const std::wstring & name);
|
||||
pfLocalizedString GetSpecificElement(const std::wstring & name, const std::wstring & languageName);
|
||||
|
||||
std::vector<std::wstring> GetAgeList();
|
||||
std::vector<std::wstring> GetSetList(const std::wstring & ageName);
|
||||
std::vector<std::wstring> GetElementList(const std::wstring & ageName, const std::wstring & setName);
|
||||
std::vector<std::wstring> GetLanguages(const std::wstring & ageName, const std::wstring & setName, const std::wstring & elementName);
|
||||
|
||||
std::wstring GetElementXMLData(const std::wstring & name, const std::wstring & languageName);
|
||||
std::wstring GetElementPlainTextData(const std::wstring & name, const std::wstring & languageName);
|
||||
|
||||
// These convert the XML data to the actual subtitle and return true if successful (editor only)
|
||||
bool SetElementXMLData(const std::wstring & name, const std::wstring & languageName, const std::wstring & xmlData);
|
||||
bool SetElementPlainTextData(const std::wstring & name, const std::wstring & languageName, const std::wstring & plainText);
|
||||
|
||||
// Addition and deletion functions, return true if successful (editor only)
|
||||
bool AddLocalization(const std::wstring & name, const std::wstring & newLanguage);
|
||||
bool AddElement(const std::wstring & name);
|
||||
bool DeleteLocalization(const std::wstring & name, const std::wstring & languageName);
|
||||
bool DeleteElement(const std::wstring & name);
|
||||
|
||||
// Writes the current database to the disk (editor only). It will create all the files and put them into path
|
||||
void WriteDatabaseToDisk(const std::string & path);
|
||||
|
||||
void OutputTreeToLog(); // prints the localization tree to the log file
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,91 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// pfLocalizationMgr - singleton class for managing localization of
|
||||
// strings (so python doesn't have to do it)
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "hsTypes.h"
|
||||
|
||||
#include "pfLocalizedString.h"
|
||||
#include "pfLocalizationDataMgr.h"
|
||||
#include "pfLocalizationMgr.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//// pfLocalizationMgr Functions /////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
pfLocalizationMgr *pfLocalizationMgr::fInstance = nil;
|
||||
|
||||
//// Constructor/Destructor //////////////////////////////////////////
|
||||
|
||||
pfLocalizationMgr::pfLocalizationMgr()
|
||||
{
|
||||
hsAssert(!fInstance, "Tried to create the localization manager more than once!");
|
||||
fInstance = this;
|
||||
}
|
||||
|
||||
pfLocalizationMgr::~pfLocalizationMgr()
|
||||
{
|
||||
fInstance = nil;
|
||||
}
|
||||
|
||||
//// Initialize //////////////////////////////////////////////////////
|
||||
|
||||
void pfLocalizationMgr::Initialize(const std::string & dataPath)
|
||||
{
|
||||
if (fInstance)
|
||||
return;
|
||||
|
||||
fInstance = TRACKED_NEW pfLocalizationMgr();
|
||||
pfLocalizationDataMgr::Initialize(dataPath); // set up the data manager
|
||||
}
|
||||
|
||||
//// Shutdown ////////////////////////////////////////////////////////
|
||||
|
||||
void pfLocalizationMgr::Shutdown()
|
||||
{
|
||||
if (fInstance)
|
||||
{
|
||||
pfLocalizationDataMgr::Shutdown(); // make sure the subtitle data manager is shut down
|
||||
delete fInstance;
|
||||
}
|
||||
}
|
||||
|
||||
//// GetString ///////////////////////////////////////////////////////
|
||||
|
||||
std::wstring pfLocalizationMgr::GetString(const std::wstring & path, const std::vector<std::wstring> & args)
|
||||
{
|
||||
return pfLocalizationDataMgr::Instance().GetElement(path) % args;
|
||||
}
|
||||
|
||||
std::wstring pfLocalizationMgr::GetString(const std::wstring & path)
|
||||
{
|
||||
std::vector<std::wstring> args; // blank args so that % signs are still handled correctly
|
||||
return pfLocalizationDataMgr::Instance().GetElement(path) % args;
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// pfLocalizationMgr - singleton class for managing localization of
|
||||
// strings (so python doesn't have to do it)
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _pfLocalizationMgr_h
|
||||
#define _pfLocalizationMgr_h
|
||||
|
||||
#include "hsTypes.h"
|
||||
#include "hsStlUtils.h"
|
||||
|
||||
class pfLocalizationMgr
|
||||
{
|
||||
private:
|
||||
static pfLocalizationMgr *fInstance;
|
||||
protected:
|
||||
pfLocalizationMgr();
|
||||
public:
|
||||
virtual ~pfLocalizationMgr();
|
||||
|
||||
static void Initialize(const std::string & dataPath);
|
||||
static void Shutdown();
|
||||
static pfLocalizationMgr &Instance(void) {return *fInstance;}
|
||||
static bool InstanceValid(void) {return fInstance != nil;}
|
||||
|
||||
// Returns the final localized string, designated by path, and with the arguments properly substituted
|
||||
// if you want to use the default argument order, just use %s like you would with printf, BUT, if you
|
||||
// want the arguments in a different order (like you had to switch things around for a specific language)
|
||||
// then you use %1s, %2s, %3s and so on to specify arguments, these two cannot be mixed and you won't get
|
||||
// the results you expect if you do mix them. Path is specified by Age.Set.Name
|
||||
std::wstring GetString(const std::wstring & path, const std::vector<std::wstring> & args);
|
||||
std::wstring GetString(const std::wstring & path);
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,368 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// pfLocalizedString - a small class to handle localized strings and
|
||||
// which can take parameters (like %s or %1s) and
|
||||
// also can be easily translated to XML format
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "hsTypes.h"
|
||||
#include "hsUtils.h"
|
||||
|
||||
#include "pfLocalizedString.h"
|
||||
|
||||
#if HS_BUILD_FOR_MAC
|
||||
#include <bxwchar.h>
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//// pfLocalizedString functions /////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
//// Constructors ////////////////////////////////////////////////////
|
||||
|
||||
pfLocalizedString::pfLocalizedString(const wchar_t *plainText)
|
||||
{
|
||||
fNumArguments = 0;
|
||||
IConvertFromPlainText(plainText);
|
||||
}
|
||||
|
||||
pfLocalizedString::pfLocalizedString(const std::wstring & plainText)
|
||||
{
|
||||
fNumArguments = 0;
|
||||
IConvertFromPlainText(plainText);
|
||||
}
|
||||
|
||||
//// IConvertFromPlainText ///////////////////////////////////////////
|
||||
|
||||
void pfLocalizedString::IConvertFromPlainText(const std::wstring & plainText)
|
||||
{
|
||||
textBlock curTextBlock;
|
||||
fText.clear();
|
||||
fPlainTextRep = plainText;
|
||||
fNumArguments = 0; // reset the argument count
|
||||
int curParameter = 0;
|
||||
|
||||
for (std::wstring::size_type curIndex = 0; curIndex < plainText.size(); curIndex++)
|
||||
{
|
||||
wchar_t curChar = plainText[curIndex];
|
||||
bool isLastChar = (curIndex == (plainText.length() - 1));
|
||||
switch (curChar)
|
||||
{
|
||||
case L'\\':
|
||||
if (!isLastChar)
|
||||
{
|
||||
// we need to see the next character
|
||||
curIndex++;
|
||||
wchar_t nextChar = plainText[curIndex];
|
||||
if ((nextChar == L'%')||(nextChar == L'\\'))
|
||||
{
|
||||
// we recognize it as an escaped character, so add it to the text
|
||||
curTextBlock.fText += nextChar;
|
||||
}
|
||||
// otherwise we don't recognize it and it will be skipped
|
||||
}
|
||||
// if it's the last char, just drop it
|
||||
break;
|
||||
case L'%':
|
||||
if (!isLastChar)
|
||||
{
|
||||
// we need to grab the trailing s character
|
||||
std::wstring::size_type endArgPos = plainText.find(L"s", curIndex);
|
||||
if (endArgPos != std::wstring::npos) // make sure the s exists
|
||||
{
|
||||
if (endArgPos == (curIndex + 1)) // no number specifier
|
||||
{
|
||||
fText.push_back(curTextBlock);
|
||||
curTextBlock.fIsParam = true;
|
||||
curTextBlock.fParamIndex = curParameter;
|
||||
curParameter++;
|
||||
curTextBlock.fText = L"";
|
||||
fText.push_back(curTextBlock);
|
||||
curTextBlock.fIsParam = false;
|
||||
curTextBlock.fParamIndex = 0;
|
||||
}
|
||||
else // number specified
|
||||
{
|
||||
fText.push_back(curTextBlock);
|
||||
curTextBlock.fIsParam = true;
|
||||
curTextBlock.fText = L"";
|
||||
|
||||
std::wstring number = plainText.substr(curIndex + 1, (endArgPos - (curIndex + 1)));
|
||||
curTextBlock.fParamIndex = _wtoi(number.c_str()) - 1; // args are 1-based, vectors are 0-based
|
||||
fText.push_back(curTextBlock);
|
||||
|
||||
curTextBlock.fIsParam = false;
|
||||
curTextBlock.fParamIndex = 0;
|
||||
}
|
||||
fNumArguments++; // increment our argument count
|
||||
curIndex = endArgPos; // update our position
|
||||
}
|
||||
// if s didn't exist, we just skip this % sign
|
||||
}
|
||||
// if it was the last char, we just skip this % sign
|
||||
break;
|
||||
default:
|
||||
curTextBlock.fText += curChar;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fText.push_back(curTextBlock);
|
||||
|
||||
IUpdateXML();
|
||||
}
|
||||
|
||||
//// IUpdatePlainText ////////////////////////////////////////////////
|
||||
|
||||
void pfLocalizedString::IUpdatePlainText()
|
||||
{
|
||||
fPlainTextRep = L"";
|
||||
for (std::vector<std::wstring>::size_type curIndex = 0; curIndex < fText.size(); curIndex++)
|
||||
{
|
||||
textBlock curTextBlock = fText[curIndex];
|
||||
|
||||
if (curTextBlock.fIsParam)
|
||||
{
|
||||
std::wstring paramStr = L"%";
|
||||
wchar_t buff[256];
|
||||
_itow(curTextBlock.fParamIndex + 1, buff, 10);
|
||||
paramStr += buff;
|
||||
paramStr += L"s";
|
||||
fPlainTextRep += paramStr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, we need to copy all the text over, making sure that % and \ are properly escaped
|
||||
for (std::wstring::size_type curChar = 0; curChar < curTextBlock.fText.size(); curChar++)
|
||||
{
|
||||
if ((curTextBlock.fText[curChar] == L'\\')||(curTextBlock.fText[curChar] == L'%'))
|
||||
fPlainTextRep += L"\\";
|
||||
fPlainTextRep += curTextBlock.fText[curChar];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//// IConvertFromXML /////////////////////////////////////////////////
|
||||
|
||||
void pfLocalizedString::IConvertFromXML(const std::wstring & xml)
|
||||
{
|
||||
textBlock curTextBlock;
|
||||
fText.clear();
|
||||
fNumArguments = 0; // reset the argument counter
|
||||
int curParameter = 0;
|
||||
|
||||
for (std::wstring::size_type curIndex = 0; curIndex < xml.length(); curIndex++)
|
||||
{
|
||||
wchar_t curChar = xml[curIndex];
|
||||
bool isLastChar = (curIndex == (xml.length() - 1));
|
||||
switch (curChar)
|
||||
{ // expat handles the > < and so on stuff for us
|
||||
case L'\\': // but we want to be able to escape the % sign and the \ character
|
||||
if (!isLastChar)
|
||||
{
|
||||
// we need to see the next character
|
||||
curIndex++;
|
||||
wchar_t nextChar = xml[curIndex];
|
||||
if ((nextChar == L'%')||(nextChar == L'\\'))
|
||||
{
|
||||
// we recognize it as an escaped character, so add it to the text
|
||||
curTextBlock.fText += nextChar;
|
||||
}
|
||||
// otherwise we don't recognize it and it will be skipped
|
||||
}
|
||||
// if it's the last char, just drop it
|
||||
break;
|
||||
case L'%':
|
||||
if (!isLastChar)
|
||||
{
|
||||
// we need to grab the trailing s character
|
||||
std::wstring::size_type endArgPos = xml.find(L"s", curIndex);
|
||||
if (endArgPos != std::wstring::npos) // make sure the s exists
|
||||
{
|
||||
if (endArgPos == (curIndex + 1)) // no number specifier
|
||||
{
|
||||
fText.push_back(curTextBlock);
|
||||
curTextBlock.fIsParam = true;
|
||||
curTextBlock.fParamIndex = curParameter;
|
||||
curParameter++;
|
||||
curTextBlock.fText = L"";
|
||||
fText.push_back(curTextBlock);
|
||||
curTextBlock.fIsParam = false;
|
||||
curTextBlock.fParamIndex = 0;
|
||||
}
|
||||
else // number specified
|
||||
{
|
||||
fText.push_back(curTextBlock);
|
||||
curTextBlock.fIsParam = true;
|
||||
curTextBlock.fText = L"";
|
||||
|
||||
std::wstring number = xml.substr(curIndex + 1, (endArgPos - (curIndex + 1)));
|
||||
curTextBlock.fParamIndex = _wtoi(number.c_str()) - 1; // args are 1-based, vectors are 0-based
|
||||
fText.push_back(curTextBlock);
|
||||
|
||||
curTextBlock.fIsParam = false;
|
||||
curTextBlock.fParamIndex = 0;
|
||||
}
|
||||
fNumArguments++; // increment the number of arguments
|
||||
curIndex = endArgPos; // update our position
|
||||
}
|
||||
// if s didn't exist, we just skip this % sign
|
||||
}
|
||||
// if it was the last char, we just skip this % sign
|
||||
break;
|
||||
default:
|
||||
curTextBlock.fText += curChar;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fText.push_back(curTextBlock);
|
||||
|
||||
IUpdatePlainText();
|
||||
IUpdateXML(); // we don't really get pure xml from the parser (since it auto translates all the &x; stuff)
|
||||
}
|
||||
|
||||
//// IUpdateXML //////////////////////////////////////////////////////
|
||||
|
||||
void pfLocalizedString::IUpdateXML()
|
||||
{
|
||||
fXMLRep = L"";
|
||||
for (std::vector<std::wstring>::size_type curIndex = 0; curIndex < fText.size(); curIndex++)
|
||||
{
|
||||
textBlock curTextBlock = fText[curIndex];
|
||||
|
||||
if (curTextBlock.fIsParam)
|
||||
{
|
||||
std::wstring paramStr = L"%";
|
||||
wchar_t buff[256];
|
||||
_itow(curTextBlock.fParamIndex + 1, buff, 10);
|
||||
paramStr += buff;
|
||||
paramStr += L"s";
|
||||
fXMLRep += paramStr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, we need to copy all the text over, making sure that %, &, <, and > are properly converted
|
||||
for (std::wstring::size_type curChar = 0; curChar < curTextBlock.fText.size(); curChar++)
|
||||
{
|
||||
if (curTextBlock.fText[curChar] == L'%')
|
||||
fXMLRep += L"\\%";
|
||||
else if (curTextBlock.fText[curChar] == L'&')
|
||||
fXMLRep += L"&";
|
||||
else if (curTextBlock.fText[curChar] == L'<')
|
||||
fXMLRep += L"<";
|
||||
else if (curTextBlock.fText[curChar] == L'>')
|
||||
fXMLRep += L">";
|
||||
else
|
||||
fXMLRep += curTextBlock.fText[curChar];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//// FromXML /////////////////////////////////////////////////////////
|
||||
|
||||
void pfLocalizedString::FromXML(const std::wstring & xml)
|
||||
{
|
||||
IConvertFromXML(xml);
|
||||
}
|
||||
|
||||
//// Operators ///////////////////////////////////////////////////////
|
||||
|
||||
bool pfLocalizedString::operator<(pfLocalizedString &obj)
|
||||
{
|
||||
return (fPlainTextRep < obj.fPlainTextRep);
|
||||
}
|
||||
|
||||
bool pfLocalizedString::operator>(pfLocalizedString &obj)
|
||||
{
|
||||
return (fPlainTextRep > obj.fPlainTextRep);
|
||||
}
|
||||
|
||||
bool pfLocalizedString::operator==(pfLocalizedString &obj)
|
||||
{
|
||||
return (fPlainTextRep == obj.fPlainTextRep);
|
||||
}
|
||||
|
||||
bool pfLocalizedString::operator<=(pfLocalizedString &obj)
|
||||
{
|
||||
return (fPlainTextRep <= obj.fPlainTextRep);
|
||||
}
|
||||
|
||||
bool pfLocalizedString::operator>=(pfLocalizedString &obj)
|
||||
{
|
||||
return (fPlainTextRep >= obj.fPlainTextRep);
|
||||
}
|
||||
|
||||
bool pfLocalizedString::operator!=(pfLocalizedString &obj)
|
||||
{
|
||||
return (fPlainTextRep != obj.fPlainTextRep);
|
||||
}
|
||||
|
||||
pfLocalizedString pfLocalizedString::operator+(pfLocalizedString &obj)
|
||||
{
|
||||
fPlainTextRep += obj.fPlainTextRep;
|
||||
IConvertFromPlainText(fPlainTextRep);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pfLocalizedString &pfLocalizedString::operator+=(pfLocalizedString &obj)
|
||||
{
|
||||
fPlainTextRep += obj.fPlainTextRep;
|
||||
IConvertFromPlainText(fPlainTextRep);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pfLocalizedString &pfLocalizedString::operator=(const std::wstring & plainText)
|
||||
{
|
||||
IConvertFromPlainText(plainText);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pfLocalizedString &pfLocalizedString::operator=(const wchar_t *plainText)
|
||||
{
|
||||
IConvertFromPlainText(plainText);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::wstring pfLocalizedString::operator%(const std::vector<std::wstring> & arguments)
|
||||
{
|
||||
std::wstring retVal = L"";
|
||||
for (std::vector<std::wstring>::size_type curIndex = 0; curIndex < fText.size(); curIndex++)
|
||||
{
|
||||
if (fText[curIndex].fIsParam)
|
||||
{
|
||||
int curParam = fText[curIndex].fParamIndex;
|
||||
if (curParam < arguments.size())
|
||||
retVal += arguments[curParam];
|
||||
}
|
||||
else
|
||||
retVal += fText[curIndex].fText;
|
||||
}
|
||||
return retVal;
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// pfLocalizedString - a small class to handle localized strings and
|
||||
// which can take parameters (like %s or %1s) and
|
||||
// also can be easily translated to XML format
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _pfLocalizedString_h
|
||||
#define _pfLocalizedString_h
|
||||
|
||||
#include "hsTypes.h"
|
||||
#include "hsStlUtils.h"
|
||||
|
||||
|
||||
//// pfLocalizedString Class Definition //////////////////////////////
|
||||
// a small class to handle localized strings and which can take
|
||||
// parameters (like %s or %1s) and also can be easily translated to
|
||||
// XML format
|
||||
|
||||
class pfLocalizedString
|
||||
{
|
||||
protected:
|
||||
// stores all basic text and param information in a nice, easy-to-use block of data
|
||||
struct textBlock
|
||||
{
|
||||
bool fIsParam; // if true, then this is a parameter, not a string
|
||||
std::wstring fText;
|
||||
UInt8 fParamIndex;
|
||||
|
||||
textBlock() : fIsParam(false), fParamIndex(0) {}
|
||||
};
|
||||
|
||||
std::vector<textBlock> fText; // the individual text elements that make up this string
|
||||
std::wstring fXMLRep; // the XML representation of this string
|
||||
std::wstring fPlainTextRep; // the plain text representation of this string
|
||||
UInt16 fNumArguments; // number of arguments this string has
|
||||
|
||||
void IConvertFromPlainText(const std::wstring & plainText);
|
||||
void IUpdatePlainText(); // from the internal representation
|
||||
void IConvertFromXML(const std::wstring & xml);
|
||||
void IUpdateXML(); // from the internal representation
|
||||
public:
|
||||
pfLocalizedString() : fNumArguments(0) {}
|
||||
pfLocalizedString(const wchar_t *plainText);
|
||||
pfLocalizedString(const std::wstring & plainText);
|
||||
virtual ~pfLocalizedString() {}
|
||||
|
||||
// To translate to and from xml format (where <, > and other signs can't be used)
|
||||
void FromXML(const std::wstring & xml);
|
||||
std::wstring ToXML() {return fXMLRep;}
|
||||
|
||||
UInt16 GetArgumentCount() {return fNumArguments;}
|
||||
|
||||
// Various operators, they all work pretty much the same as the standard string or wstring operators
|
||||
// but note that the all work on the plain text representation (not the XML representation)
|
||||
bool operator<(pfLocalizedString &obj);
|
||||
bool operator>(pfLocalizedString &obj);
|
||||
bool operator==(pfLocalizedString &obj);
|
||||
bool operator<=(pfLocalizedString &obj);
|
||||
bool operator>=(pfLocalizedString &obj);
|
||||
bool operator!=(pfLocalizedString &obj);
|
||||
|
||||
operator const wchar_t *() {return fPlainTextRep.c_str();}
|
||||
operator std::wstring() {return fPlainTextRep;}
|
||||
|
||||
pfLocalizedString operator+(pfLocalizedString &obj);
|
||||
pfLocalizedString &operator+=(pfLocalizedString &obj);
|
||||
|
||||
pfLocalizedString &operator=(const std::wstring & plainText);
|
||||
pfLocalizedString &operator=(const wchar_t *plainText);
|
||||
|
||||
// Specialized operator for replacing text with arguments
|
||||
std::wstring operator%(const std::vector<std::wstring> & arguments);
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user