2012-07-10 20:16:48 +00:00
|
|
|
#include "Localization.h"
|
|
|
|
|
|
|
|
#ifdef BBGE_BUILD_WINDOWS
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BBGE_BUILD_UNIX
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BBGE_BUILD_MACOSX
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#include <CoreFoundation/CFLocale.h>
|
|
|
|
#include <CoreFoundation/CFString.h>
|
|
|
|
|
|
|
|
// veeery clunky.
|
|
|
|
static std::string _CFToStdString(CFStringRef cs)
|
|
|
|
{
|
2012-07-11 02:50:19 +00:00
|
|
|
char buf[1024];
|
|
|
|
CFStringGetCString(cs, &buf[0], 1024, kCFStringEncodingUTF8);
|
|
|
|
return &buf[0];
|
2012-07-10 20:16:48 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static std::string s_locale;
|
2012-07-14 13:00:37 +00:00
|
|
|
static std::string s_modpath;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
|
|
|
void setUsedLocale(const std::string& s)
|
|
|
|
{
|
|
|
|
s_locale = s;
|
|
|
|
}
|
|
|
|
|
2012-07-14 12:34:46 +00:00
|
|
|
const char *getUsedLocale()
|
|
|
|
{
|
|
|
|
return s_locale.c_str();
|
|
|
|
}
|
|
|
|
|
2012-07-14 13:00:37 +00:00
|
|
|
void setLocalisationModPath(const std::string& s)
|
|
|
|
{
|
|
|
|
s_modpath = s;
|
|
|
|
stringToLower(s_modpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hackish
|
|
|
|
// intended to be used only for paths which are known to start with the mod path,
|
|
|
|
// but can deal with it if this is not the case
|
|
|
|
std::string localisePathInternalModpath(const std::string &path)
|
|
|
|
{
|
|
|
|
std::string tmp = path;
|
|
|
|
stringToLower(tmp);
|
|
|
|
|
|
|
|
if(!strncmp(tmp.c_str(), s_modpath.c_str(), s_modpath.length()))
|
|
|
|
return localisePath(path, s_modpath);
|
|
|
|
|
|
|
|
return localisePath(path);
|
|
|
|
}
|
|
|
|
|
2012-07-14 12:34:46 +00:00
|
|
|
std::string localisePath(const std::string &path, const std::string& modpath /* = "" */)
|
2012-07-10 20:16:48 +00:00
|
|
|
{
|
2012-07-14 17:50:01 +00:00
|
|
|
if (s_locale.empty() || s_locale == "-")
|
2012-07-11 02:50:19 +00:00
|
|
|
return path;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
const std::string fname = path.substr(modpath.length());
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
/* we first try with complete locale name, i.e "locales/en_US/" */
|
|
|
|
std::string localisedPath = modpath + "locales/" + s_locale + "/" + fname;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
if (exists(localisedPath.c_str()))
|
|
|
|
return localisedPath;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
/* ok didn't work, let's retry with only language part of locale name, i.e "locales/en/" */
|
|
|
|
const size_t found = s_locale.find('_');
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
/* hmm, seems like we didn't have a full locale name anyway, use original path */
|
2012-07-10 20:16:48 +00:00
|
|
|
if (found == std::string::npos)
|
2012-07-11 02:50:19 +00:00
|
|
|
return path;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
localisedPath = modpath + "locales/" + s_locale.substr(0,found) + "/" + fname;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
/* hooray we found a file! */
|
|
|
|
if (exists(localisedPath.c_str()))
|
|
|
|
return localisedPath;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
/* seems like we don't have a localized version of the file available, use original path */
|
|
|
|
return path;
|
2012-07-10 20:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string getSystemLocale()
|
|
|
|
{
|
2012-07-11 02:50:19 +00:00
|
|
|
std::string localeStr;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
|
|
|
#ifdef BBGE_BUILD_WINDOWS
|
2012-07-11 02:50:19 +00:00
|
|
|
LCID lcid = GetThreadLocale();
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
char buf[100];
|
|
|
|
char ctry[100];
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
if (GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, sizeof buf) != 0)
|
|
|
|
{
|
|
|
|
localeStr = buf;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
if (GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, ctry, sizeof ctry) != 0)
|
|
|
|
{
|
|
|
|
localeStr += "_";
|
|
|
|
localeStr += ctry;
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 20:16:48 +00:00
|
|
|
#elif BBGE_BUILD_MACOSX
|
2012-07-11 02:50:19 +00:00
|
|
|
CFLocaleRef locale = CFLocaleCopyCurrent();
|
|
|
|
CFStringRef buf;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleLanguageCode)) != NULL)
|
|
|
|
{
|
|
|
|
localeStr = _CFToStdString(buf);
|
|
|
|
CFRelease(buf);
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCountryCode)) != NULL)
|
|
|
|
{
|
|
|
|
localeStr += "_";
|
|
|
|
localeStr += _CFToStdString(buf);
|
|
|
|
CFRelease(buf);
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
CFRelease(locale);
|
2012-07-10 20:16:48 +00:00
|
|
|
|
|
|
|
#else
|
2012-07-11 02:50:19 +00:00
|
|
|
const char *lang = (const char *)getenv("LANG");
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
if (lang && *lang)
|
|
|
|
{
|
|
|
|
localeStr = lang;
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
size_t found = localeStr.find('.');
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2012-07-12 03:46:18 +00:00
|
|
|
if (found != std::string::npos)
|
2012-07-11 02:50:19 +00:00
|
|
|
localeStr.resize(found);
|
|
|
|
}
|
2012-07-10 20:16:48 +00:00
|
|
|
#endif
|
|
|
|
|
2012-07-11 02:50:19 +00:00
|
|
|
return localeStr;
|
2012-07-10 20:16:48 +00:00
|
|
|
}
|
|
|
|
|