2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file aegisublocale.cpp
|
|
|
|
/// @brief Enumerate available locales for picking translation on Windows
|
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2012-09-22 18:51:39 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2006-01-16 22:02:54 +01:00
|
|
|
#include <locale.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
#include <wx/dir.h>
|
|
|
|
#include <wx/filename.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#include <wx/intl.h>
|
2009-01-12 22:20:53 +01:00
|
|
|
#include <wx/stdpaths.h>
|
2009-09-11 00:48:29 +02:00
|
|
|
#include <wx/choicdlg.h> // Keep this last so wxUSE_CHOICEDLG is set.
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "aegisublocale.h"
|
|
|
|
#include "standard_paths.h"
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2006-02-02 23:20:49 +01:00
|
|
|
AegisubLocale::~AegisubLocale() {
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-09-22 18:42:32 +02:00
|
|
|
int AegisubLocale::EnglishId() const {
|
|
|
|
static const int english_ids[] = {
|
|
|
|
wxLANGUAGE_ENGLISH,
|
|
|
|
wxLANGUAGE_ENGLISH_US,
|
|
|
|
wxLANGUAGE_ENGLISH_UK,
|
|
|
|
wxLANGUAGE_ENGLISH_AUSTRALIA,
|
|
|
|
wxLANGUAGE_ENGLISH_BELIZE,
|
|
|
|
wxLANGUAGE_ENGLISH_BOTSWANA,
|
|
|
|
wxLANGUAGE_ENGLISH_CANADA,
|
|
|
|
wxLANGUAGE_ENGLISH_CARIBBEAN,
|
|
|
|
wxLANGUAGE_ENGLISH_DENMARK,
|
|
|
|
wxLANGUAGE_ENGLISH_EIRE,
|
|
|
|
wxLANGUAGE_ENGLISH_JAMAICA,
|
|
|
|
wxLANGUAGE_ENGLISH_NEW_ZEALAND,
|
|
|
|
wxLANGUAGE_ENGLISH_PHILIPPINES,
|
|
|
|
wxLANGUAGE_ENGLISH_SOUTH_AFRICA,
|
|
|
|
wxLANGUAGE_ENGLISH_TRINIDAD,
|
|
|
|
wxLANGUAGE_ENGLISH_ZIMBABWE,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const int *id = english_ids; *id; ++id) {
|
|
|
|
if (wxLocale::IsAvailable(*id)) {
|
|
|
|
return *id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
void AegisubLocale::Init(int language) {
|
2012-03-25 06:05:25 +02:00
|
|
|
if (language == -1)
|
2012-09-22 18:42:32 +02:00
|
|
|
language = EnglishId();
|
2009-06-06 23:05:31 +02:00
|
|
|
|
2012-03-25 06:05:25 +02:00
|
|
|
if (!wxLocale::IsAvailable(language))
|
2009-06-06 23:05:31 +02:00
|
|
|
language = wxLANGUAGE_UNKNOWN;
|
|
|
|
|
2012-03-25 06:05:25 +02:00
|
|
|
locale.reset(new wxLocale(language));
|
2009-01-06 11:44:49 +01:00
|
|
|
|
2006-05-10 02:33:44 +02:00
|
|
|
#ifdef __WINDOWS__
|
2011-09-28 21:43:11 +02:00
|
|
|
locale->AddCatalogLookupPathPrefix(StandardPaths::DecodePath("?data/locale/"));
|
|
|
|
locale->AddCatalog("aegisub");
|
2009-01-06 11:44:49 +01:00
|
|
|
#else
|
2012-08-19 23:33:50 +02:00
|
|
|
locale->AddCatalog(AEGISUB_COMMAND);
|
2009-01-06 11:44:49 +01:00
|
|
|
#endif
|
|
|
|
|
2011-09-28 21:43:11 +02:00
|
|
|
locale->AddCatalog("wxstd");
|
2006-05-06 16:38:53 +02:00
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
setlocale(LC_CTYPE, "C");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int AegisubLocale::PickLanguage() {
|
|
|
|
wxArrayInt langs = GetAvailableLanguages();
|
|
|
|
|
|
|
|
// Check if english is in it, else add it
|
|
|
|
if (langs.Index(wxLANGUAGE_ENGLISH) == wxNOT_FOUND) {
|
2012-09-22 18:42:32 +02:00
|
|
|
int id = EnglishId();
|
|
|
|
if (id)
|
|
|
|
langs.Insert(id, 0);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user local language is available, if so, make it first
|
|
|
|
int user = wxLocale::GetSystemLanguage();
|
|
|
|
if (langs.Index(user) != wxNOT_FOUND) {
|
|
|
|
langs.Remove(user);
|
2012-03-25 06:05:25 +02:00
|
|
|
langs.Insert(user, 0);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-09-22 18:51:39 +02:00
|
|
|
// Remove languages which won't work due to the locale not being installed
|
|
|
|
langs.erase(remove_if(langs.begin(), langs.end(), not1(std::ptr_fun(&wxLocale::IsAvailable))), langs.end());
|
|
|
|
|
2012-03-25 06:05:25 +02:00
|
|
|
// Nothing to pick
|
|
|
|
if (langs.empty()) return -1;
|
|
|
|
|
|
|
|
// Only one language, so don't bother asking the user
|
|
|
|
if (langs.size() == 1 && !locale)
|
|
|
|
return langs[0];
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
// Generate names
|
|
|
|
wxArrayString langNames;
|
2012-03-25 06:05:25 +02:00
|
|
|
for (size_t i = 0; i < langs.size(); ++i)
|
2006-01-16 22:02:54 +01:00
|
|
|
langNames.Add(wxLocale::GetLanguageName(langs[i]));
|
|
|
|
|
2012-03-25 06:05:25 +02:00
|
|
|
long style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxOK | wxCENTRE;
|
|
|
|
if (locale)
|
|
|
|
style |= wxCANCEL;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-08-03 04:32:30 +02:00
|
|
|
wxSingleChoiceDialog dialog(NULL, "Please choose a language:", "Language", langNames,
|
|
|
|
#if wxCHECK_VERSION(2, 9, 4)
|
|
|
|
(void **)0,
|
|
|
|
#else
|
|
|
|
0,
|
|
|
|
#endif
|
|
|
|
style);
|
2012-03-25 06:05:25 +02:00
|
|
|
if (dialog.ShowModal() == wxID_OK) {
|
|
|
|
int picked = dialog.GetSelection();
|
|
|
|
if (locale && langs[picked] == locale->GetLanguage())
|
|
|
|
return -1;
|
|
|
|
return langs[picked];
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-03-25 06:05:25 +02:00
|
|
|
return -1;
|
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
wxArrayInt AegisubLocale::GetAvailableLanguages() {
|
|
|
|
wxArrayInt final;
|
2009-01-01 03:16:09 +01:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2006-01-16 22:02:54 +01:00
|
|
|
// Open directory
|
2011-09-28 21:43:11 +02:00
|
|
|
wxString folder = StandardPaths::DecodePath("?data/locale/");
|
2007-01-20 20:55:20 +01:00
|
|
|
wxDir dir;
|
|
|
|
if (!dir.Exists(folder)) return final;
|
|
|
|
if (!dir.Open(folder)) return final;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Enumerate folders
|
2012-03-25 06:05:25 +02:00
|
|
|
wxString temp1;
|
|
|
|
for (bool cont = dir.GetFirst(&temp1, "", wxDIR_DIRS); cont; cont = dir.GetNext(&temp1)) {
|
2006-01-16 22:02:54 +01:00
|
|
|
// Check if .so exists inside folder
|
2012-03-25 06:05:25 +02:00
|
|
|
if (wxFileName::FileExists(folder + temp1 + "/aegisub.mo")) {
|
2006-01-16 22:02:54 +01:00
|
|
|
const wxLanguageInfo *lang = wxLocale::FindLanguageInfo(temp1);
|
|
|
|
if (lang) {
|
|
|
|
final.Add(lang->Language);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-01 03:16:09 +01:00
|
|
|
#else
|
2011-09-28 21:43:11 +02:00
|
|
|
const char* langs[] = {
|
|
|
|
"ca",
|
|
|
|
"cs",
|
|
|
|
"da",
|
|
|
|
"de",
|
|
|
|
"el",
|
|
|
|
"es",
|
2012-06-27 03:44:33 +02:00
|
|
|
"eu",
|
2012-09-17 04:12:05 +02:00
|
|
|
"fa",
|
2011-09-28 21:43:11 +02:00
|
|
|
"fi",
|
|
|
|
"fr_FR",
|
|
|
|
"hu",
|
2012-09-17 04:12:05 +02:00
|
|
|
"id",
|
2011-09-28 21:43:11 +02:00
|
|
|
"it",
|
|
|
|
"ja",
|
|
|
|
"ko",
|
2012-09-17 04:12:05 +02:00
|
|
|
"pl",
|
2011-09-28 21:43:11 +02:00
|
|
|
"pt_BR",
|
2012-09-17 04:12:05 +02:00
|
|
|
"pt_PT",
|
2011-09-28 21:43:11 +02:00
|
|
|
"ru",
|
2012-09-17 04:12:05 +02:00
|
|
|
"sr_RS",
|
|
|
|
"sr_RS@latin",
|
2011-09-28 21:43:11 +02:00
|
|
|
"vi",
|
|
|
|
"zh_CN",
|
|
|
|
"zh_TW"
|
2009-01-01 06:37:47 +01:00
|
|
|
};
|
|
|
|
|
2011-09-28 21:43:11 +02:00
|
|
|
size_t len = sizeof(langs)/sizeof(char*);
|
2009-01-01 06:37:47 +01:00
|
|
|
for (size_t i=0; i<len; i++) {
|
2009-01-01 03:16:09 +01:00
|
|
|
const wxLanguageInfo *lang = wxLocale::FindLanguageInfo(langs[i]);
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
// If the locale file doesn't exist then don't list it as an option.
|
2009-01-12 22:20:53 +01:00
|
|
|
wxString locDir = wxStandardPaths::Get().GetLocalizedResourcesDir(langs[i], wxStandardPathsBase::ResourceCat_Messages);
|
2012-08-19 23:33:50 +02:00
|
|
|
wxFileName file(wxString::Format("%s/%s.mo", locDir, AEGISUB_COMMAND));
|
2009-01-12 22:20:53 +01:00
|
|
|
if (lang && file.FileExists()) final.Add(lang->Language);
|
|
|
|
}
|
2009-01-01 03:16:09 +01:00
|
|
|
#endif
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
return final;
|
|
|
|
}
|