From 37728b23d0dbd8cd8c99de4af3d693282982b245 Mon Sep 17 00:00:00 2001 From: miro Date: Wed, 15 May 2024 03:31:08 +0100 Subject: [PATCH] automations/translations --- .github/workflows/publish_alpha.yml | 72 +++++++++++++++++++++++++++++ .github/workflows/sync_tx.yml | 32 +++++++++++++ test/prepare_translations.py | 53 +++++++++++++++++++++ test/sync_translations.py | 56 ++++++++++++++++++++++ translations/en-us/dialogs.json | 45 ++++++++++++++++++ translations/en-us/intents.json | 25 ++++++++++ 6 files changed, 283 insertions(+) create mode 100644 .github/workflows/publish_alpha.yml create mode 100644 .github/workflows/sync_tx.yml create mode 100644 test/prepare_translations.py create mode 100644 test/sync_translations.py create mode 100644 translations/en-us/dialogs.json create mode 100644 translations/en-us/intents.json diff --git a/.github/workflows/publish_alpha.yml b/.github/workflows/publish_alpha.yml new file mode 100644 index 0000000..b43f906 --- /dev/null +++ b/.github/workflows/publish_alpha.yml @@ -0,0 +1,72 @@ +# This workflow will generate a distribution and upload it to PyPI + +name: Publish Alpha Build ...aX +on: + push: + branches: + - dev + paths-ignore: + - 'test/**' + - 'examples/**' + - '.github/**' + - '.gitignore' + - 'LICENSE' + - 'CHANGELOG.md' + - 'MANIFEST.in' + - 'readme.md' + - 'scripts/**' + - 'translations/**' + workflow_dispatch: + +jobs: + build_and_publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: dev + fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. + - name: Setup Python + uses: actions/setup-python@v1 + with: + python-version: 3.8 + - name: Install Build Tools + run: | + python -m pip install build wheel + - name: Increment Version + run: | + VER=$(python setup.py --version) + python scripts/bump_alpha.py + - name: "Generate release changelog" + uses: heinrichreimer/github-changelog-generator-action@v2.3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + id: changelog + - name: Commit to dev + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Prepare alpha version package + branch: dev + - name: version + run: echo "::set-output name=version::$(python setup.py --version)" + id: version + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token + with: + tag_name: V${{ steps.version.outputs.version }} + release_name: Release ${{ steps.version.outputs.version }} + body: | + Changes in this Release + ${{ steps.changelog.outputs.changelog }} + draft: false + prerelease: true + - name: Build Distribution Packages + run: | + python setup.py bdist_wheel + - name: Publish to Test PyPI + uses: pypa/gh-action-pypi-publish@master + with: + password: ${{secrets.PYPI_TOKEN}} diff --git a/.github/workflows/sync_tx.yml b/.github/workflows/sync_tx.yml new file mode 100644 index 0000000..2fd378e --- /dev/null +++ b/.github/workflows/sync_tx.yml @@ -0,0 +1,32 @@ +name: Run script on merge to dev by gitlocalize-app + +on: + workflow_dispatch: + push: + branches: + - dev + +jobs: + run-script: + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v2 + with: + ref: dev + fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. + - name: Setup Python + uses: actions/setup-python@v1 + with: + python-version: 3.9 + + - name: Run script if merged by gitlocalize-app[bot] + if: github.event_name == 'push' && github.event.head_commit.author.username == 'gitlocalize-app[bot]' + run: | + python scripts/sync_translations.py + + - name: Commit to dev + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Update translations + branch: dev diff --git a/test/prepare_translations.py b/test/prepare_translations.py new file mode 100644 index 0000000..d3f5478 --- /dev/null +++ b/test/prepare_translations.py @@ -0,0 +1,53 @@ +"""this script should run every time the contents of the locale folder change +except if PR originated from @gitlocalize-app +TODO - on commit to dev +""" + +import json +from os.path import dirname +import os + +locale = f"{dirname(dirname(__file__))}/skill_randomness/locale" +tx = f"{dirname(dirname(__file__))}/translations" + + +for lang in os.listdir(locale): + intents = {} + dialogs = {} + vocs = {} + regexes = {} + for root, _, files in os.walk(f"{locale}/{lang}"): + b = root.split(f"/{lang}")[-1] + + for f in files: + if b: + fid = f"{b}/{f}" + else: + fid = f + with open(f"{root}/{f}") as fi: + strings = [l.replace("{{", "{").replace("}}", "}") + for l in fi.read().split("\n") if l.strip() + and not l.startswith("#")] + + if fid.endswith(".intent"): + intents[fid] = strings + elif fid.endswith(".dialog"): + dialogs[fid] = strings + elif fid.endswith(".voc"): + vocs[fid] = strings + elif fid.endswith(".rx"): + regexes[fid] = strings + + os.makedirs(f"{tx}/{lang}", exist_ok=True) + if intents: + with open(f"{tx}/{lang}/intents.json", "w") as f: + json.dump(intents, f, indent=4) + if dialogs: + with open(f"{tx}/{lang}/dialogs.json", "w") as f: + json.dump(dialogs, f, indent=4) + if vocs: + with open(f"{tx}/{lang}/vocabs.json", "w") as f: + json.dump(vocs, f, indent=4) + if regexes: + with open(f"{tx}/{lang}/regexes.json", "w") as f: + json.dump(regexes, f, indent=4) diff --git a/test/sync_translations.py b/test/sync_translations.py new file mode 100644 index 0000000..29ccc92 --- /dev/null +++ b/test/sync_translations.py @@ -0,0 +1,56 @@ +"""this script should run in every PR originated from @gitlocalize-app +TODO - before PR merge +""" + +import json +from os.path import dirname +import os + +locale = f"{dirname(dirname(__file__))}/skill_randomness/locale" +tx = f"{dirname(dirname(__file__))}/translations" + + +for lang in os.listdir(tx): + intents = f"{tx}/{lang}/intents.json" + dialogs = f"{tx}/{lang}/dialogs.json" + vocs = f"{tx}/{lang}/vocabs.json" + regexes = f"{tx}/{lang}/regexes.json" + + if os.path.isfile(intents): + with open(intents) as f: + data = json.load(f) + for fid, samples in data.items(): + if samples: + samples = [s for s in samples if s] # s may be None + with open(f"{locale}/{lang}/{fid}", "w") as f: + f.write("\n".join(sorted(samples))) + + if os.path.isfile(dialogs): + with open(dialogs) as f: + data = json.load(f) + for fid, samples in data.items(): + if samples: + samples = [s for s in samples if s] # s may be None + with open(f"{locale}/{lang}/{fid}", "w") as f: + f.write("\n".join(sorted(samples))) + + if os.path.isfile(vocs): + with open(vocs) as f: + data = json.load(f) + for fid, samples in data.items(): + if samples: + samples = [s for s in samples if s] # s may be None + with open(f"{locale}/{lang}/{fid}", "w") as f: + f.write("\n".join(sorted(samples))) + + if os.path.isfile(regexes): + with open(regexes) as f: + data = json.load(f) + for fid, samples in data.items(): + if samples: + samples = [s for s in samples if s] # s may be None + if not fid.startswith("/"): + fid = "/" + fid + with open(f"{locale}/{lang}{fid}", "w") as f: + f.write("\n".join(sorted(samples))) + diff --git a/translations/en-us/dialogs.json b/translations/en-us/dialogs.json new file mode 100644 index 0000000..15b87d4 --- /dev/null +++ b/translations/en-us/dialogs.json @@ -0,0 +1,45 @@ +{ + "/dialog/first-choice.dialog": [ + "What's your first option?", + "Ok, what's the first choice?" + ], + "/dialog/coin-result.dialog": [ + "It landed on {result}", + "{result}" + ], + "/dialog/fortune-result.dialog": [ + "{answer}" + ], + "/dialog/die-result.dialog": [ + "You rolled a {result}", + "{result}" + ], + "/dialog/over-dice-limit.dialog": [ + "You have a limit of {number} dice set in your settings. I will roll {number} of dice instead." + ], + "/dialog/fortune-query.dialog": [ + "What is it you wish to know? I will answer yes or no.", + "I know all things...if they are yes or no questions. What is yours?" + ], + "/dialog/second-choice.dialog": [ + "What's your second option?", + "Ok, now what's the second choice?" + ], + "/dialog/multiple-die-result.dialog": [ + "Your dice are {result_string} with a total of {result_total}" + ], + "/dialog/number-result.dialog": [ + "{number}", + "Okay, {number}", + "I picked {number}", + "I chose {number}" + ], + "/dialog/choice-result.dialog": [ + "Tough choice, but let's go with {choice}!", + "Definitely {choice}." + ], + "/dialog/number-range-not-specified.dialog": [ + "I didn't quite understand your numbers, so I'll pick between 1 and 10.", + "I'm not sure what you said. Let's choose between 1 and 10." + ] +} \ No newline at end of file diff --git a/translations/en-us/intents.json b/translations/en-us/intents.json new file mode 100644 index 0000000..5207cff --- /dev/null +++ b/translations/en-us/intents.json @@ -0,0 +1,25 @@ +{ + "/intents/fortune-teller.intent": [ + "Tell me (the |a |my |)(future|fortune)" + ], + "/intents/roll-single-die.intent": [ + "roll a {faces} sided die", + "roll a {faces} sided dice", + "roll a dice", + "roll (a| ) die" + ], + "/intents/make-a-choice.intent": [ + "help me (make a choice|decide something)" + ], + "/intents/pick-a-number.intent": [ + "(pick|select|choose) a number between {lower} and {upper}" + ], + "/intents/roll-multiple-dice.intent": [ + "roll {number} d {faces}", + "roll {number} {faces} sided (dice|die)", + "roll {number} (dice|die)" + ], + "/intents/flip-a-coin.intent": [ + "flip a coin" + ] +} \ No newline at end of file