Skip to content

Commit

Permalink
Implement language autodetection
Browse files Browse the repository at this point in the history
  • Loading branch information
nextghost committed Mar 15, 2020
1 parent 255de30 commit d315df2
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 60 deletions.
156 changes: 106 additions & 50 deletions signus/src/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// Globalni definice, deklarace a funkce pro Signus


#include <string>
#include "global.h"
#include "events.h"
#include "graphio.h"
Expand All @@ -35,12 +36,14 @@
#include <sys/stat.h>
#include <limits.h>
#include <cstdlib>
#include <clocale>

extern "C" {
#include "iniparser.h"
}


#define DEFAULT_LANG "en"
#define MIN_MEM_NEEDED (1024 * 1024 * 12)
// minim. pamet potrebna pro signus

Expand Down Expand Up @@ -101,6 +104,57 @@ int iniTitledAnims, iniInterpolateAnims;



void detect_language(void) {
std::string lang, path = getSignusDataDir();
const char *env;
size_t pos, baselen;
File testfile;

env = setlocale(LC_MESSAGES, NULL);

if (!env) {
strcpy(iniLocale, DEFAULT_LANG);
return;
}

if (path[path.length() - 1] != '/') {
path += '/';
}

lang = env;

if (lang.length() >= 200) {
lang.erase(200);
}

pos = lang.find('.');
baselen = path.length();

if (pos != std::string::npos) {
lang.erase(pos);
}

while (1) {
path += lang;
path += "/texts.dat";

if (testfile.open(path.c_str(), File::READ)) {
strcpy(iniLocale, lang.c_str());
return;
}

path.erase(baselen);
pos = lang.rfind('_');

if (pos == std::string::npos) {
break;
}

lang.erase(pos);
}

strcpy(iniLocale, DEFAULT_LANG);
}

bool dirExists(const char *filename)
{
Expand All @@ -126,56 +180,58 @@ static const char *GetConfigFileName()
return inifile;
}

bool LoadINI()
{
dictionary *dict = NULL;

const char *configname = GetConfigFileName();
if (fileExists(configname))
dict = iniparser_load((char*)configname);
if (dict == NULL)
dict = iniparser_load(SIGNUS_DATA_DIR "/default_signus.ini");
if (dict == NULL)
{
fprintf(stderr, "Fatal error: cannot read configuration file.\n"
"Please reinstall Signus.\n");
return FALSE;
}

// FIXME -- use locales!!!
strcpy(iniLanguage, "e");

//iniResolution = iniparser_getint(dict, "video:resolution", -1);
iniResolution = SVGA_800x600; // FIXME -- get rid of iniResolution !!

iniBrightCorr = iniparser_getint(dict, "video:brightness", -1);
iniTitledAnims = iniparser_getint(dict, "video:anims_titled", -1);
iniInterpolateAnims = iniparser_getint(dict, "video:anims_interpolated", -1);

iniMusicVol = iniparser_getint(dict, "audio:music_volume", -1);
iniSoundVol = iniparser_getint(dict, "audio:sound_volume", -1);
iniSpeechVol = iniparser_getint(dict, "audio:speech_volume", -1);
iniJukeboxRepeat = iniparser_getint(dict, "audio:jukebox_repeat", -1);
iniJukeboxRandom = iniparser_getint(dict, "audio:jukebox_random_order", -1);
iniJukeboxListSize = iniparser_getint(dict, "audio:jukebox_play_list_size", -1);
iniJukeboxSave = iniparser_getint(dict, "audio:jukebox_save_changes", -1);

iniAnimDelay = iniparser_getint(dict, "interface:anim_delay", -1);
iniAnimDelay2 = iniparser_getint(dict, "interface:anim_delay2", -1);
iniIdleDelay = iniparser_getint(dict, "interface:idle_delay", -1);
iniScrollDelay = iniparser_getint(dict, "interface:scroll_delay", -1);
iniEnhancedGuiOn = iniparser_getint(dict, "interface:enable_anim_gui", -1);
iniShowStatusbar = iniparser_getint(dict, "interface:unit_status_bar", -1);
iniShowMoveRange = iniparser_getint(dict, "interface:unit_move_rng", -1);
iniShowShootRange = iniparser_getint(dict, "interface:unit_shoot_rng", -1);
iniShowVisibRange = iniparser_getint(dict, "interface:unit_visib_rng", -1);
iniStopOnNewEnemy = iniparser_getint(dict, "interface:stop_on_new_enemy", -1);

iniparser_freedict(dict);

strcpy(iniLocale, "en"); // FIXME FIXME FIXME !

return true;
bool LoadINI() {
dictionary *dict = NULL;
const char *configname = GetConfigFileName();

if (fileExists(configname)) {
dict = iniparser_load((char*)configname);
}

if (dict == NULL) {
dict = iniparser_load(SIGNUS_DATA_DIR "/default_signus.ini");
}

if (dict == NULL) {
fprintf(stderr, "Fatal error: cannot read configuration file.\n"
"Please reinstall Signus.\n");
return FALSE;
}

//iniResolution = iniparser_getint(dict, "video:resolution", -1);
iniResolution = SVGA_800x600; // FIXME -- get rid of iniResolution !!

iniBrightCorr = iniparser_getint(dict, "video:brightness", -1);
iniTitledAnims = iniparser_getint(dict, "video:anims_titled", -1);
iniInterpolateAnims = iniparser_getint(dict, "video:anims_interpolated", -1);

iniMusicVol = iniparser_getint(dict, "audio:music_volume", -1);
iniSoundVol = iniparser_getint(dict, "audio:sound_volume", -1);
iniSpeechVol = iniparser_getint(dict, "audio:speech_volume", -1);
iniJukeboxRepeat = iniparser_getint(dict, "audio:jukebox_repeat", -1);
iniJukeboxRandom = iniparser_getint(dict, "audio:jukebox_random_order", -1);
iniJukeboxListSize = iniparser_getint(dict, "audio:jukebox_play_list_size", -1);
iniJukeboxSave = iniparser_getint(dict, "audio:jukebox_save_changes", -1);

iniAnimDelay = iniparser_getint(dict, "interface:anim_delay", -1);
iniAnimDelay2 = iniparser_getint(dict, "interface:anim_delay2", -1);
iniIdleDelay = iniparser_getint(dict, "interface:idle_delay", -1);
iniScrollDelay = iniparser_getint(dict, "interface:scroll_delay", -1);
iniEnhancedGuiOn = iniparser_getint(dict, "interface:enable_anim_gui", -1);
iniShowStatusbar = iniparser_getint(dict, "interface:unit_status_bar", -1);
iniShowMoveRange = iniparser_getint(dict, "interface:unit_move_rng", -1);
iniShowShootRange = iniparser_getint(dict, "interface:unit_shoot_rng", -1);
iniShowVisibRange = iniparser_getint(dict, "interface:unit_visib_rng", -1);
iniStopOnNewEnemy = iniparser_getint(dict, "interface:stop_on_new_enemy", -1);

iniparser_freedict(dict);

// FIXME -- delete iniLanguage, only used to decide whether to use
// subtitles in (missing) animations
strcpy(iniLanguage, "e");
detect_language();

return true;
}


Expand Down
21 changes: 11 additions & 10 deletions signus/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/


#include <clocale>
#include "events.h"
#include "graphio.h"
#include "engtimer.h"
Expand Down Expand Up @@ -120,8 +121,10 @@ static void signus_thread(void *)

extern int InitGlobal();

int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
// Honor system locale
setlocale(LC_ALL, "");

#if 0 // FIXME
bool flagDebug = argc > 1 && strcmp(argv[1], "--debug") == 0;
bool flagServer = argc > 1 && strcmp(argv[1], "--from-crashguard") == 0;
Expand All @@ -135,15 +138,13 @@ int main(int argc, char *argv[])
}
#endif

if (!InitGlobal()) return FALSE;

if (!doInit()) return FALSE;
if (!InitGlobal()) return FALSE;

signus_thread_is_running = TRUE;
signus_thread(NULL);

return 0;
}
if (!doInit()) return FALSE;

signus_thread_is_running = TRUE;
signus_thread(NULL);

return 0;
}

0 comments on commit d315df2

Please sign in to comment.