Skip to content

Commit

Permalink
Add github action to generate pot file and upload to Crowdin
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelDCurran committed Dec 23, 2023
1 parent 68c24da commit 4879213
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/crowdin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: crowdin upload
on: push [crowdinUpload2]

jobs:
Crowdin upload
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.sha }}
- uses: actions/setup-python@v4
with:
python-version: 3.11
architecture: x86
- name: generate pot file
run: |
xgettext -o nvda.pot --package-name "nvda" --package-version ${{ github.sha }} --foreign-user --add-comments=Translators:" --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --from-code utf-8 --language=python source/*.pyw source/**.py
- name: Upload to Crowdin
env:
crowdinProjectID: ${{ secrets.crowdinProjectID }}
crowdinAuthToken: ${{ secrets.crowdinAuthToken }}
run: |
py crowdinSync.py uploadSourceFile 2 nvda.pot nvda.pot
55 changes: 55 additions & 0 deletions crowdinSync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# based on file from https://github.com/jcsteh/osara

import time
import os
import glob
import sys
import zipfile
import shutil
import subprocess
import requests

AUTH_TOKEN = os.getenv("crowdinAuthToken")
if not AUTH_TOKEN:
raise ValueError("crowdinAuthToken environment variable not set")
PROJECT_ID = int(os.getenv("crowdinProjectID"))
if not PROJECT_ID:
raise ValueError("crowdinProjectID environment variable not set")


def request(path, method=requests.get, headers={}, **kwargs):
headers["Authorization"] = "Bearer %s" % AUTH_TOKEN
r = method("https://api.crowdin.com/api/v2/%s" % path, headers=headers,
**kwargs)
# Convert errors to exceptions, but print the response before raising.
try:
r.raise_for_status()
except requests.exceptions.HTTPError:
print(r.json())
raise
return r

def projectRequest(path, **kwargs):
return request("projects/%d/%s" % (PROJECT_ID, path), **kwargs)

def uploadSourceFile(crowdinFileID, crowdinFileName, localFilePath):
fn = os.path.basename(localFilePath)
f = open(localFilePath, "rb")
print(f"Uploading {localFilePath} to Crowdin temporary storage as {crowdinFileName}")
r = request("storages", method=requests.post,
headers={"Crowdin-API-FileName": crowdinFileName}, data=f)
storageID = r.json()["data"]["id"]
print(f"Updating file {crowdinFileID} on Crowdin with storage ID {storageID}")
r = projectRequest("files/%d" % crowdinFileID, method=requests.put,
json={"storageId": storageID})
return r

if __name__ == "__main__":
command = sys.argv[1]
if command == "uploadSourceFile":
crowdinFileID = int(sys.argv[2])
crowdinFileName = sys.argv[3]
localFilePath = sys.argv[4]
uploadSourceFile(crowdinFileID, crowdinFileName, localFilePath)
else:
raise ValueError("Unknown command")

0 comments on commit 4879213

Please sign in to comment.