Clean up AegisubLocale a bit

Don't let the user cancel the language selection dialog on first
startup, and don't bother with the dialog at all if there's only one
language available.

Originally committed to SVN as r6602.
This commit is contained in:
Thomas Goyne 2012-03-25 04:05:25 +00:00
parent 0b9a21bf82
commit e120bec4f0
3 changed files with 36 additions and 72 deletions

View File

@ -34,9 +34,6 @@
/// @ingroup utility /// @ingroup utility
/// ///
///////////
// Headers
#include "config.h" #include "config.h"
#ifndef AGI_PRE #ifndef AGI_PRE
@ -52,34 +49,17 @@
#include "aegisublocale.h" #include "aegisublocale.h"
#include "standard_paths.h" #include "standard_paths.h"
/// @brief Constructor
///
AegisubLocale::AegisubLocale () {
locale = NULL;
curCode = -1;
}
/// @brief DOCME
///
AegisubLocale::~AegisubLocale() { AegisubLocale::~AegisubLocale() {
delete locale;
} }
/// @brief Initialize
/// @param language
///
void AegisubLocale::Init(int language) { void AegisubLocale::Init(int language) {
if (language == -1) language = wxLANGUAGE_ENGLISH; if (language == -1)
if (locale) delete locale; language = wxLANGUAGE_ENGLISH;
if (!wxLocale::IsAvailable(language)) { if (!wxLocale::IsAvailable(language))
language = wxLANGUAGE_UNKNOWN; language = wxLANGUAGE_UNKNOWN;
}
curCode = language; locale.reset(new wxLocale(language));
locale = new wxLocale(language);
#ifdef __WINDOWS__ #ifdef __WINDOWS__
locale->AddCatalogLookupPathPrefix(StandardPaths::DecodePath("?data/locale/")); locale->AddCatalogLookupPathPrefix(StandardPaths::DecodePath("?data/locale/"));
@ -93,52 +73,52 @@ void AegisubLocale::Init(int language) {
setlocale(LC_CTYPE, "C"); setlocale(LC_CTYPE, "C");
} }
/// @brief Pick a language
/// @return
///
int AegisubLocale::PickLanguage() { int AegisubLocale::PickLanguage() {
// Get list
wxArrayInt langs = GetAvailableLanguages(); wxArrayInt langs = GetAvailableLanguages();
// Check if english is in it, else add it // Check if english is in it, else add it
if (langs.Index(wxLANGUAGE_ENGLISH) == wxNOT_FOUND) { if (langs.Index(wxLANGUAGE_ENGLISH) == wxNOT_FOUND) {
langs.Insert(wxLANGUAGE_ENGLISH,0); langs.Insert(wxLANGUAGE_ENGLISH, 0);
} }
// Check if user local language is available, if so, make it first // Check if user local language is available, if so, make it first
int user = wxLocale::GetSystemLanguage(); int user = wxLocale::GetSystemLanguage();
if (langs.Index(user) != wxNOT_FOUND) { if (langs.Index(user) != wxNOT_FOUND) {
langs.Remove(user); langs.Remove(user);
langs.Insert(user,0); langs.Insert(user, 0);
}
// Generate names
wxArrayString langNames;
for (size_t i=0;i<langs.Count();i++) {
langNames.Add(wxLocale::GetLanguageName(langs[i]));
} }
// Nothing to pick // Nothing to pick
if (langs.Count() == 0) return -1; if (langs.empty()) return -1;
// Popup // Only one language, so don't bother asking the user
int picked = wxGetSingleChoiceIndex("Please choose a language:","Language",langNames,NULL,-1,-1,true,300,400); if (langs.size() == 1 && !locale)
if (picked == -1) return -1; return langs[0];
// Generate names
wxArrayString langNames;
for (size_t i = 0; i < langs.size(); ++i)
langNames.Add(wxLocale::GetLanguageName(langs[i]));
long style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxOK | wxCENTRE;
if (locale)
style |= wxCANCEL;
wxSingleChoiceDialog dialog(NULL, "Please choose a language:", "Language", langNames, 0, style);
if (dialog.ShowModal() == wxID_OK) {
int picked = dialog.GetSelection();
if (locale && langs[picked] == locale->GetLanguage())
return -1;
return langs[picked]; return langs[picked];
}
return -1;
} }
/// @brief Get list of available languages
///
wxArrayInt AegisubLocale::GetAvailableLanguages() { wxArrayInt AegisubLocale::GetAvailableLanguages() {
wxArrayInt final; wxArrayInt final;
#ifdef __WINDOWS__ #ifdef __WINDOWS__
wxString temp1;
// Open directory // Open directory
wxString folder = StandardPaths::DecodePath("?data/locale/"); wxString folder = StandardPaths::DecodePath("?data/locale/");
wxDir dir; wxDir dir;
@ -146,19 +126,17 @@ wxArrayInt AegisubLocale::GetAvailableLanguages() {
if (!dir.Open(folder)) return final; if (!dir.Open(folder)) return final;
// Enumerate folders // Enumerate folders
for (bool cont = dir.GetFirst(&temp1,"",wxDIR_DIRS);cont;cont = dir.GetNext(&temp1)) { wxString temp1;
for (bool cont = dir.GetFirst(&temp1, "", wxDIR_DIRS); cont; cont = dir.GetNext(&temp1)) {
// Check if .so exists inside folder // Check if .so exists inside folder
wxFileName file(folder + temp1 + "/aegisub.mo"); if (wxFileName::FileExists(folder + temp1 + "/aegisub.mo")) {
if (file.FileExists()) {
const wxLanguageInfo *lang = wxLocale::FindLanguageInfo(temp1); const wxLanguageInfo *lang = wxLocale::FindLanguageInfo(temp1);
if (lang) { if (lang) {
final.Add(lang->Language); final.Add(lang->Language);
} }
} }
} }
#else #else
const char* langs[] = { const char* langs[] = {
"ca", "ca",
"cs", "cs",

View File

@ -34,33 +34,20 @@
/// @ingroup utility /// @ingroup utility
/// ///
#include <libaegisub/scoped_ptr.h>
//////////////
// Prototypes
class wxLocale; class wxLocale;
/// DOCME /// DOCME
/// @class AegisubLocale /// @class AegisubLocale
/// @brief DOCME /// @brief DOCME
/// ///
/// DOCME /// DOCME
class AegisubLocale { class AegisubLocale {
private: agi::scoped_ptr<wxLocale> locale;
/// DOCME
wxLocale *locale;
wxArrayInt GetAvailableLanguages(); wxArrayInt GetAvailableLanguages();
public: public:
/// DOCME
int curCode;
AegisubLocale();
~AegisubLocale(); ~AegisubLocale();
void Init(int language); void Init(int language);
int PickLanguage(); int PickLanguage();

View File

@ -185,15 +185,14 @@ struct app_language : public Command {
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
// Get language // Get language
int old = wxGetApp().locale.curCode;
int newCode = wxGetApp().locale.PickLanguage(); int newCode = wxGetApp().locale.PickLanguage();
// Is OK? // Is OK?
if (newCode != -1 && newCode != old) { if (newCode != -1) {
// Set code // Set code
OPT_SET("App/Locale")->SetInt(newCode); OPT_SET("App/Locale")->SetInt(newCode);
// Ask to restart program // Ask to restart program
int result = wxMessageBox("Aegisub needs to be restarted so that the new language can be applied. Restart now?","Restart Aegisub?",wxICON_QUESTION | wxYES_NO); int result = wxMessageBox("Aegisub needs to be restarted so that the new language can be applied. Restart now?", "Restart Aegisub?", wxICON_QUESTION | wxYES_NO);
if (result == wxYES) { if (result == wxYES) {
// Restart Aegisub // Restart Aegisub
if (wxGetApp().frame->Close()) { if (wxGetApp().frame->Close()) {