Skip to content

Commit

Permalink
Fully handle languange changes (CleverRaven#19838)
Browse files Browse the repository at this point in the history
* Fully handle language changes

If you changed the language via 'Settings', some strings- some in
the main menu, and others in the 'Settings' window- weren't switched
over to the new language.
  • Loading branch information
sethsimon authored and codemime committed Dec 31, 2016
1 parent 11892bf commit a074722
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 77 deletions.
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ int main(int argc, char *argv[])
DebugLog(D_WARNING, D_MAIN) << "Error while setlocale(LC_ALL, '').";
}

// Options strings loaded with system locale
// Options strings loaded with system locale. Even though set_language calls these, we
// need to call them from here too.
get_options().init();
get_options().load();

set_language(true);
set_language();

// in test mode don't initialize curses to avoid escape sequences being inserted into output stream
if( !test_mode ) {
Expand Down
105 changes: 48 additions & 57 deletions src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,15 @@ std::vector<std::string> main_menu::load_file( const std::string &path,
return result;
}

void main_menu::mmenu_refresh_title()
void main_menu::init_strings()
{
// ASCII Art
mmenu_title = load_file( PATH_INFO::find_translated_file( "titledir", ".title", "title" ),
_( "Cataclysm: Dark Days Ahead" ) );
}

void main_menu::mmenu_refresh_motd()
{
// MOTD
mmenu_motd = load_file( PATH_INFO::find_translated_file( "motddir", ".motd", "motd" ),
_( "No message today." ) );
}

void main_menu::mmenu_refresh_credits()
{
// Credits
mmenu_credits.clear();
std::vector<std::string> buffer;
read_from_file_optional( PATH_INFO::find_translated_file( "creditsdir", ".credits",
Expand Down Expand Up @@ -189,6 +184,46 @@ void main_menu::mmenu_refresh_credits()
if( mmenu_credits.empty() ) {
mmenu_credits.push_back( _( "No credits information found." ) );
}

// fill menu with translated menu items
vMenuItems.clear();
vMenuItems.push_back( pgettext( "Main Menu", "<M|m>OTD" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<N|n>ew Game" ) );
vMenuItems.push_back( pgettext( "Main Menu", "Lo<a|A>d" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<W|w>orld" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<S|s>pecial" ) );
vMenuItems.push_back( pgettext( "Main Menu", "Se<t|T>tings" ) );
vMenuItems.push_back( pgettext( "Main Menu", "H<e|E|?>lp" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<C|c>redits" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<Q|q>uit" ) );

// determine hotkeys from translated menu item text
vMenuHotkeys.clear();
for( const std::string &item : vMenuItems ) {
vMenuHotkeys.push_back( get_hotkeys( item ) );
}

vWorldSubItems.clear();
vWorldSubItems.push_back( pgettext( "Main Menu|World", "<C|c>reate World" ) );
vWorldSubItems.push_back( pgettext( "Main Menu|World", "<D|d>elete World" ) );
vWorldSubItems.push_back( pgettext( "Main Menu|World", "<R|r>eset World" ) );

vWorldHotkeys.clear();
for( const std::string &item : vWorldSubItems ) {
vWorldHotkeys.push_back( get_hotkeys( item ) );
}

vSettingsSubItems.clear();
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<O|o>ptions" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "K<e|E>ybindings" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<A|a>utopickup" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<S|s>afemode" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<C|c>olors" ) );

vSettingsHotkeys.clear();
for( auto item : vSettingsSubItems ) {
vSettingsHotkeys.push_back( get_hotkeys( item ) );
}
}

std::vector<std::string> main_menu::get_hotkeys( const std::string &s )
Expand Down Expand Up @@ -262,46 +297,7 @@ bool main_menu::opening_screen()
// note: if iMenuOffset is changed,
// please update MOTD and credits to indicate how long they can be.

// fill menu with translated menu items
vMenuItems.clear();
vMenuItems.push_back( pgettext( "Main Menu", "<M|m>OTD" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<N|n>ew Game" ) );
vMenuItems.push_back( pgettext( "Main Menu", "Lo<a|A>d" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<W|w>orld" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<S|s>pecial" ) );
vMenuItems.push_back( pgettext( "Main Menu", "Se<t|T>tings" ) );
vMenuItems.push_back( pgettext( "Main Menu", "H<e|E|?>lp" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<C|c>redits" ) );
vMenuItems.push_back( pgettext( "Main Menu", "<Q|q>uit" ) );

// determine hotkeys from (possibly translated) menu item text
vMenuHotkeys.clear();
for( auto item : vMenuItems ) {
vMenuHotkeys.push_back( get_hotkeys( item ) );
}

std::vector<std::string> vWorldSubItems;
vWorldSubItems.push_back( pgettext( "Main Menu|World", "<C|c>reate World" ) );
vWorldSubItems.push_back( pgettext( "Main Menu|World", "<D|d>elete World" ) );
vWorldSubItems.push_back( pgettext( "Main Menu|World", "<R|r>eset World" ) );
std::vector<std::vector<std::string>> vWorldHotkeys;
for( auto item : vWorldSubItems ) {
vWorldHotkeys.push_back( get_hotkeys( item ) );
}

std::vector<std::string> vSettingsSubItems;
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<O|o>ptions" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "K<e|E>ybindings" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<A|a>utopickup" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<S|s>afemode" ) );
vSettingsSubItems.push_back( pgettext( "Main Menu|Settings", "<C|c>olors" ) );

std::vector<std::vector<std::string>> vSettingsHotkeys;
for( auto item : vSettingsSubItems ) {
vSettingsHotkeys.push_back( get_hotkeys( item ) );
}

mmenu_refresh_title();
init_strings();
print_menu( w_open, 0, iMenuOffsetX, iMenuOffsetY );

dirent *dp;
Expand Down Expand Up @@ -338,10 +334,6 @@ bool main_menu::opening_screen()
ctxt.register_action( "ANY_INPUT" );
bool start = false;

// Load MOTD and Credits, load it once as it shouldn't change for the duration of the application being open
mmenu_refresh_motd();
mmenu_refresh_credits();

g->u = player();

// Make [Load Game] the default cursor position if there's game save available
Expand Down Expand Up @@ -610,10 +602,9 @@ bool main_menu::opening_screen()
if( action == "UP" || action == "CONFIRM" ) {
if( sel2 == 0 ) {
get_options().show( true );
// Refresh these since the language may have changed
mmenu_refresh_title();
mmenu_refresh_credits();
mmenu_refresh_motd();
// The language may have changed- gracefully handle this.
init_strings();
print_menu( w_open, sel1, iMenuOffsetX, iMenuOffsetY, ( sel1 != 0 ) );
} else if( sel2 == 1 ) {
ctxt.display_help();
} else if( sel2 == 2 ) {
Expand Down
23 changes: 16 additions & 7 deletions src/main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,24 @@ class main_menu
std::vector<std::string> mmenu_motd;
std::vector<std::string> mmenu_credits;
std::vector<std::string> vMenuItems; // MOTD, New Game, Load Game, etc.
std::vector<std::string> vWorldSubItems;
std::vector< std::vector<std::string> > vWorldHotkeys;
std::vector<std::string> vSettingsSubItems;
std::vector< std::vector<std::string> > vSettingsHotkeys;
std::vector< std::vector<std::string> > vMenuHotkeys; // hotkeys for the vMenuItems

/**
* Does what it sounds like, but this function also exists in order to gracefully handle
* the case where the player goes to the 'Settings' tab and changes the language.
*/
void init_strings();
/** Helper function for @ref init_strings */
std::vector<std::string> load_file( const std::string &path,
const std::string &alt_text ) const;
/** Another helper function for @ref init_strings */
std::vector<std::string> get_hotkeys( const std::string &s );


// Play a sound whenver the user moves left or right in the main menu or its tabs
void on_move() const;

Expand Down Expand Up @@ -61,13 +77,6 @@ class main_menu
void print_menu( WINDOW *w_open, int iSel, const int iMenuOffsetX, int iMenuOffsetY,
bool bShowDDA = true );

std::vector<std::string> load_file( const std::string &path,
const std::string &alt_text ) const;
void mmenu_refresh_title();
void mmenu_refresh_credits();
void mmenu_refresh_motd();
std::vector<std::string> get_hotkeys( const std::string &s );

void display_credits();
};

Expand Down
4 changes: 2 additions & 2 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ void options_manager::init()
optionNames["ru"] = R"(Русский)";
optionNames["zh_CN"] = R"(中文(天朝))";
optionNames["zh_TW"] = R"(中文(台灣))";
add("USE_LANG", "interface", _("Language"), _("Switch Language. Requires restart."),
add("USE_LANG", "interface", _("Language"), _("Switch Language."),
",en,fr,de,it_IT,es_AR,es_ES,ja,ko,pt_BR,pt_PT,ru,zh_CN,zh_TW",
""
);
Expand Down Expand Up @@ -1870,7 +1870,7 @@ void options_manager::show(bool ingame)
}
}
if( lang_changed ) {
set_language(false);
set_language();
}

refresh_tiles( used_tiles_changed, pixel_minimap_height_changed, ingame );
Expand Down
12 changes: 5 additions & 7 deletions src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const char *npgettext( const char *const context, const char *const msgid,
}
}

void set_language( bool reload_options )
void set_language()
{
// Step 1. Setup locale settings.
std::string lang_opt = get_option<std::string>( "USE_LANG" );
Expand Down Expand Up @@ -90,20 +90,18 @@ void set_language( bool reload_options )
textdomain( "cataclysm-dda" );

// Step 3. Reload options strings with right language
if( reload_options ) {
get_options().init();
get_options().load();
}
get_options().init();
get_options().load();
}

#else // !LOCALIZE

#include <cstring> // strcmp
#include <map>

void set_language( bool reload_options )
void set_language()
{
( void ) reload_options; // Cancels MinGW warning on Windows
return;
}

// sanitized message cache
Expand Down
2 changes: 1 addition & 1 deletion src/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ const char *strip_positional_formatting( const char *msgid );
#define npgettext(STRING0, STRING1, STRING2, COUNT) ngettext(STRING1, STRING2, COUNT)

#endif // LOCALIZE
void set_language( bool reload_options );
void set_language();

#endif // _TRANSLATIONS_H_

0 comments on commit a074722

Please sign in to comment.