0ad/source/ps/i18n.cpp
janwas e6be7e36d2 FILE_WRITE_TO_MOD is now FILE_WRITE_TO_TARGET
ERR_(FILE|PATH)_NOT_FOUND -> ERR_TNODE_NOT_FOUND, ERR_NOT_(FILE|DIR) ->
ERR_TNODE_WRONG TYPE
path_util:
- path_append: bugfix (no more extraneous /); add support for appending
slash
- path_replace: bugfix (no more warnings for expected error)
- add path_last_component, path_foreach_component
- move some defs to the header that belong there from vfs.h/path.h

file: add dir_create; move PosixFile definition here and provide
accessor for fileio
vfs: no longer automatically append slash; instead, make sure caller
does
vfs_mount:
- mount_realpath: bugfix (strip trailing /), interface improvement
- document write_target; clarify MULTIPLE_MOUNTINGS; add support for
creating real dirs
vfs_tree:
- refactor TDir::add into find, add, find_and_add
- fix TDir and TNode for dir-ends-with-slash requirement
- split up lookup into lookup_cb and path_foreach_component
- add support for inserting dirs with given mount point: tree_add_path
(needed when mounting)

wposix: get rid of weird PATH_MAX = 260 (Win32 weirdness)


TextureManager: remove no longer needed SupportedTextureFormats
GameSetup, Xeromyces: setup XMB location to data/cache/mods/official/xmb
Util: HardcodedErrorString now uses error_description_r
VFSUtil, i18n: fixes for dir slash issue

Closes #79, #80

This was SVN commit r3874.
2006-05-17 14:48:18 +00:00

126 lines
3.1 KiB
C++

#include "precompiled.h"
#include "ps/i18n.h"
#include "ps/CVFSFile.h"
#include "scripting/ScriptingHost.h"
#include "ps/VFSUtil.h"
#include "ps/CLogger.h"
#define LOG_CATEGORY "i18n"
// Yay, global variables. (The user only ever wants to be using one language
// at a time, so this is sufficient)
I18n::CLocale_interface* g_CurrentLocale = NULL;
std::string g_CurrentLocaleName;
bool I18n::LoadLanguage(const char* name)
{
// 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)
{
CLocale_interface* locale_ptr = I18n::NewLocale(NULL, NULL);
debug_assert(locale_ptr);
delete g_CurrentLocale;
g_CurrentLocale = locale_ptr;
g_CurrentLocaleName = "";
return true;
}
CLocale_interface* locale_ptr = I18n::NewLocale(g_ScriptingHost.getContext(), JS_GetGlobalObject(g_ScriptingHost.getContext()));
if (! locale_ptr)
{
debug_warn("Failed to create locale");
return false;
}
// Automatically delete the pointer when returning early
std::auto_ptr<CLocale_interface> locale (locale_ptr);
CStr dirname = CStr("language/")+name+"/";
VFSUtil::FileList files;
VFSUtil::FileList::iterator filename;
// Open *.lng with LoadStrings
if (! VFSUtil::FindFiles(dirname, "*.lng", files))
return false;
for (filename = files.begin(); filename != files.end(); ++filename)
{
CVFSFile strings;
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
if (! VFSUtil::FindFiles(dirname, "*.wrd", files))
return false;
for (filename = files.begin(); filename != files.end(); ++filename)
{
CVFSFile strings;
if (! (strings.Load(*filename) == PSRETURN_OK && locale->LoadDictionary((const char*)strings.GetBuffer())))
{
LOG(ERROR, LOG_CATEGORY, "Error opening language string file '%s'", filename->c_str());
return false;
}
}
// Open *.js with LoadFunctions
if (! VFSUtil::FindFiles(dirname, "*.js", files))
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()
)))
{
LOG(ERROR, LOG_CATEGORY, "Error opening language function file '%s'", filename->c_str());
return false;
}
}
// Free any previously loaded data
delete g_CurrentLocale;
// Store the new CLocale*, and stop the auto_ptr from deleting it
g_CurrentLocale = locale.release();
// Remember the name
g_CurrentLocaleName = name;
return true;
}
const char* I18n::CurrentLanguageName()
{
return g_CurrentLocaleName.c_str();
}
void I18n::Shutdown()
{
delete g_CurrentLocale;
g_CurrentLocale = NULL;
}