Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan for languages #40056

Merged
merged 6 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,9 +1279,10 @@ std::string game_info::game_report()

std::string lang = get_option<std::string>( "USE_LANG" );
std::string lang_translated;
for( const options_manager::id_and_option &vItem : options_manager::lang_options ) {
for( const options_manager::id_and_option &vItem : options_manager::actual_lang_options ) {
if( vItem.first == lang ) {
lang_translated = vItem.second.translated();
break;
}
}

Expand Down
75 changes: 54 additions & 21 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,8 @@ bool tile_iso;
std::map<std::string, std::string> TILESETS; // All found tilesets: <name, tileset_dir>
std::map<std::string, std::string> SOUNDPACKS; // All found soundpacks: <name, soundpack_dir>

std::vector<options_manager::id_and_option> 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::id_and_option> options_manager::actual_lang_options =
options_manager::get_actual_lang_options();

options_manager &get_options()
{
Expand Down Expand Up @@ -1035,6 +1018,57 @@ std::vector<options_manager::id_and_option> options_manager::build_soundpacks_li
return result;
}

std::unordered_set<std::string> options_manager::get_lang_list()
akirashirosawa marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<std::string> 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<std::string>( lang_dirs.begin(), lang_dirs.end() );
}

std::vector<options_manager::id_and_option> options_manager::get_actual_lang_options()
{
std::vector<options_manager::id_and_option> 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<std::string> lang_list = options_manager::get_lang_list();

std::vector<options_manager::id_and_option> 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 )
{
Expand Down Expand Up @@ -1330,9 +1364,8 @@ 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, "" );
translate_marker( "Switch Language." ), options_manager::actual_lang_options, "" );

add_empty_line();

Expand Down
5 changes: 4 additions & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <unordered_map>
#include <utility>
#include <vector>
#include <unordered_set>
#include <tuple>

#include "translations.h"
Expand All @@ -29,14 +30,16 @@ class options_manager
: std::pair<std::string, translation>( first, second ) {
}
};
static std::vector<id_and_option> lang_options;
static const std::vector<id_and_option> actual_lang_options;
akirashirosawa marked this conversation as resolved.
Show resolved Hide resolved
private:
static std::vector<id_and_option> build_tilesets_list();
static std::vector<id_and_option> build_soundpacks_list();
static std::vector<id_and_option> load_tilesets_from(
const std::string &path );
static std::vector<id_and_option> load_soundpack_from(
const std::string &path );
static std::unordered_set<std::string> get_lang_list();
static std::vector<id_and_option> get_actual_lang_options();

bool load_legacy();

Expand Down
12 changes: 12 additions & 0 deletions src/path_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions src/path_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down