Skip to content

Commit 41f3a89

Browse files
committed
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
1 parent 0f13c8f commit 41f3a89

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/edapp.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ wxString PoeditApp::GetAppVersion() const
293293
return wxString::FromAscii(POEDIT_VERSION);
294294
}
295295

296+
wxString PoeditApp::GetMajorAppVersion() const
297+
{
298+
auto v = wxSplit(GetAppVersion(), '.');
299+
return wxString::Format("%s.%s", v[0], v[1]);
300+
}
301+
296302
wxString PoeditApp::GetAppBuildNumber() const
297303
{
298304
#if defined(__WXOSX__)
@@ -631,18 +637,29 @@ void PoeditApp::SetupLanguage()
631637
#endif
632638

633639
#ifdef SUPPORTS_OTA_UPDATES
634-
SetupOTALanguageUpdate(trans, str::to_utf8(bestTrans));
640+
SetupOTALanguageUpdate(trans, bestTrans);
635641
#endif
636642
}
637643

638644
#ifdef SUPPORTS_OTA_UPDATES
639-
void PoeditApp::SetupOTALanguageUpdate(wxTranslations *trans, const std::string& lang)
645+
void PoeditApp::SetupOTALanguageUpdate(wxTranslations *trans, const wxString& lang)
640646
{
641647
if (lang == "en")
642648
return;
643649

650+
// normalize language code for requests
651+
wxString langMO(lang);
652+
if (langMO == "zh-Hans")
653+
langMO = "zh_CN";
654+
else if (langMO == "zh-Hant")
655+
langMO = "zh_TW";
656+
else
657+
langMO.Replace("-", "_");
658+
659+
auto version = str::to_utf8(GetMajorAppVersion());
660+
644661
// use downloaded OTA translations:
645-
auto dir = GetCacheDir("Translations") + "/" + POEDIT_VERSION;
662+
auto dir = GetCacheDir("Translations") + "/" + version;
646663
wxFileTranslationsLoader::AddCatalogLookupPathPrefix(dir);
647664
trans->AddCatalog("poedit-ota");
648665

@@ -663,7 +680,7 @@ void PoeditApp::SetupOTALanguageUpdate(wxTranslations *trans, const std::string&
663680
if (!etag.empty())
664681
hdrs.emplace_back("If-None-Match", etag);
665682

666-
http_client::download_from_anywhere("https://ota.poedit.net/i18n/" POEDIT_VERSION "/" + lang + "/poedit-ota.mo.gz", hdrs)
683+
http_client::download_from_anywhere("https://ota.poedit.net/i18n/" + version + "/" + str::to_utf8(langMO) + "/poedit-ota.mo.gz", hdrs)
667684
.then_on_main([=](downloaded_file f)
668685
{
669686
TempOutputFileFor temp(mofile);

src/edapp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class PoeditApp : public wxApp, public MenusManager
6262
static wxString GetCacheDir(const wxString& category);
6363

6464
/// Returns Poedit version string.
65-
wxString GetAppVersion() const;
65+
wxString GetAppVersion() const; // e.g. "3.4.2"
66+
wxString GetMajorAppVersion() const; // e.g. "3.4"
6667
wxString GetAppBuildNumber() const;
6768
bool CheckForBetaUpdates() const;
6869

@@ -108,7 +109,7 @@ class PoeditApp : public wxApp, public MenusManager
108109

109110
void SetupLanguage();
110111
#ifdef SUPPORTS_OTA_UPDATES
111-
void SetupOTALanguageUpdate(wxTranslations *trans, const std::string& lang);
112+
void SetupOTALanguageUpdate(wxTranslations *trans, const wxString& lang);
112113
#endif
113114

114115
// App-global menu commands:

0 commit comments

Comments
 (0)