Override GetPreferredUILanguages with WXLANGUAGE
Add support for overriding GetPreferredUILanguages() return value by setting the WXLANGUAGE environment variable to a colon-separated list of desired languages (same as GNU's LANGUAGE variable). Primarily used for unit tests, but may be more generally useful.
This commit is contained in:
parent
f248f0593c
commit
1f658ff729
2 changed files with 35 additions and 1 deletions
|
|
@ -51,6 +51,12 @@ wxWidgets programs.
|
||||||
default value if it's not a number, so that e.g. setting it to "yes"
|
default value if it's not a number, so that e.g. setting it to "yes"
|
||||||
suppresses all GTK diagnostics while setting it to 16 only suppresses
|
suppresses all GTK diagnostics while setting it to 16 only suppresses
|
||||||
GTK warning messages.}
|
GTK warning messages.}
|
||||||
|
@itemdef{WXLANGUAGE,
|
||||||
|
This variable can be set to override OS setting of preferred languages
|
||||||
|
and make wxUILocale::GetPreferredUILanguages() return the set list
|
||||||
|
instead. The format is same as GNU's <a
|
||||||
|
href="https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html">LANGUAGE</a>
|
||||||
|
variable: a colon-separated list of language codes.}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@see wxSystemOptions
|
@see wxSystemOptions
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@
|
||||||
|
|
||||||
#include "wx/arrstr.h"
|
#include "wx/arrstr.h"
|
||||||
#include "wx/intl.h"
|
#include "wx/intl.h"
|
||||||
|
#include "wx/log.h"
|
||||||
|
#include "wx/tokenzr.h"
|
||||||
|
#include "wx/utils.h"
|
||||||
|
|
||||||
#ifndef __WINDOWS__
|
#ifndef __WINDOWS__
|
||||||
#include "wx/language.h"
|
#include "wx/language.h"
|
||||||
|
|
@ -31,6 +34,8 @@
|
||||||
|
|
||||||
#include "wx/private/uilocale.h"
|
#include "wx/private/uilocale.h"
|
||||||
|
|
||||||
|
#define TRACE_I18N wxS("i18n")
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// helper functions
|
// helper functions
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
@ -678,7 +683,7 @@ int wxUILocale::GetSystemLanguage()
|
||||||
{
|
{
|
||||||
const wxLanguageInfos& languagesDB = wxGetLanguageInfos();
|
const wxLanguageInfos& languagesDB = wxGetLanguageInfos();
|
||||||
size_t count = languagesDB.size();
|
size_t count = languagesDB.size();
|
||||||
wxVector<wxString> preferred = wxUILocaleImpl::GetPreferredUILanguages();
|
wxVector<wxString> preferred = wxUILocale::GetPreferredUILanguages();
|
||||||
|
|
||||||
for (wxVector<wxString>::const_iterator j = preferred.begin();
|
for (wxVector<wxString>::const_iterator j = preferred.begin();
|
||||||
j != preferred.end();
|
j != preferred.end();
|
||||||
|
|
@ -743,6 +748,29 @@ int wxUILocale::GetSystemLocale()
|
||||||
/* static */
|
/* static */
|
||||||
wxVector<wxString> wxUILocale::GetPreferredUILanguages()
|
wxVector<wxString> wxUILocale::GetPreferredUILanguages()
|
||||||
{
|
{
|
||||||
|
// The WXLANGUAGE variable may contain a colon separated list of language
|
||||||
|
// codes in the order of preference. It is modelled after GNU's LANGUAGE:
|
||||||
|
// http://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
|
||||||
|
wxString languageFromEnv;
|
||||||
|
if (wxGetEnv("WXLANGUAGE", &languageFromEnv) && !languageFromEnv.empty())
|
||||||
|
{
|
||||||
|
wxVector<wxString> preferred;
|
||||||
|
wxStringTokenizer tknzr(languageFromEnv, ":");
|
||||||
|
while (tknzr.HasMoreTokens())
|
||||||
|
{
|
||||||
|
const wxString tok = tknzr.GetNextToken();
|
||||||
|
if (const wxLanguageInfo* li = wxUILocale::FindLanguageInfo(tok))
|
||||||
|
{
|
||||||
|
preferred.push_back(li->CanonicalName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!preferred.empty())
|
||||||
|
{
|
||||||
|
wxLogTrace(TRACE_I18N, " - using languages override from WXLANGUAGE: '%s'", languageFromEnv);
|
||||||
|
return preferred;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return wxUILocaleImpl::GetPreferredUILanguages();
|
return wxUILocaleImpl::GetPreferredUILanguages();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue