1
0
forked from 0ad/0ad

Change L10n code to use ps/Singleton, so that we can recreate it.

This allows for adding locales in mods.

This was SVN commit r15874.
This commit is contained in:
leper 2014-10-15 16:04:37 +00:00
parent f5179930f5
commit 18ab827886
10 changed files with 52 additions and 57 deletions

View File

@ -1263,7 +1263,7 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
XMBElement grandchild = grandchildren.Item(i);
if (grandchild.GetNodeName() == elmt_translate)
{
code += L10n::Instance().Translate(grandchild.GetText());
code += g_L10n.Translate(grandchild.GetText());
}
else if (grandchild.GetNodeName() == elmt_keep)
{
@ -1304,12 +1304,12 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
CStr context(child.GetAttributes().GetNamedItem(attr_context)); // Read the context if any.
if (!context.empty())
{
CStr translatedValue(L10n::Instance().TranslateWithContext(context, value));
CStr translatedValue(g_L10n.TranslateWithContext(context, value));
object->SetSetting(attributeName, translatedValue.UnescapeBackslashes().FromUTF8(), true);
}
else
{
CStr translatedValue(L10n::Instance().Translate(value));
CStr translatedValue(g_L10n.Translate(value));
object->SetSetting(attributeName, translatedValue.UnescapeBackslashes().FromUTF8(), true);
}
}
@ -1332,7 +1332,7 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
XMBElement grandchild = grandchildren.Item(i);
if (grandchild.GetNodeName() == elmt_translate)
{
translatedValue += L10n::Instance().Translate(grandchild.GetText());
translatedValue += g_L10n.Translate(grandchild.GetText());
}
else if (grandchild.GetNodeName() == elmt_keep)
{

View File

@ -203,12 +203,12 @@ bool COList::HandleAdditionalChildren(const XMBElement& child, CXeromyces* pFile
CStr context(grandchild.GetAttributes().GetNamedItem(attr_context)); // Read the context if any.
if (!context.empty())
{
CStr translatedValue(L10n::Instance().TranslateWithContext(context, value));
CStr translatedValue(g_L10n.TranslateWithContext(context, value));
oDef.m_Heading = translatedValue.FromUTF8();
}
else
{
CStr translatedValue(L10n::Instance().Translate(value));
CStr translatedValue(g_L10n.Translate(value));
oDef.m_Heading = translatedValue.FromUTF8();
}
}

View File

@ -201,7 +201,7 @@ void GUIRenderer::UpdateDrawCallCache(DrawCalls &Calls, const CStr& SpriteName,
if (!(*cit)->m_TextureName.empty())
{
CTextureProperties textureProps(L10n::Instance().LocalizePath((*cit)->m_TextureName));
CTextureProperties textureProps(g_L10n.LocalizePath((*cit)->m_TextureName));
textureProps.SetWrap((*cit)->m_WrapMode);
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
texture->Prefetch();

View File

@ -789,31 +789,31 @@ std::wstring GetBuildTimestamp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), i
char buf[200];
if (mode == -1) // Date, time and revision.
{
UDate dateTime = L10n::Instance().ParseDateTime(__DATE__ " " __TIME__, "MMM d yyyy HH:mm:ss", Locale::getUS());
std::string dateTimeString = L10n::Instance().LocalizeDateTime(dateTime, L10n::DateTime, SimpleDateFormat::DATE_TIME);
UDate dateTime = g_L10n.ParseDateTime(__DATE__ " " __TIME__, "MMM d yyyy HH:mm:ss", Locale::getUS());
std::string dateTimeString = g_L10n.LocalizeDateTime(dateTime, L10n::DateTime, SimpleDateFormat::DATE_TIME);
char svnRevision[32];
sprintf_s(svnRevision, ARRAY_SIZE(svnRevision), "%ls", svn_revision);
if (strcmp(svnRevision, "custom build") == 0)
{
// Translation: First item is a date and time, item between parenthesis is the Subversion revision number of the current build.
sprintf_s(buf, ARRAY_SIZE(buf), L10n::Instance().Translate("%s (custom build)").c_str(), dateTimeString.c_str());
sprintf_s(buf, ARRAY_SIZE(buf), g_L10n.Translate("%s (custom build)").c_str(), dateTimeString.c_str());
}
else
{
// Translation: First item is a date and time, item between parenthesis is the Subversion revision number of the current build.
sprintf_s(buf, ARRAY_SIZE(buf), L10n::Instance().Translate("%s (%ls)").c_str(), dateTimeString.c_str(), svn_revision);
sprintf_s(buf, ARRAY_SIZE(buf), g_L10n.Translate("%s (%ls)").c_str(), dateTimeString.c_str(), svn_revision);
}
}
else if (mode == 0) // Date.
{
UDate dateTime = L10n::Instance().ParseDateTime(__DATE__, "MMM d yyyy", Locale::getUS());
std::string dateTimeString = L10n::Instance().LocalizeDateTime(dateTime, L10n::Date, SimpleDateFormat::MEDIUM);
UDate dateTime = g_L10n.ParseDateTime(__DATE__, "MMM d yyyy", Locale::getUS());
std::string dateTimeString = g_L10n.LocalizeDateTime(dateTime, L10n::Date, SimpleDateFormat::MEDIUM);
sprintf_s(buf, ARRAY_SIZE(buf), "%s", dateTimeString.c_str());
}
else if (mode == 1) // Time.
{
UDate dateTime = L10n::Instance().ParseDateTime(__TIME__, "HH:mm:ss", Locale::getUS());
std::string dateTimeString = L10n::Instance().LocalizeDateTime(dateTime, L10n::Time, SimpleDateFormat::MEDIUM);
UDate dateTime = g_L10n.ParseDateTime(__TIME__, "HH:mm:ss", Locale::getUS());
std::string dateTimeString = g_L10n.LocalizeDateTime(dateTime, L10n::Time, SimpleDateFormat::MEDIUM);
sprintf_s(buf, ARRAY_SIZE(buf), "%s", dateTimeString.c_str());
}
else if (mode == 2) // Revision.
@ -822,7 +822,7 @@ std::wstring GetBuildTimestamp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), i
sprintf_s(svnRevision, ARRAY_SIZE(svnRevision), "%ls", svn_revision);
if (strcmp(svnRevision, "custom build") == 0)
{
sprintf_s(buf, ARRAY_SIZE(buf), "%s", L10n::Instance().Translate("custom build").c_str());
sprintf_s(buf, ARRAY_SIZE(buf), "%s", g_L10n.Translate("custom build").c_str());
}
else
{

View File

@ -36,12 +36,6 @@
#include "ps/GameSetup/GameSetup.h"
L10n& L10n::Instance()
{
static L10n m_instance;
return m_instance;
}
L10n::L10n()
: currentLocaleIsOriginalGameLocale(false), useLongStrings(false), dictionary(new tinygettext::Dictionary())
{

View File

@ -32,12 +32,16 @@
#include "lib/file/vfs/vfs_path.h"
#include "ps/Singleton.h"
#define g_L10n L10n::GetSingleton()
/**
* %Singleton for internationalization and localization.
*
* @sa http://trac.wildfiregames.com/wiki/Internationalization_and_Localization
*/
class L10n
class L10n : public Singleton<L10n>
{
/**
* Marks the L10n class as noncopyable.
@ -76,14 +80,6 @@ public:
Time ///< Only time.
};
/**
* Returns the running instance of the internationalization and localization
* singleton.
*
* If there is no instance yet, Instance() creates one and returns it.
*/
static L10n& Instance();
/**
* Returns the current locale.
*

View File

@ -26,31 +26,31 @@
// Returns a translation of the specified English string into the current language.
std::wstring JSI_L10n::Translate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring sourceString)
{
return wstring_from_utf8(L10n::Instance().Translate(utf8_from_wstring(sourceString)));
return wstring_from_utf8(g_L10n.Translate(utf8_from_wstring(sourceString)));
}
// Returns a translation of the specified English string, for the specified context.
std::wstring JSI_L10n::TranslateWithContext(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string context, std::wstring sourceString)
{
return wstring_from_utf8(L10n::Instance().TranslateWithContext(context, utf8_from_wstring(sourceString)));
return wstring_from_utf8(g_L10n.TranslateWithContext(context, utf8_from_wstring(sourceString)));
}
// Return a translated version of the given strings (singular and plural) depending on an integer value.
std::wstring JSI_L10n::TranslatePlural(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring singularSourceString, std::wstring pluralSourceString, int number)
{
return wstring_from_utf8(L10n::Instance().TranslatePlural(utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number));
return wstring_from_utf8(g_L10n.TranslatePlural(utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number));
}
// Return a translated version of the given strings (singular and plural) depending on an integer value, for the specified context.
std::wstring JSI_L10n::TranslatePluralWithContext(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string context, std::wstring singularSourceString, std::wstring pluralSourceString, int number)
{
return wstring_from_utf8(L10n::Instance().TranslatePluralWithContext(context, utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number));
return wstring_from_utf8(g_L10n.TranslatePluralWithContext(context, utf8_from_wstring(singularSourceString), utf8_from_wstring(pluralSourceString), number));
}
// Return a translated version of the given string, localizing it line by line.
std::wstring JSI_L10n::TranslateLines(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring sourceString)
{
return wstring_from_utf8(L10n::Instance().TranslateLines(utf8_from_wstring(sourceString)));
return wstring_from_utf8(g_L10n.TranslateLines(utf8_from_wstring(sourceString)));
}
// Return a translated version of the items in the specified array.
@ -59,96 +59,96 @@ std::vector<std::wstring> JSI_L10n::TranslateArray(ScriptInterface::CxPrivate* U
std::vector<std::wstring> translatedArray;
for (std::vector<std::wstring>::iterator iterator = sourceArray.begin(); iterator != sourceArray.end(); ++iterator)
{
translatedArray.push_back(wstring_from_utf8(L10n::Instance().Translate(utf8_from_wstring(*iterator))));
translatedArray.push_back(wstring_from_utf8(g_L10n.Translate(utf8_from_wstring(*iterator))));
}
return translatedArray;
}
std::wstring JSI_L10n::GetFallbackToAvailableDictLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().GetFallbackToAvailableDictLocale(locale);
return g_L10n.GetFallbackToAvailableDictLocale(locale);
}
// Return a localized version of a time given in milliseconds.
std::wstring JSI_L10n::FormatMillisecondsIntoDateString(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), UDate milliseconds, std::wstring formatString)
{
return wstring_from_utf8(L10n::Instance().FormatMillisecondsIntoDateString(milliseconds, utf8_from_wstring(formatString)));
return wstring_from_utf8(g_L10n.FormatMillisecondsIntoDateString(milliseconds, utf8_from_wstring(formatString)));
}
// Return a localized version of the given decimal number.
std::wstring JSI_L10n::FormatDecimalNumberIntoString(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), double number)
{
return wstring_from_utf8(L10n::Instance().FormatDecimalNumberIntoString(number));
return wstring_from_utf8(g_L10n.FormatDecimalNumberIntoString(number));
}
std::vector<std::string> JSI_L10n::GetSupportedLocaleBaseNames(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
return L10n::Instance().GetSupportedLocaleBaseNames();
return g_L10n.GetSupportedLocaleBaseNames();
}
std::vector<std::wstring> JSI_L10n::GetSupportedLocaleDisplayNames(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
return L10n::Instance().GetSupportedLocaleDisplayNames();
return g_L10n.GetSupportedLocaleDisplayNames();
}
std::string JSI_L10n::GetCurrentLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
return L10n::Instance().GetCurrentLocaleString();
return g_L10n.GetCurrentLocaleString();
}
bool JSI_L10n::UseLongStrings(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
return L10n::Instance().UseLongStrings();
return g_L10n.UseLongStrings();
}
std::vector<std::string> JSI_L10n::GetAllLocales(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
return L10n::Instance().GetAllLocales();
return g_L10n.GetAllLocales();
}
std::string JSI_L10n::GetDictionaryLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string configLocale)
{
return L10n::Instance().GetDictionaryLocale(configLocale);
return g_L10n.GetDictionaryLocale(configLocale);
}
std::vector<std::wstring> JSI_L10n::GetDictionariesForLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().GetDictionariesForLocale(locale);
return g_L10n.GetDictionariesForLocale(locale);
}
std::string JSI_L10n::GetLocaleLanguage(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().GetLocaleLanguage(locale);
return g_L10n.GetLocaleLanguage(locale);
}
std::string JSI_L10n::GetLocaleBaseName(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().GetLocaleBaseName(locale);
return g_L10n.GetLocaleBaseName(locale);
}
std::string JSI_L10n::GetLocaleCountry(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().GetLocaleCountry(locale);
return g_L10n.GetLocaleCountry(locale);
}
std::string JSI_L10n::GetLocaleScript(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().GetLocaleScript(locale);
return g_L10n.GetLocaleScript(locale);
}
bool JSI_L10n::ValidateLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().ValidateLocale(locale);
return g_L10n.ValidateLocale(locale);
}
bool JSI_L10n::SaveLocale(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string locale)
{
return L10n::Instance().SaveLocale(locale);
return g_L10n.SaveLocale(locale);
}
void JSI_L10n::ReevaluateCurrentLocaleAndReload(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
L10n::Instance().ReevaluateCurrentLocaleAndReload();
g_L10n.ReevaluateCurrentLocaleAndReload();
}

View File

@ -45,6 +45,7 @@
#include "gui/GUI.h"
#include "gui/GUIManager.h"
#include "gui/scripting/ScriptFunctions.h"
#include "i18n/L10n.h"
#include "maths/MathUtil.h"
#include "network/NetServer.h"
#include "network/NetClient.h"
@ -730,6 +731,8 @@ void Shutdown(int flags)
//delete g_DebuggingServer;
//TIMER_END(L"shutdown DebuggingServer (if active)");
delete &g_L10n;
from_config:
TIMER_BEGIN(L"shutdown ConfigDB");
delete &g_ConfigDB;
@ -950,6 +953,8 @@ bool Init(const CmdLineArgs& args, int flags)
}
}
new L10n;
// before scripting
// JS debugger temporarily disabled during the SpiderMonkey upgrade (check trac ticket #2348 for details)
//if (g_JSDebuggerEnabled)

View File

@ -126,7 +126,7 @@ Status SavedGames::Save(const std::wstring& name, const std::wstring& descriptio
OsPath realPath;
WARN_RETURN_STATUS_IF_ERR(g_VFS->GetRealPath(filename, realPath));
LOGMESSAGERENDER(wstring_from_utf8(L10n::Instance().Translate("Saved game to '%ls'") + "\n").c_str(), realPath.string().c_str());
LOGMESSAGERENDER(wstring_from_utf8(g_L10n.Translate("Saved game to '%ls'") + "\n").c_str(), realPath.string().c_str());
return INFO::OK;
}

View File

@ -241,7 +241,7 @@ void WriteScreenshot(const VfsPath& extension)
{
OsPath realPath;
g_VFS->GetRealPath(filename, realPath);
LOGMESSAGERENDER(wstring_from_utf8(L10n::Instance().Translate("Screenshot written to '%ls'")).c_str(), realPath.string().c_str());
LOGMESSAGERENDER(wstring_from_utf8(g_L10n.Translate("Screenshot written to '%ls'")).c_str(), realPath.string().c_str());
}
else
LOGERROR(L"Error writing screenshot to '%ls'", filename.string().c_str());
@ -374,7 +374,7 @@ void WriteBigScreenshot(const VfsPath& extension, int tiles)
{
OsPath realPath;
g_VFS->GetRealPath(filename, realPath);
LOGMESSAGERENDER(wstring_from_utf8(L10n::Instance().Translate("Screenshot written to '%ls'")).c_str(), realPath.string().c_str());
LOGMESSAGERENDER(wstring_from_utf8(g_L10n.Translate("Screenshot written to '%ls'")).c_str(), realPath.string().c_str());
}
else
LOGERROR(L"Error writing screenshot to '%ls'", filename.string().c_str());