1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-07-03 22:44:32 +00:00

Move localisation code to BBGE, and allow textures to be localised.

This commit is contained in:
fgenesis 2012-07-10 22:16:48 +02:00
parent b22ceb2aa0
commit 7b2fa39428
12 changed files with 174 additions and 153 deletions

View file

@ -1209,11 +1209,11 @@ void Continuity::loadSongBank()
songSlotNames.clear(); songSlotNames.clear();
songBank.clear(); songBank.clear();
loadIntoSongBank(dsq->user.localisePath("data/songs.xml")); loadIntoSongBank(localisePath("data/songs.xml"));
if (dsq->mod.isActive()) if (dsq->mod.isActive())
{ {
loadIntoSongBank(dsq->user.localisePath(dsq->mod.getPath() + "scripts/songs.xml", dsq->mod.getPath())); loadIntoSongBank(localisePath(dsq->mod.getPath() + "scripts/songs.xml", dsq->mod.getPath()));
} }
} }
@ -3244,12 +3244,12 @@ void Continuity::reset()
loadIngredientDisplayNames("data/ingredientnames.txt"); loadIngredientDisplayNames("data/ingredientnames.txt");
std::string fname = dsq->user.localisePath("data/ingredientnames.txt"); std::string fname = localisePath("data/ingredientnames.txt");
loadIngredientDisplayNames(fname); loadIngredientDisplayNames(fname);
if(dsq->mod.isActive()) if(dsq->mod.isActive())
{ {
fname = dsq->user.localisePath(dsq->mod.getPath() + "ingredientnames.txt", dsq->mod.getPath()); fname = localisePath(dsq->mod.getPath() + "ingredientnames.txt", dsq->mod.getPath());
loadIngredientDisplayNames(fname); loadIngredientDisplayNames(fname);
} }
@ -3260,13 +3260,13 @@ void Continuity::reset()
if(dsq->mod.isActive()) if(dsq->mod.isActive())
{ {
//load mod ingredients //load mod ingredients
fname = dsq->user.localisePath(dsq->mod.getPath() + "ingredients.txt", dsq->mod.getPath()); fname = localisePath(dsq->mod.getPath() + "ingredients.txt", dsq->mod.getPath());
loadIngredientData(fname); loadIngredientData(fname);
} }
//load ingredients for the main game //load ingredients for the main game
if(ingredientData.empty() && recipes.empty()) { if(ingredientData.empty() && recipes.empty()) {
fname = dsq->user.localisePath("data/ingredients.txt"); fname = localisePath("data/ingredients.txt");
loadIngredientData(fname); loadIngredientData(fname);
} }

View file

@ -485,7 +485,7 @@ void DSQ::loadFonts()
destroyFonts(); destroyFonts();
std::string file = user.localisePath("data/font-small.glf"); std::string file = localisePath("data/font-small.glf");
font.load(file, 1, false); font.load(file, 1, false);
font.fontTopColor = Vector(0.9,0.9,1); font.fontTopColor = Vector(0.9,0.9,1);
@ -513,7 +513,7 @@ void DSQ::loadFonts()
goldFont.overrideTexture = core->addTexture("font"); goldFont.overrideTexture = core->addTexture("font");
file = user.localisePath("data/font.ttf"); file = localisePath("data/font.ttf");
debugLog("ttf..."); debugLog("ttf...");
arialFontData = (unsigned char *)readFile(file, &arialFontDataSize); arialFontData = (unsigned char *)readFile(file, &arialFontDataSize);

View file

@ -7919,29 +7919,29 @@ void Game::toggleHelpScreen(bool on, const std::string &label)
// These say "Mac" but we use them on Linux, too. // These say "Mac" but we use them on Linux, too.
#if defined(BBGE_BUILD_UNIX) #if defined(BBGE_BUILD_UNIX)
std::string fname = dsq->user.localisePath("data/help_header_mac.txt"); std::string fname = localisePath("data/help_header_mac.txt");
appendFileToString(data, fname); appendFileToString(data, fname);
#else #else
std::string fname = dsq->user.localisePath("data/help_header.txt"); std::string fname = localisePath("data/help_header.txt");
appendFileToString(data, fname); appendFileToString(data, fname);
#endif #endif
if (dsq->continuity.hasSong(SONG_BIND)) { if (dsq->continuity.hasSong(SONG_BIND)) {
fname = dsq->user.localisePath("data/help_bindsong.txt"); fname = localisePath("data/help_bindsong.txt");
appendFileToString(data, fname); appendFileToString(data, fname);
} }
if (dsq->continuity.hasSong(SONG_ENERGYFORM)) { if (dsq->continuity.hasSong(SONG_ENERGYFORM)) {
fname = dsq->user.localisePath("data/help_energyform.txt"); fname = localisePath("data/help_energyform.txt");
appendFileToString(data, fname); appendFileToString(data, fname);
} }
fname = dsq->user.localisePath("data/help_start.txt"); fname = localisePath("data/help_start.txt");
appendFileToString(data, fname); appendFileToString(data, fname);
// These say "Mac" but we use them on Linux, too. // These say "Mac" but we use them on Linux, too.
#if defined(BBGE_BUILD_UNIX) #if defined(BBGE_BUILD_UNIX)
fname = dsq->user.localisePath("data/help_end_mac.txt"); fname = localisePath("data/help_end_mac.txt");
appendFileToString(data, fname); appendFileToString(data, fname);
#else #else
fname = dsq->user.localisePath("data/help_end.txt"); fname = localisePath("data/help_end.txt");
appendFileToString(data, fname); appendFileToString(data, fname);
#endif #endif

View file

@ -174,7 +174,7 @@ void StatsAndAchievements::RunFrame()
VFILE *io = NULL; VFILE *io = NULL;
// Get generic achievement data... // Get generic achievement data...
std::string fname = dsq->user.localisePath("data/achievements.txt"); std::string fname = localisePath("data/achievements.txt");
io = vfopen(fname.c_str(), "r"); io = vfopen(fname.c_str(), "r");
char line[1024]; char line[1024];
for (size_t i = 0; i < max_achievements; i++) for (size_t i = 0; i < max_achievements; i++)

View file

@ -34,11 +34,11 @@ void StringBank::load()
_load(dsq->mod.getPath() + "stringbank.txt"); _load(dsq->mod.getPath() + "stringbank.txt");
// Then, load localized ones. If some entries in these are missing, the default for each is taken. // Then, load localized ones. If some entries in these are missing, the default for each is taken.
std::string fname = dsq->user.localisePath("data/stringbank.txt"); std::string fname = localisePath("data/stringbank.txt");
_load(fname); _load(fname);
if (dsq->mod.isActive()) { if (dsq->mod.isActive()) {
fname = dsq->user.localisePath(dsq->mod.getPath() + "stringbank.txt", dsq->mod.getPath()); fname = localisePath(dsq->mod.getPath() + "stringbank.txt", dsq->mod.getPath());
_load(fname); _load(fname);
} }
} }

View file

@ -46,7 +46,7 @@ void SubtitlePlayer::go(const std::string &subs)
if (dsq->mod.isActive()) if (dsq->mod.isActive())
{ {
f = dsq->mod.getPath() + "audio/" + subs + ".txt"; f = dsq->mod.getPath() + "audio/" + subs + ".txt";
f = dsq->user.localisePath(f, dsq->mod.getPath()); f = localisePath(f, dsq->mod.getPath());
f = core->adjustFilenameCase(f); f = core->adjustFilenameCase(f);
if (exists(f)) if (exists(f))
checkAfter = false; checkAfter = false;
@ -55,7 +55,7 @@ void SubtitlePlayer::go(const std::string &subs)
if (checkAfter) if (checkAfter)
{ {
f = "scripts/vox/" + subs + ".txt"; f = "scripts/vox/" + subs + ".txt";
f = dsq->user.localisePath(f); f = localisePath(f);
f = core->adjustFilenameCase(f); f = core->adjustFilenameCase(f);
if (!exists(f)) if (!exists(f))
{ {

View file

@ -28,31 +28,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../ExternalLibs/tinyxml.h" #include "../ExternalLibs/tinyxml.h"
#endif #endif
#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)
{
char buf[1024];
CFStringGetCString(cs, &buf[0], 2048, kCFStringEncodingUTF8);
return &buf[0];
}
#endif
void UserSettings::save() void UserSettings::save()
{ {
@ -560,11 +535,6 @@ void UserSettings::load(bool doApply, const std::string &overrideFile)
network.masterServer = serv; network.masterServer = serv;
} }
if (system.locale.empty())
getSystemLocale();
else
debugLog("use user config locale: " + system.locale);
//clearInputCodeMap(); //clearInputCodeMap();
if (doApply) if (doApply)
@ -606,93 +576,18 @@ void UserSettings::apply()
core->settings.prebufferSounds = audio.prebuffer; core->settings.prebufferSounds = audio.prebuffer;
#endif
}
std::string UserSettings::localisePath(const std::string &path, const std::string &modpath)
{
if (system.locale.empty()) if (system.locale.empty())
return path;
const std::string fname = path.substr(modpath.length());
/* we first try with complete locale name, i.e "locales/en_US/" */
std::string localisedPath = modpath + "locales/" + system.locale + "/" + fname;
if (exists(localisedPath.c_str()))
return localisedPath;
/* ok didn't work, let's retry with only language part of locale name, i.e "locales/en/" */
const size_t found = system.locale.find('_');
/* hmm, seems like we didn't have a full locale name anyway, use original path */
if (found == string::npos)
return path;
localisedPath = modpath + "locales/" + system.locale.substr(0,found) + "/" + fname;
/* hooray we found a file! */
if (exists(localisedPath.c_str()))
return localisedPath;
/* seems like we don't have a localized version of the file available, use original path */
return path;
}
void UserSettings::getSystemLocale()
{
system.isSystemLocale = true;
#ifdef BBGE_BUILD_WINDOWS
LCID lcid = GetThreadLocale();
char buf[100];
char ctry[100];
if (GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, sizeof buf) != 0)
{ {
system.locale = buf; std::string loc = getSystemLocale();
debugLog("Using autodetected system locale: " + loc);
if (GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, ctry, sizeof ctry) != 0) setUsedLocale(loc);
{
system.locale += "_";
system.locale += ctry;
}
} }
#elif BBGE_BUILD_MACOSX
CFLocaleRef locale = CFLocaleCopyCurrent();
CFStringRef buf;
if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleLanguageCode)) != NULL)
{
system.locale = _CFToStdString(buf);
CFRelease(buf);
if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCountryCode)) != NULL)
{
system.locale += "_";
system.locale += _CFToStdString(buf);
CFRelease(buf);
}
}
CFRelease(locale);
#else
const char *lang = (const char *)getenv("LANG");
if (lang && *lang)
{
system.locale = lang;
size_t found = system.locale.find('.');
if (found != string::npos)
system.locale.resize(found);
}
#endif
if (system.locale.empty())
debugLog("could not establish system locale");
else else
debugLog("use system locale: " + system.locale); {
debugLog("Using user config locale: " + system.locale);
setUsedLocale(system.locale);
}
#endif
} }

View file

@ -182,8 +182,4 @@ public:
void load(bool doApply=true, const std::string &overrideFile=""); void load(bool doApply=true, const std::string &overrideFile="");
void save(); void save();
void apply(); void apply();
std::string localisePath(const std::string &path, const std::string &modpath="");
private:
void getSystemLocale();
}; };

View file

@ -39,6 +39,7 @@ BUILD_LINUX
#include "Flags.h" #include "Flags.h"
//#include "Scripting.h" //#include "Scripting.h"
#include "Effects.h" #include "Effects.h"
#include "Localization.h"
#include "DarkLayer.h" #include "DarkLayer.h"

120
BBGE/Localization.cpp Normal file
View file

@ -0,0 +1,120 @@
#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)
{
char buf[1024];
CFStringGetCString(cs, &buf[0], 2048, kCFStringEncodingUTF8);
return &buf[0];
}
#endif
static std::string s_locale;
void setUsedLocale(const std::string& s)
{
s_locale = s;
}
std::string localisePath(const std::string &path, const std::string &modpath /* = "" */)
{
if (s_locale.empty())
return path;
const std::string fname = path.substr(modpath.length());
/* we first try with complete locale name, i.e "locales/en_US/" */
std::string localisedPath = modpath + "locales/" + s_locale + "/" + fname;
if (exists(localisedPath.c_str()))
return localisedPath;
/* 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('_');
/* hmm, seems like we didn't have a full locale name anyway, use original path */
if (found == std::string::npos)
return path;
localisedPath = modpath + "locales/" + s_locale.substr(0,found) + "/" + fname;
/* hooray we found a file! */
if (exists(localisedPath.c_str()))
return localisedPath;
/* seems like we don't have a localized version of the file available, use original path */
return path;
}
std::string getSystemLocale()
{
std::string localeStr;
#ifdef BBGE_BUILD_WINDOWS
LCID lcid = GetThreadLocale();
char buf[100];
char ctry[100];
if (GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, sizeof buf) != 0)
{
localeStr = buf;
if (GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, ctry, sizeof ctry) != 0)
{
localeStr += "_";
localeStr += ctry;
}
}
#elif BBGE_BUILD_MACOSX
CFLocaleRef locale = CFLocaleCopyCurrent();
CFStringRef buf;
if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleLanguageCode)) != NULL)
{
localeStr = _CFToStdString(buf);
CFRelease(buf);
if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCountryCode)) != NULL)
{
system.locale += "_";
system.locale += _CFToStdString(buf);
CFRelease(buf);
}
}
CFRelease(locale);
#else
const char *lang = (const char *)getenv("LANG");
if (lang && *lang)
{
localeStr = lang;
size_t found = system.locale.find('.');
if (found != string::npos)
localeStr.resize(found);
}
#endif
return localeStr;
}

10
BBGE/Localization.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef LOCALIZATION_H
#define LOCALIZATION_H
#include "Base.h"
void setUsedLocale(const std::string& s);
std::string localisePath(const std::string &path, const std::string &modpath = "");
std::string getSystemLocale();
#endif

View file

@ -281,9 +281,6 @@ void Texture::load(std::string file)
return; return;
} }
stringToLowerUserData(file);
file = core->adjustFilenameCase(file);
loadName = file; loadName = file;
repeating = false; repeating = false;
@ -298,26 +295,28 @@ void Texture::load(std::string file)
pos = std::string::npos; pos = std::string::npos;
} }
if (core->debugLogTextures) /*if (core->debugLogTextures)
{ {
std::ostringstream os; std::ostringstream os;
os << "pos [" << pos << "], file :" << file; os << "pos [" << pos << "], file :" << file;
debugLog(os.str()); debugLog(os.str());
}*/
bool found = exists(file);
if(!found && exists(file + ".png"))
{
found = true;
file += ".png";
} }
bool found = exists(file); // .tga/.zga are never used as game graphics anywhere except save slot thumbnails.
// if so, their file names are passed exact, not with a missing extension
if(!found && exists(file + ".png"))
{
found = true;
file += ".png";
}
// .tga/.zga are never used as game graphics anywhere except save slot thumbnails.
// if so, their file names are passed exact, not with a missing extension
if (found) if (found)
{ {
file = localisePath(file);
/* /*
std::ostringstream os; std::ostringstream os;
os << "Loading texture [" << file << "]"; os << "Loading texture [" << file << "]";