From: Christoph Sommer Date: Sun, 24 Jun 2007 10:20:19 +0000 (+0000) Subject: Added menu option to select language X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=f171cfbf82ac235d60cc53d24bf9be6cc5365550;p=supertux.git Added menu option to select language SVN-Revision: 5113 --- diff --git a/src/gameconfig.cpp b/src/gameconfig.cpp index a1a8a3da4..2373d700d 100644 --- a/src/gameconfig.cpp +++ b/src/gameconfig.cpp @@ -48,6 +48,8 @@ Config::Config() aspect_ratio = -1; // autodetect enable_script_debugger = false; + + locale = ""; // autodetect } Config::~Config() @@ -65,6 +67,7 @@ Config::load() config_lisp->get("show_fps", show_fps); config_lisp->get("console", console_enabled); + config_lisp->get("locale", locale); config_lisp->get("random_seed", random_seed); const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video"); @@ -97,6 +100,7 @@ Config::save() writer.write_bool("show_fps", show_fps); writer.write_bool("console", console_enabled); + writer.write_string("locale", locale); writer.start_list("video"); writer.write_bool("fullscreen", use_fullscreen); diff --git a/src/gameconfig.hpp b/src/gameconfig.hpp index f4cb884c6..4263bda55 100644 --- a/src/gameconfig.hpp +++ b/src/gameconfig.hpp @@ -52,6 +52,8 @@ public: bool enable_script_debugger; std::string start_demo; std::string record_demo; + + std::string locale; /**< force SuperTux language to this locale, e.g. "de". A file "data/locale/xx.po" must exist for this to work. An empty string means autodetect. */ }; extern Config* config; diff --git a/src/lisp/parser.cpp b/src/lisp/parser.cpp index 52e87b297..a558adc1a 100644 --- a/src/lisp/parser.cpp +++ b/src/lisp/parser.cpp @@ -30,6 +30,8 @@ #include "lisp.hpp" #include "obstack/obstackpp.hpp" +#include "gameconfig.hpp" + namespace lisp { @@ -39,6 +41,7 @@ Parser::Parser(bool translate) if(translate) { dictionary_manager = new TinyGetText::DictionaryManager(); dictionary_manager->set_charset("UTF-8"); + if (config && (config->locale != "")) dictionary_manager->set_language(config->locale); } obstack_init(&obst); diff --git a/src/main.cpp b/src/main.cpp index 495e68d84..8780ddca8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,6 +76,11 @@ static void init_tinygettext() { dictionary_manager.add_directory("locale"); dictionary_manager.set_charset("UTF-8"); + + // Config setting "locale" overrides language detection + if (config->locale != "") { + dictionary_manager.set_language( config->locale ); + } } static void init_physfs(const char* argv0) @@ -239,10 +244,7 @@ static bool pre_parse_commandline(int argc, char** argv) for(int i = 1; i < argc; ++i) { std::string arg = argv[i]; - if(arg == "--help") { - print_usage(argv[0]); - return true; - } else if(arg == "--version") { + if(arg == "--version") { std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl; return true; } @@ -259,7 +261,10 @@ static bool parse_commandline(int argc, char** argv) for(int i = 1; i < argc; ++i) { std::string arg = argv[i]; - if(arg == "--fullscreen" || arg == "-f") { + if(arg == "--help") { + print_usage(argv[0]); + return true; + } else if(arg == "--fullscreen" || arg == "-f") { config->use_fullscreen = true; } else if(arg == "--window" || arg == "-w") { config->use_fullscreen = false; diff --git a/src/options_menu.cpp b/src/options_menu.cpp index 509c85d16..a87676e01 100644 --- a/src/options_menu.cpp +++ b/src/options_menu.cpp @@ -35,6 +35,52 @@ enum OptionsMenuIDs { MNID_MUSIC }; +class LanguageMenu : public Menu +{ +public: + LanguageMenu() { + add_label(_("Language")); + add_hl(); + add_entry(0, std::string("(")+_("auto-detect language")+")"); + + int mnid = 10; + std::set languages = dictionary_manager.get_languages(); + for (std::set::iterator i = languages.begin(); i != languages.end(); i++) { + std::string locale_name = *i; + TinyGetText::LanguageDef ldef = TinyGetText::get_language_def(locale_name); + std::string locale_fullname = locale_name; + if (std::string(ldef.code) == locale_name) { + locale_fullname = ldef.name; + } + add_entry(mnid++, locale_fullname); + } + + add_hl(); + add_back(_("Back")); + } + + virtual void menu_action(MenuItem* item) { + if (item->id == 0) { + config->locale = ""; + dictionary_manager.set_language(config->locale); + config->save(); + Menu::set_current(0); + } + int mnid = 10; + std::set languages = dictionary_manager.get_languages(); + for (std::set::iterator i = languages.begin(); i != languages.end(); i++) { + std::string locale_name = *i; + if (item->id == mnid++) { + config->locale = locale_name; + dictionary_manager.set_language(config->locale); + config->save(); + Menu::set_current(0); + } + } + } +}; + + class OptionsMenu : public Menu { public: @@ -42,13 +88,20 @@ public: virtual ~OptionsMenu(); virtual void menu_action(MenuItem* item); + +protected: + std::auto_ptr language_menu; + }; OptionsMenu::OptionsMenu() { + language_menu.reset(new LanguageMenu()); + add_label(_("Options")); add_hl(); add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen); + add_submenu(_("Language"), language_menu.get()); if (sound_manager->is_audio_enabled()) { add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled); add_toggle(MNID_MUSIC, _("Music"), config->music_enabled); @@ -98,8 +151,8 @@ OptionsMenu::menu_action(MenuItem* item) Menu* get_options_menu() { - if(options_menu == NULL) - options_menu = new OptionsMenu(); + //if(options_menu == NULL) + options_menu = new OptionsMenu(); return options_menu; } diff --git a/src/title.cpp b/src/title.cpp index 2a34b101e..a53f8aba5 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -314,9 +314,10 @@ TitleScreen::check_addons_menu() if (!addon.isInstalled) { try { addon.install(); - generate_addons_menu(); - Menu::set_current(addons_menu.get()); - addons_menu->set_active_item(index); + //generate_addons_menu(); + //Menu::set_current(addons_menu.get()); + //addons_menu->set_active_item(index); + Menu::set_current(0); } catch (std::runtime_error e) { log_warning << "Installation of Add-on failed: " << e.what() << std::endl; @@ -324,9 +325,10 @@ TitleScreen::check_addons_menu() } else { try { addon.remove(); - generate_addons_menu(); - Menu::set_current(addons_menu.get()); - addons_menu->set_active_item(index); + //generate_addons_menu(); + //Menu::set_current(addons_menu.get()); + //addons_menu->set_active_item(index); + Menu::set_current(0); } catch (std::runtime_error e) { log_warning << "Removal of Add-on failed: " << e.what() << std::endl; @@ -378,6 +380,12 @@ TitleScreen::TitleScreen() player->set_controller(controller.get()); player->set_speedlimit(230); //MAX_WALK_XM + generate_main_menu(); +} + +void +TitleScreen::generate_main_menu() +{ main_menu.reset(new Menu()); main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35); main_menu->add_entry(MNID_STARTGAME, _("Start Game")); @@ -506,6 +514,7 @@ TitleScreen::update(float elapsed_time) // reopen menu of user closed it (so that the app doesn't close when user // accidently hit ESC) if(Menu::current() == 0) { + generate_main_menu(); Menu::set_current(main_menu.get()); } } diff --git a/src/title.hpp b/src/title.hpp index c0748b6d2..466ec0e9e 100644 --- a/src/title.hpp +++ b/src/title.hpp @@ -50,6 +50,7 @@ private: bool process_load_game_menu(); void make_tux_jump(); void update_load_game_menu(); + void generate_main_menu(); void generate_contrib_menu(); void check_levels_contrib_menu(); void check_contrib_world_menu();