/********************************************************************** Audacity: A Digital Audio Editor Languages.cpp Dominic Mazzoni *******************************************************************//*! \file Languages.cpp \brief Determine installed languages. Figure out what translations are installed and return a list of language codes (like "es", "fr", or "pt-br") and corresponding language names (like "Español", "Français", and "Português"). We use our own list of translations of language names (i.e. "Français" instead of "French") but we fallback on the language name in wxWidgets if we don't have it listed. This code is designed to work well with all of the current languages, but adapt to any language that wxWidgets supports. Other languages will only be supported if they're added to the database using wxLocale::AddLanguage. But for the most part, this means that somebody could add a NEW translation and have it work immediately. *//*******************************************************************/ #include "Languages.h" #include #include "wxArrayStringEx.h" #include "Internat.h" #include "wxArrayStringEx.h" #include #include #include #include #include #include // for wxSetEnv #include #include using LangHash = std::unordered_map; using ReverseLangHash = std::unordered_map; 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; FindFilesInPathList(code + L"/audacity.mo", pathList, results); #if defined(__WXMAC__) FindFilesInPathList(code + L".lproj/audacity.mo", pathList, results); #endif FindFilesInPathList(code + L"/LC_MESSAGES/audacity.mo", pathList, results); return (results.size() > 0); } #ifdef __WXMAC__ #include #include #endif namespace Languages { wxString GetSystemLanguageCode(const FilePaths &pathList) { wxArrayString langCodes; TranslatableStrings langNames; GetLanguages(pathList, langCodes, langNames); int sysLang = wxLocale::GetSystemLanguage(); const wxLanguageInfo *info; #ifdef __WXMAC__ // PRL: Bug 1227, system language on Mac may not be right because wxW3 is // dependent on country code too in wxLocale::GetSystemLanguage(). if (sysLang == wxLANGUAGE_UNKNOWN) { // wxW3 did a too-specific lookup of language and country, when // there is nothing for that combination; try it by language alone. // The following lines are cribbed from that function. wxCFRef userLocaleRef(CFLocaleCopyCurrent()); wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode))); auto lang = str.AsString(); // Now avoid wxLocale::GetLanguageInfo(), instead calling: info = wxLocale::FindLanguageInfo(lang); } else #endif { info = wxLocale::GetLanguageInfo(sysLang); } if (info) { wxString fullCode = info->CanonicalName; if (fullCode.length() < 2) return wxT("en"); wxString code = fullCode.Left(2); unsigned int i; for(i=0; ifirst); if (!info) { wxASSERT(info != NULL); continue; } wxString fullCode = info->CanonicalName; wxString code = fullCode.Left(2); auto name = Verbatim( info->Description ); // Logic: Languages codes are sometimes hierarchical, with a // general language code and then a subheading. For example, // zh_TW for Traditional Chinese and zh_CN for Simplified // Chinese - but just zh for Chinese in general. First we look // for the full code, like zh_TW. If that doesn't exist, we // look for a code corresponding to the first two letters. // Note that if the language for a fullCode exists but we only // have a name for the short code, we will use the short code's // name but associate it with the full code. This allows someone // to drop in a NEW language and still get reasonable behavior. if (fullCode.length() < 2) continue; auto found = localLanguageName.find( code ); if ( found != end ) { name = found->second; } found = localLanguageName.find( fullCode ); if ( found != end ) { name = found->second; } if (TranslationExists(pathList, fullCode)) { code = fullCode; } if (!tempHash[code].empty()) continue; if (TranslationExists(pathList, code) || code==wxT("en")) { tempCodes.push_back(code); tempNames.push_back(name); tempHash[code] = name; /* wxLogDebug(wxT("code=%s name=%s fullCode=%s name=%s -> %s"), code, localLanguageName[code], fullCode, localLanguageName[fullCode], name);*/ } } // JKC: Adding language for simplified audacity. { wxString code; code = wxT("en-simple"); auto name = XO("Simplified"); if (TranslationExists(pathList, code) ) { tempCodes.push_back(code); tempNames.push_back(name); tempHash[code] = name; } } // Sort unsigned int j; for(j=0; j sLocale; static wxString sLocaleName; wxString SetLang( const FilePaths &pathList, const wxString & lang ) { wxString result = lang; sLocale.reset(); #if defined(__WXMAC__) // This should be reviewed again during the wx3 conversion. // On OSX, if the LANG environment variable isn't set when // using a language like Japanese, an assertion will trigger // because conversion to Japanese from "?" doesn't return a // valid length, so make OSX happy by defining/overriding // the LANG environment variable with U.S. English for now. wxSetEnv(wxT("LANG"), wxT("en_US.UTF-8")); #endif const wxLanguageInfo *info = NULL; if (!lang.empty() && lang != wxT("System")) { // Try to find the given language info = wxLocale::FindLanguageInfo(lang); } if (!info) { // Not given a language or can't find it; substitute the system language result = Languages::GetSystemLanguageCode(pathList); info = wxLocale::FindLanguageInfo(result); if (!info) // Return the substituted system language, but we can't complete setup // Should we try to do something better? return result; } sLocale = std::make_unique(info->Language); for( const auto &path : pathList ) sLocale->AddCatalogLookupPathPrefix( path ); // LL: Must add the wxWidgets catalog manually since the search // paths were not set up when mLocale was created. The // catalogs are search in LIFO order, so add wxstd first. sLocale->AddCatalog(wxT("wxstd")); // Must match TranslationExists() in Languages.cpp sLocale->AddCatalog("audacity"); // Initialize internationalisation (number formats etc.) // // This must go _after_ creating the wxLocale instance because // creating the wxLocale instance sets the application-wide locale. Internat::Init(); using future1 = decltype( // The file of unused strings is part of the source tree scanned by // xgettext when compiling the catalog template audacity.pot. // Including it here doesn't change that but does make the C++ compiler // check for correct syntax, but also generate no object code for them. #include "FutureStrings.h" 0 ); sLocaleName = wxSetlocale(LC_ALL, NULL); return result; } wxString GetLocaleName() { return sLocaleName; } wxString GetLang() { if (sLocale) return sLocale->GetSysName(); else return {}; } wxString GetLangShort() { if (sLocale) return sLocale->GetName(); else return {}; } }