Skip to content

Commit

Permalink
Merge branch 'devel/ota-translations'
Browse files Browse the repository at this point in the history
  • Loading branch information
vslavik committed Nov 6, 2023
2 parents 2e39fba + 449fe8e commit 5440714
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/build-ota-updates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: OTA translations

on:
workflow_dispatch:
schedule:
# Every day at 10:11 UTC
- cron: '11 10 * * *'

jobs:
build-ota-translations:
name: Build OTA translations
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install GNU gettext
run: sudo apt-get install gettext

- name: Install Crowdin CLI
run: npm i -g @crowdin/cli

- name: Download latest translations from Crowdin
run: |
echo 'api_token: "${{secrets.CROWDIN_PERSONAL_TOKEN}}"' >>crowdin.yaml
crowdin download
rm crowdin.yaml
- name: Build OTA updates
run: scripts/build-ota-translations.sh

- name: Upload OTA updates
run: |
VERSION=$(sed -n -e 's/.*POEDIT_VERSION.* "\([0-9]*\)\.\([0-9]*\).*".*/\1.\2/p' src/version.h)
echo "OTA version: $VERSION"
curl --fail-with-body -F '[email protected]' -H "X-Api-Key: ${{secrets.OTA_API_KEY}}" "${{secrets.OTA_UPLOAD_ENDPOINT}}?version=${VERSION}"
28 changes: 28 additions & 0 deletions scripts/build-ota-translations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
set -e

DESTDIR="ota-update-$$"
rm -rf "$DESTDIR"
mkdir -p "$DESTDIR"
function finish {
rm -rf "$DESTDIR"
}
trap finish EXIT

# compile PO files, taking care to make them reproducible, i.e. not change if the actual
# translations didn't change:
for po in locales/*.po ; do
mo="$(basename "$po" .po).mo"
mogz="$mo.gz"
printf "%-30s" "$po"

sed -e 's/^"PO-Revision-Date: .*/"PO-Revision-Date: \\n"/g' "$po" | msgfmt -c -o "$DESTDIR/$mo" -
gzip --no-name -9 "$DESTDIR/$mo"

size=$(ls -ks "$DESTDIR/$mogz" | cut -d' ' -f1)
printf "%'3d kB %s\n" "$size" "$(cd $DESTDIR && md5sum $mogz)"
done

# create a tarball of all updates:
tar -C "$DESTDIR" -cf ota-update.tar .

2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ parts:
./bootstrap
./configure \
--with-wx-prefix="$SNAPCRAFT_STAGE" \
CPPFLAGS="-I${SNAPCRAFT_STAGE}/include -I${SNAPCRAFT_PART_INSTALL}/../build/deps/json/src" \
CPPFLAGS="-DSNAPCRAFT -I${SNAPCRAFT_STAGE}/include -I${SNAPCRAFT_PART_INSTALL}/../build/deps/json/src" \
LDFLAGS="-Wl,--copy-dt-needed-entries -L${SNAPCRAFT_STAGE}/lib" \
PKG_CONFIG_PATH="${SNAPCRAFT_STAGE}"/lib/pkgconfig
make -j1
Expand Down
27 changes: 22 additions & 5 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")
if (lang == "en" || lang == "en_US")
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
7 changes: 4 additions & 3 deletions src/edapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
class WXDLLIMPEXP_FWD_BASE wxConfigBase;
class WXDLLIMPEXP_FWD_BASE wxSingleInstanceChecker;

#if defined(HAVE_HTTP_CLIENT) && (defined(__WXMSW__) || defined(__WXOSX__))
#if defined(HAVE_HTTP_CLIENT) && (defined(__WXMSW__) || defined(__WXOSX__) || defined(SNAPCRAFT))
#define SUPPORTS_OTA_UPDATES
#endif

Expand All @@ -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 5440714

Please sign in to comment.