namespace for Languages, which does not depend on FileNames...

(some small code duplication of code for path lists results)
This commit is contained in:
Paul Licameli 2021-05-21 23:04:06 -04:00
parent e31593c06a
commit ad5f895f65
4 changed files with 63 additions and 38 deletions

View File

@ -75,9 +75,10 @@ LangChoiceDialog::LangChoiceDialog(wxWindow * parent,
wxDialogWrapper(parent, id, title)
{
SetName();
GetLanguages(mLangCodes, mLangNames);
int lang =
make_iterator_range( mLangCodes ).index( GetSystemLanguageCode() );
const auto &paths = FileNames::AudacityPathList();
Languages::GetLanguages(paths, mLangCodes, mLangNames);
int lang = make_iterator_range( mLangCodes )
.index( Languages::GetSystemLanguageCode(paths) );
ShuttleGui S(this, eIsCreating);
@ -105,7 +106,8 @@ void LangChoiceDialog::OnOk(wxCommandEvent & WXUNUSED(event))
int ndx = mChoice->GetSelection();
mLang = mLangCodes[ndx];
wxString slang = GetSystemLanguageCode();
auto slang =
Languages::GetSystemLanguageCode(FileNames::AudacityPathList());
int sndx = make_iterator_range( mLangCodes ).index( slang );
wxString sname;

View File

@ -32,40 +32,40 @@
#include "Languages.h"
#include "audacity/Types.h"
#include "MemoryX.h"
#include "Internat.h"
#include <wx/defs.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/intl.h>
#include <wx/textfile.h>
#include "FileNames.h"
#include <unordered_map>
using LangHash = std::unordered_map<wxString, TranslatableString>;
using ReverseLangHash = std::unordered_map<TranslatableString, wxString>;
static bool TranslationExists(const FilePaths &audacityPathList, wxString code)
static void FindFilesInPathList(const wxString & pattern,
const FilePaths & pathList, FilePaths & results)
{
wxFileName ff;
for (const auto &path : pathList) {
ff = path + wxFILE_SEP_PATH + pattern;
wxDir::GetAllFiles(ff.GetPath(), &results, ff.GetFullName(), wxDIR_FILES);
}
}
static bool TranslationExists(const FilePaths &pathList, wxString code)
{
FilePaths results;
FileNames::FindFilesInPathList(wxString::Format(wxT("%s/audacity.mo"),
code),
audacityPathList,
results);
FindFilesInPathList(code + L"/audacity.mo", pathList, results);
#if defined(__WXMAC__)
FileNames::FindFilesInPathList(wxString::Format(wxT("%s.lproj/audacity.mo"),
code),
audacityPathList,
results);
FindFilesInPathList(code + L".lproj/audacity.mo", pathList, results);
#endif
FileNames::FindFilesInPathList(wxString::Format(wxT("%s/LC_MESSAGES/audacity.mo"),
code),
audacityPathList,
results);
FindFilesInPathList(code + L"/LC_MESSAGES/audacity.mo", pathList, results);
return (results.size() > 0);
}
@ -74,12 +74,14 @@ static bool TranslationExists(const FilePaths &audacityPathList, wxString code)
#include <wx/osx/core/cfstring.h>
#endif
wxString GetSystemLanguageCode()
namespace Languages {
wxString GetSystemLanguageCode(const FilePaths &pathList)
{
wxArrayString langCodes;
TranslatableStrings langNames;
GetLanguages(langCodes, langNames);
GetLanguages(pathList, langCodes, langNames);
int sysLang = wxLocale::GetSystemLanguage();
@ -128,7 +130,7 @@ wxString GetSystemLanguageCode()
return wxT("en");
}
void GetLanguages(
void GetLanguages( FilePaths pathList,
wxArrayString &langCodes, TranslatableStrings &langNames)
{
static const char *const utf8Names[] = {
@ -209,13 +211,15 @@ void GetLanguages(
return localLanguageName;
}();
auto audacityPathList = FileNames::AudacityPathList();
#if defined(__WXGTK__)
FileNames::AddUniquePathToPathList(
wxString::Format(wxT("%s/share/locale"),
wxT(INSTALL_PREFIX)),
audacityPathList);
{
wxFileName pathNorm{ wxString{INSTALL_PREFIX} + L"/share/locale" };
pathNorm.Normalize();
const wxString newPath{ pathNorm.GetFullPath() };
if (pathList.end() ==
std::find(pathList.begin(), pathList.end(), newPath))
pathList.push_back(newPath);
}
#endif
// For each language in our list we look for a corresponding entry in
@ -257,14 +261,14 @@ void GetLanguages(
name = found->second;
}
if (TranslationExists(audacityPathList, fullCode)) {
if (TranslationExists(pathList, fullCode)) {
code = fullCode;
}
if (!tempHash[code].empty())
continue;
if (TranslationExists(audacityPathList, code) || code==wxT("en")) {
if (TranslationExists(pathList, code) || code==wxT("en")) {
tempCodes.push_back(code);
tempNames.push_back(name);
tempHash[code] = name;
@ -281,7 +285,7 @@ void GetLanguages(
wxString code;
code = wxT("en-simple");
auto name = XO("Simplified");
if (TranslationExists(audacityPathList, code) ) {
if (TranslationExists(pathList, code) ) {
tempCodes.push_back(code);
tempNames.push_back(name);
tempHash[code] = name;
@ -309,3 +313,5 @@ void GetLanguages(
langCodes.push_back(reverseHash[tempNames[j]]);
}
}
}

View File

@ -16,10 +16,25 @@ class wxString;
#include "audacity/Types.h"
namespace Languages {
/*!
@param pathList paths to search for .mo files, grouped into subdirectories for the different
languages
@param[out] langCodes two-letter language abbreviations (like "fr") or language and country
(like "pt_BR")
@param[out] langNames corresponding autonyms of those languages (like "Português")
*/
AUDACITY_DLL_API
void GetLanguages(
void GetLanguages( FilePaths pathList,
wxArrayString &langCodes, TranslatableStrings &langNames);
wxString GetSystemLanguageCode();
/*!
@param pathList paths to search for .mo files, grouped into subdirectories for the different languages
*/
AUDACITY_DLL_API
wxString GetSystemLanguageCode(const FilePaths &pathList);
}
#endif // __AUDACITY_LANGUAGES__

View File

@ -102,7 +102,8 @@ void GUIPrefs::GetRangeChoices(
void GUIPrefs::Populate()
{
// First any pre-processing for constructing the GUI.
GetLanguages(mLangCodes, mLangNames);
Languages::GetLanguages(
FileNames::AudacityPathList(), mLangCodes, mLangNames);
GetRangeChoices(&mRangeChoices, &mRangeCodes, &mDefaultRangeIndex);
@ -261,7 +262,8 @@ wxString GUIPrefs::InitLang( wxString langCode )
// Use the system default language if one wasn't specified or if the user selected System.
if (langCode.empty())
{
langCode = GetSystemLanguageCode();
langCode =
Languages::GetSystemLanguageCode(FileNames::AudacityPathList());
}
// Initialize the language
@ -297,7 +299,7 @@ wxString GUIPrefs::SetLang( const wxString & lang )
}
if (!info)
{
result = GetSystemLanguageCode();
result = Languages::GetSystemLanguageCode(FileNames::AudacityPathList());
info = wxLocale::FindLanguageInfo(result);
if (!info)
return result;