diff --git a/src/debug.cpp b/src/debug.cpp index d0e9f05f7b083..ceaaff557690d 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -1282,6 +1282,7 @@ std::string game_info::game_report() for( const options_manager::id_and_option &vItem : options_manager::lang_options ) { if( vItem.first == lang ) { lang_translated = vItem.second.translated(); + break; } } diff --git a/src/options.cpp b/src/options.cpp index aa939df391d97..c605bc079ae69 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -58,25 +58,8 @@ bool tile_iso; std::map TILESETS; // All found tilesets: std::map SOUNDPACKS; // All found soundpacks: -std::vector options_manager::lang_options = { - { "", translate_marker( "System language" ) }, - // Note: language names are in their own language and are *not* translated at all. - // Note: Somewhere in Github PR was better link to msdn.microsoft.com with language names. - // http://en.wikipedia.org/wiki/List_of_language_names - { "en", no_translation( R"(English)" ) }, - { "de", no_translation( R"(Deutsch)" ) }, - { "es_AR", no_translation( R"(Español (Argentina))" ) }, - { "es_ES", no_translation( R"(Español (España))" ) }, - { "fr", no_translation( R"(Français)" ) }, - { "hu", no_translation( R"(Magyar)" ) }, - { "ja", no_translation( R"(日本語)" ) }, - { "ko", no_translation( R"(한국어)" ) }, - { "pl", no_translation( R"(Polski)" ) }, - { "pt_BR", no_translation( R"(Português (Brasil))" )}, - { "ru", no_translation( R"(Русский)" ) }, - { "zh_CN", no_translation( R"(中文 (天朝))" ) }, - { "zh_TW", no_translation( R"(中文 (台灣))" ) }, -}; +const std::vector options_manager::lang_options = + options_manager::get_lang_options(); options_manager &get_options() { @@ -1035,6 +1018,57 @@ std::vector options_manager::build_soundpacks_li return result; } +std::unordered_set options_manager::get_langs_with_translation_files() +{ + std::vector lang_dirs = get_directories_with( PATH_INFO::lang_file(), + PATH_INFO::langdir(), true ); + const std::string start_str = "mo/"; + const std::size_t start_len = start_str.length(); + const std::string end_str = "/LC_MESSAGES"; + std::for_each( lang_dirs.begin(), lang_dirs.end(), [&]( std::string & dir ) { + const std::size_t start = dir.find( start_str ) + start_len; + const std::size_t len = dir.rfind( end_str ) - start; + dir = dir.substr( start, len ); + } ); + return std::unordered_set( lang_dirs.begin(), lang_dirs.end() ); +} + +std::vector options_manager::get_lang_options() +{ + std::vector lang_options = { + { "", translate_marker( "System language" ) }, + // Note: language names are in their own language and are *not* translated at all. + // Note: Somewhere in Github PR was better link to msdn.microsoft.com with language names. + // http://en.wikipedia.org/wiki/List_of_language_names + { "en", no_translation( R"(English)" ) }, + { "de", no_translation( R"(Deutsch)" ) }, + { "es_AR", no_translation( R"(Español (Argentina))" ) }, + { "es_ES", no_translation( R"(Español (España))" ) }, + { "fr", no_translation( R"(Français)" ) }, + { "hu", no_translation( R"(Magyar)" ) }, + { "ja", no_translation( R"(日本語)" ) }, + { "ko", no_translation( R"(한국어)" ) }, + { "pl", no_translation( R"(Polski)" ) }, + { "pt_BR", no_translation( R"(Português (Brasil))" )}, + { "ru", no_translation( R"(Русский)" ) }, + { "zh_CN", no_translation( R"(中文 (天朝))" ) }, + { "zh_TW", no_translation( R"(中文 (台灣))" ) }, + }; + + std::unordered_set lang_list = options_manager::get_langs_with_translation_files(); + + std::vector options; + + lang_list.insert( "" ); // for System language option + lang_list.insert( "en" ); // for English option + + std::copy_if( lang_options.begin(), lang_options.end(), std::back_inserter( options ), + [&lang_list]( const options_manager::id_and_option & pair ) { + return lang_list.count( pair.first ); + } ); + return options; +} + #if defined(__ANDROID__) bool android_get_default_setting( const char *settings_name, bool default_value ) { @@ -1330,7 +1364,6 @@ void options_manager::add_options_interface() interface_page_.items_.emplace_back(); }; - // TODO: scan for languages like we do for tilesets. add( "USE_LANG", "interface", translate_marker( "Language" ), translate_marker( "Switch Language." ), options_manager::lang_options, "" ); diff --git a/src/options.h b/src/options.h index e7c4c822fc28c..79e619920db55 100644 --- a/src/options.h +++ b/src/options.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "translations.h" @@ -29,7 +30,7 @@ class options_manager : std::pair( first, second ) { } }; - static std::vector lang_options; + static const std::vector lang_options; private: static std::vector build_tilesets_list(); static std::vector build_soundpacks_list(); @@ -37,6 +38,8 @@ class options_manager const std::string &path ); static std::vector load_soundpack_from( const std::string &path ); + static std::unordered_set get_langs_with_translation_files(); + static std::vector get_lang_options(); bool load_legacy(); diff --git a/src/path_info.cpp b/src/path_info.cpp index 77c3efc006f62..5e33233746c14 100644 --- a/src/path_info.cpp +++ b/src/path_info.cpp @@ -35,6 +35,7 @@ static std::string autopickup_value; static std::string keymap_value; static std::string options_value; static std::string memorialdir_value; +static std::string langdir_value; void PATH_INFO::init_base_path( std::string path ) { @@ -82,13 +83,16 @@ void PATH_INFO::set_standard_filenames() #if defined(DATA_DIR_PREFIX) datadir_value = base_path_value + "share/cataclysm-dda/"; gfxdir_value = datadir_value + "gfx/"; + langdir_value = datadir_value + "lang/"; #else datadir_value = base_path_value + "data/"; gfxdir_value = base_path_value + "gfx/"; + langdir_value = base_path_value + "lang/"; #endif } else { datadir_value = "data/"; gfxdir_value = "gfx/"; + langdir_value = "lang/"; } // Shared dirs @@ -372,6 +376,14 @@ std::string PATH_INFO::gfxdir() { return gfxdir_value; } +std::string PATH_INFO::langdir() +{ + return langdir_value; +} +std::string PATH_INFO::lang_file() +{ + return "cataclysm-dda.mo"; +} std::string PATH_INFO::data_sound() { return datadir_value + "sound"; diff --git a/src/path_info.h b/src/path_info.h index 50dee5ee4c8c2..79923a7041ac0 100644 --- a/src/path_info.h +++ b/src/path_info.h @@ -57,6 +57,8 @@ std::string worldoptions(); std::string crash(); std::string tileset_conf(); std::string gfxdir(); +std::string langdir(); +std::string lang_file(); std::string user_gfx(); std::string data_sound(); std::string user_sound();