From 41f3a89063dc957a998c158471dff23759eb6d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Wed, 1 Nov 2023 19:09:22 +0100 Subject: [PATCH] Normalize OTA translation requests Normalize language update requests to minimize cache misses and avoid re-downloads on minor version updates: - only use major.minor version number for version - normalize language code into xx[_YY] locale form --- src/edapp.cpp | 25 +++++++++++++++++++++---- src/edapp.h | 5 +++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/edapp.cpp b/src/edapp.cpp index 15821aaef7..08bdc4a65b 100644 --- a/src/edapp.cpp +++ b/src/edapp.cpp @@ -293,6 +293,12 @@ wxString PoeditApp::GetAppVersion() const return wxString::FromAscii(POEDIT_VERSION); } +wxString PoeditApp::GetMajorAppVersion() const +{ + auto v = wxSplit(GetAppVersion(), '.'); + return wxString::Format("%s.%s", v[0], v[1]); +} + wxString PoeditApp::GetAppBuildNumber() const { #if defined(__WXOSX__) @@ -631,18 +637,29 @@ void PoeditApp::SetupLanguage() #endif #ifdef SUPPORTS_OTA_UPDATES - SetupOTALanguageUpdate(trans, str::to_utf8(bestTrans)); + SetupOTALanguageUpdate(trans, bestTrans); #endif } #ifdef SUPPORTS_OTA_UPDATES -void PoeditApp::SetupOTALanguageUpdate(wxTranslations *trans, const std::string& lang) +void PoeditApp::SetupOTALanguageUpdate(wxTranslations *trans, const wxString& lang) { if (lang == "en") return; + // normalize language code for requests + wxString langMO(lang); + if (langMO == "zh-Hans") + langMO = "zh_CN"; + else if (langMO == "zh-Hant") + langMO = "zh_TW"; + else + langMO.Replace("-", "_"); + + auto version = str::to_utf8(GetMajorAppVersion()); + // use downloaded OTA translations: - auto dir = GetCacheDir("Translations") + "/" + POEDIT_VERSION; + auto dir = GetCacheDir("Translations") + "/" + version; wxFileTranslationsLoader::AddCatalogLookupPathPrefix(dir); trans->AddCatalog("poedit-ota"); @@ -663,7 +680,7 @@ void PoeditApp::SetupOTALanguageUpdate(wxTranslations *trans, const std::string& if (!etag.empty()) hdrs.emplace_back("If-None-Match", etag); - http_client::download_from_anywhere("https://ota.poedit.net/i18n/" POEDIT_VERSION "/" + lang + "/poedit-ota.mo.gz", hdrs) + http_client::download_from_anywhere("https://ota.poedit.net/i18n/" + version + "/" + str::to_utf8(langMO) + "/poedit-ota.mo.gz", hdrs) .then_on_main([=](downloaded_file f) { TempOutputFileFor temp(mofile); diff --git a/src/edapp.h b/src/edapp.h index 87f35ebd8d..1fc253f95a 100644 --- a/src/edapp.h +++ b/src/edapp.h @@ -62,7 +62,8 @@ class PoeditApp : public wxApp, public MenusManager static wxString GetCacheDir(const wxString& category); /// Returns Poedit version string. - wxString GetAppVersion() const; + wxString GetAppVersion() const; // e.g. "3.4.2" + wxString GetMajorAppVersion() const; // e.g. "3.4" wxString GetAppBuildNumber() const; bool CheckForBetaUpdates() const; @@ -108,7 +109,7 @@ class PoeditApp : public wxApp, public MenusManager void SetupLanguage(); #ifdef SUPPORTS_OTA_UPDATES - void SetupOTALanguageUpdate(wxTranslations *trans, const std::string& lang); + void SetupOTALanguageUpdate(wxTranslations *trans, const wxString& lang); #endif // App-global menu commands: