Skip to content

Commit

Permalink
Normalize OTA translation requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vslavik committed Nov 1, 2023
1 parent 0f13c8f commit 41f3a89
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 21 additions & 4 deletions src/edapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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");

Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/edapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 41f3a89

Please sign in to comment.