From 027732b09fb07b84a2378f4ac1b060f507b5050e Mon Sep 17 00:00:00 2001 From: Jonathan Wright <8390543+jonathanspw@users.noreply.github.com> Date: Sat, 22 Jul 2023 20:07:02 -0500 Subject: [PATCH] Auto fix i18n strings (#262) * Fix inaccurate comment * Automatic PR base language string fixes --- .github/workflows/publish-preview.yml | 2 +- .github/workflows/publish.yml | 16 ++++++++++++++++ find_missing_i18n_strings.py | 27 ++++++++++++--------------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/.github/workflows/publish-preview.yml b/.github/workflows/publish-preview.yml index f46df66f..7481b60e 100644 --- a/.github/workflows/publish-preview.yml +++ b/.github/workflows/publish-preview.yml @@ -37,4 +37,4 @@ jobs: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }} projectName: ${{ vars.CLOUDFLARE_PROJECT_NAME }} - directory: public + directory: public diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 8ce1ca53..7b2868d2 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -7,6 +7,7 @@ jobs: publish: runs-on: ubuntu-latest permissions: + pull-requests: write contents: read deployments: write name: Publish to Cloudflare Pages @@ -26,6 +27,21 @@ jobs: - name: Generate language files run: python3 setup-pages-for-supported-languages.py + - name: Create Pull Request for i18n string changes + uses: peter-evans/create-pull-request@v5 + with: + add-paths: i18n/en.json + title: Automagic i18n string updates + commit-message: Automagic i18n string updates + branch-suffix: short-commit-hash + delete-branch: true + body: | + Changes generated by find_missing_i18n_strings.py + + OP#217 + + Automated changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action + - name: Build run: hugo --minify diff --git a/find_missing_i18n_strings.py b/find_missing_i18n_strings.py index 6b05fa49..06659933 100644 --- a/find_missing_i18n_strings.py +++ b/find_missing_i18n_strings.py @@ -27,23 +27,20 @@ error = True print(f'TRANSLATION ERROR: {string}') missing_keys[string] = '' - en[string] = string # Add the missing key to the dictionary with an empty value + print(f"Adding '{string}'") + en[string] = string # Add the missing key to the dictionary elif string in unused_keys: unused_keys.remove(string) # If there are missing keys, dump the updated dictionary back into the json file -if error: - print(json.dumps(missing_keys, indent=3)) - with open('i18n/en.json', 'w') as f: - json.dump(en, f, indent=3) - exit(1) - -# If there are unused keys, print them -# TODO: Do something useful? -if unused_keys: - print("UNUSED KEYS:") - for key in unused_keys: - print(key) -else: - print("No unused keys found.") +if error or unused_keys: + # Remove unused keys + if unused_keys: + for key in unused_keys: + print(f"Removing '{key}'") + del en[key] + else: + print("No unused keys found.") + with open('i18n/en.json', 'w') as f: + json.dump(en, f, indent=3)