2004-08-21 13:45:01 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "ps/i18n.h"
|
|
|
|
#include "ps/CVFSFile.h"
|
|
|
|
#include "scripting/ScriptingHost.h"
|
|
|
|
|
2004-08-24 13:29:40 +02:00
|
|
|
#include "ps/VFSUtil.h"
|
|
|
|
|
2004-08-21 13:45:01 +02:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#define LOG_CATEGORY "i18n"
|
|
|
|
|
2004-09-05 13:28:59 +02:00
|
|
|
// Yay, global variables. (The user only ever wants to be using one language
|
|
|
|
// at a time, so this is sufficient)
|
2004-08-21 13:45:01 +02:00
|
|
|
I18n::CLocale_interface* g_CurrentLocale = NULL;
|
2004-09-05 13:28:59 +02:00
|
|
|
std::string g_CurrentLocaleName;
|
2004-08-21 13:45:01 +02:00
|
|
|
|
|
|
|
bool I18n::LoadLanguage(const char* name)
|
|
|
|
{
|
2004-08-24 13:29:40 +02:00
|
|
|
// Special case: If name==NULL, use an 'empty' locale which should have
|
|
|
|
// no external dependencies other than CLogger, so it can be called
|
|
|
|
// before SpiderMonkey/etc has been initialised (useful for localised
|
|
|
|
// error messages that should fall back to English if the language hasn't
|
|
|
|
// been loaded yet)
|
|
|
|
if (name == NULL)
|
|
|
|
{
|
2004-09-01 21:48:03 +02:00
|
|
|
CLocale_interface* locale_ptr = I18n::NewLocale(NULL, NULL);
|
2005-06-28 06:06:25 +02:00
|
|
|
debug_assert(locale_ptr);
|
2004-08-24 13:29:40 +02:00
|
|
|
delete g_CurrentLocale;
|
|
|
|
g_CurrentLocale = locale_ptr;
|
2004-09-05 13:28:59 +02:00
|
|
|
g_CurrentLocaleName = "";
|
2004-08-24 13:29:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-01 21:48:03 +02:00
|
|
|
CLocale_interface* locale_ptr = I18n::NewLocale(g_ScriptingHost.getContext(), JS_GetGlobalObject(g_ScriptingHost.getContext()));
|
2004-08-21 13:45:01 +02:00
|
|
|
|
|
|
|
if (! locale_ptr)
|
|
|
|
{
|
|
|
|
debug_warn("Failed to create locale");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-24 13:29:40 +02:00
|
|
|
// Automatically delete the pointer when returning early
|
2004-08-21 13:45:01 +02:00
|
|
|
std::auto_ptr<CLocale_interface> locale (locale_ptr);
|
|
|
|
|
2006-05-17 16:48:18 +02:00
|
|
|
CStr dirname = CStr("language/")+name+"/";
|
2004-08-24 13:29:40 +02:00
|
|
|
|
|
|
|
VFSUtil::FileList files;
|
|
|
|
VFSUtil::FileList::iterator filename;
|
|
|
|
|
|
|
|
// Open *.lng with LoadStrings
|
|
|
|
|
2004-11-24 00:52:52 +01:00
|
|
|
if (! VFSUtil::FindFiles(dirname, "*.lng", files))
|
2004-08-24 13:29:40 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (filename = files.begin(); filename != files.end(); ++filename)
|
2004-08-21 13:45:01 +02:00
|
|
|
{
|
|
|
|
CVFSFile strings;
|
|
|
|
|
2004-08-24 13:29:40 +02:00
|
|
|
if (! (strings.Load(*filename) == PSRETURN_OK && locale->LoadStrings((const char*)strings.GetBuffer())))
|
|
|
|
{
|
|
|
|
LOG(ERROR, LOG_CATEGORY, "Error opening language string file '%s'", filename->c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open *.wrd with LoadDictionary
|
2004-08-21 13:45:01 +02:00
|
|
|
|
2004-11-24 00:52:52 +01:00
|
|
|
if (! VFSUtil::FindFiles(dirname, "*.wrd", files))
|
2004-08-24 13:29:40 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (filename = files.begin(); filename != files.end(); ++filename)
|
|
|
|
{
|
|
|
|
CVFSFile strings;
|
|
|
|
|
|
|
|
if (! (strings.Load(*filename) == PSRETURN_OK && locale->LoadDictionary((const char*)strings.GetBuffer())))
|
2004-08-21 13:45:01 +02:00
|
|
|
{
|
2004-08-24 13:29:40 +02:00
|
|
|
LOG(ERROR, LOG_CATEGORY, "Error opening language string file '%s'", filename->c_str());
|
2004-08-21 13:45:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
2004-08-24 13:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open *.js with LoadFunctions
|
2004-08-21 13:45:01 +02:00
|
|
|
|
2004-11-24 00:52:52 +01:00
|
|
|
if (! VFSUtil::FindFiles(dirname, "*.js", files))
|
2004-08-24 13:29:40 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (filename = files.begin(); filename != files.end(); ++filename)
|
|
|
|
{
|
|
|
|
CVFSFile strings;
|
|
|
|
|
|
|
|
if (! (strings.Load(*filename) == PSRETURN_OK
|
|
|
|
&&
|
|
|
|
locale->LoadFunctions(
|
|
|
|
(const char*)strings.GetBuffer(),
|
|
|
|
strings.GetBufferSize(),
|
|
|
|
filename->c_str()
|
|
|
|
)))
|
2004-08-21 13:45:01 +02:00
|
|
|
{
|
2004-08-24 13:29:40 +02:00
|
|
|
LOG(ERROR, LOG_CATEGORY, "Error opening language function file '%s'", filename->c_str());
|
2004-08-21 13:45:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free any previously loaded data
|
|
|
|
delete g_CurrentLocale;
|
|
|
|
|
2004-08-24 13:29:40 +02:00
|
|
|
// Store the new CLocale*, and stop the auto_ptr from deleting it
|
2004-08-21 13:45:01 +02:00
|
|
|
g_CurrentLocale = locale.release();
|
2004-08-24 13:29:40 +02:00
|
|
|
|
2004-09-05 13:28:59 +02:00
|
|
|
// Remember the name
|
|
|
|
g_CurrentLocaleName = name;
|
|
|
|
|
2004-08-21 13:45:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-05 13:28:59 +02:00
|
|
|
const char* I18n::CurrentLanguageName()
|
|
|
|
{
|
|
|
|
return g_CurrentLocaleName.c_str();
|
|
|
|
}
|
|
|
|
|
2004-08-21 13:45:01 +02:00
|
|
|
void I18n::Shutdown()
|
|
|
|
{
|
|
|
|
delete g_CurrentLocale;
|
2004-08-24 13:29:40 +02:00
|
|
|
g_CurrentLocale = NULL;
|
2004-08-21 13:45:01 +02:00
|
|
|
}
|