From f5559876dea68b2562deb6341dc7292b6255b486 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:01:59 -0500 Subject: [PATCH] fix: Un-approve Crowdin translations to allow community to vote on them (#8787) Co-authored-by: mealie-actions[bot] <247473823+mealie-actions[bot]@users.noreply.github.com> Co-authored-by: GitHub Action --- .github/workflows/locale-sync.yml | 6 +- dev/code-generation/gen_ts_locales.py | 62 ++++++++++++++++ dev/code-generation/main.py | 8 +- .../use-locales/available-locales.ts | 74 +++++++++---------- 4 files changed, 109 insertions(+), 41 deletions(-) diff --git a/.github/workflows/locale-sync.yml b/.github/workflows/locale-sync.yml index 64fc5854f..4b721b580 100644 --- a/.github/workflows/locale-sync.yml +++ b/.github/workflows/locale-sync.yml @@ -61,12 +61,14 @@ jobs: uv sync --group dev if: steps.cached-python-dependencies.outputs.cache-hit != 'true' - - name: Run locale generation + - name: Remove Crowdin approvals and run locale generation run: | cd dev/code-generation - uv run python main.py locales + uv run python main.py crowdin-approvals locales env: CROWDIN_API_KEY: ${{ secrets.CROWDIN_API_KEY }} + # Codegen imports mealie, which otherwise defaults to production and its /app data dir + PRODUCTION: "false" - name: Check for changes id: changes diff --git a/dev/code-generation/gen_ts_locales.py b/dev/code-generation/gen_ts_locales.py index 5ca1ece28..4abd366eb 100644 --- a/dev/code-generation/gen_ts_locales.py +++ b/dev/code-generation/gen_ts_locales.py @@ -2,10 +2,13 @@ import json import os import pathlib import re +import time +from concurrent.futures import ThreadPoolExecutor from pathlib import Path import dotenv import requests +import yaml from jinja2 import Template from pydantic import ConfigDict from requests import Response @@ -80,6 +83,48 @@ class CrowdinApi: data = response.json()["data"] return {p["data"]["languageId"]: p["data"]["translationProgress"] for p in data} + def _request(self, method: str, path: str, **params) -> Response: + url = f"https://api.crowdin.com/api/v2/projects/{self.project_id}{path}" + for attempt in range(5): + response = requests.request(method, url, headers=self.headers, params=params) + if response.status_code != 429 or attempt == 4: + break + time.sleep(2**attempt) + response.raise_for_status() + return response + + def _get_all(self, path: str, **params) -> list[dict]: + items: list[dict] = [] + while True: + page = self._request("GET", path, limit=500, offset=len(items), **params).json()["data"] + items.extend(p["data"] for p in page) + if len(page) < 500: + return items + + def get_json_files(self) -> list[dict]: + return [f for f in self._get_all("/files") if f["path"].endswith(".json")] + + def remove_all_approvals(self) -> int: + """ + Remove every translation approval, returning the number of strings affected. + + An approved translation is always exported and can't be outvoted, so approvals would + lock out the community; without them Crowdin exports translations based on votes. + """ + languages = self.get_project().json()["data"]["targetLanguageIds"] + string_ids = { + approval["stringId"] + for file in self.get_json_files() + for language in languages + for approval in self._get_all("/approvals", fileId=file["id"], languageId=language) + } + + # Removal by fileId only works for asset files, so remove per string. + # Crowdin allows 20 simultaneous requests per account; stay well under it. + with ThreadPoolExecutor(max_workers=8) as pool: + list(pool.map(lambda sid: self._request("DELETE", "/approvals", stringId=sid), string_ids)) + return len(string_ids) + PROJECT_DIR = Path(__file__).parent.parent.parent @@ -230,6 +275,23 @@ def generate_locales_ts_file(): f.write(rendered) # type:ignore +def remove_crowdin_approvals(): + if not API_KEY: + log.error("CROWDIN_API_KEY is not set, cannot remove Crowdin approvals") + return + + api = CrowdinApi(None) + log.info(f"removed Crowdin approvals from {api.remove_all_approvals()} strings") + + # A source file that moves gets a new file in Crowdin, and the old one keeps collecting translations + # that are never exported; flag those so they can be cleaned up + with open(BASE / "crowdin.yml") as f: + sources = [file["source"] for file in yaml.safe_load(f)["files"]] + for file in api.get_json_files(): + if not any(file["path"].endswith(source) for source in sources): + log.warning(f"Crowdin file {file['path']} (id {file['id']}) is not in crowdin.yml and is never exported") + + def main(): generate_locales_ts_file() inject_nuxt_values() diff --git a/dev/code-generation/main.py b/dev/code-generation/main.py index e37084f4c..c6c54e27f 100644 --- a/dev/code-generation/main.py +++ b/dev/code-generation/main.py @@ -17,7 +17,7 @@ def main(): parser.add_argument( "generators", nargs="*", - help="Specific generators to run (schema, types, locales, unit-systems, data-paths, routes). If none specified, all will run.", # noqa: E501 - long line + help="Specific generators to run (schema, types, locales, unit-systems, data-paths, routes, crowdin-approvals). If none specified, all except crowdin-approvals will run.", # noqa: E501 - long line ) args = parser.parse_args() @@ -29,8 +29,12 @@ def main(): "unit-systems": (gen_ts_unit_systems.main, "unit systems"), "data-paths": (gen_py_pytest_data_paths.main, "test data paths"), "routes": (gen_py_pytest_routes.main, "pytest routes"), + "crowdin-approvals": (gen_ts_locales.remove_crowdin_approvals, "Crowdin approval removal"), } + # Changes Crowdin rather than local files, so it only runs when requested + opt_in_generators = {"crowdin-approvals"} + # Determine which generators to run if args.generators: # Validate requested generators @@ -43,7 +47,7 @@ def main(): generators_to_run = [(all_generators[g][0], all_generators[g][1]) for g in args.generators] else: # Run all generators (default behavior) - generators_to_run = list(all_generators.values()) + generators_to_run = [value for key, value in all_generators.items() if key not in opt_in_generators] # Run the selected generators for func, name in generators_to_run: diff --git a/frontend/app/composables/use-locales/available-locales.ts b/frontend/app/composables/use-locales/available-locales.ts index 291cde605..91593b97b 100644 --- a/frontend/app/composables/use-locales/available-locales.ts +++ b/frontend/app/composables/use-locales/available-locales.ts @@ -3,49 +3,49 @@ export const LOCALES = [ { name: "繁體中文 (Chinese traditional)", value: "zh-TW", - progress: 98, + progress: 91, dir: "ltr", pluralFoodHandling: "never", }, { name: "简体中文 (Chinese simplified)", value: "zh-CN", - progress: 59, + progress: 92, dir: "ltr", pluralFoodHandling: "never", }, { name: "Tiếng Việt (Vietnamese)", value: "vi-VN", - progress: 2, + progress: 51, dir: "ltr", pluralFoodHandling: "never", }, { name: "Українська (Ukrainian)", value: "uk-UA", - progress: 87, + progress: 96, dir: "ltr", pluralFoodHandling: "always", }, { name: "Türkçe (Turkish)", value: "tr-TR", - progress: 49, + progress: 93, dir: "ltr", pluralFoodHandling: "never", }, { name: "Svenska (Swedish)", value: "sv-SE", - progress: 72, + progress: 96, dir: "ltr", pluralFoodHandling: "always", }, { name: "српски (Serbian)", value: "sr-SP", - progress: 94, + progress: 92, dir: "ltr", pluralFoodHandling: "always", }, @@ -59,28 +59,28 @@ export const LOCALES = [ { name: "Slovenčina (Slovak)", value: "sk-SK", - progress: 59, + progress: 93, dir: "ltr", pluralFoodHandling: "always", }, { name: "Pусский (Russian)", value: "ru-RU", - progress: 84, + progress: 96, dir: "ltr", pluralFoodHandling: "always", }, { name: "Română (Romanian)", value: "ro-RO", - progress: 68, + progress: 92, dir: "ltr", pluralFoodHandling: "always", }, { name: "Português (Portuguese)", value: "pt-PT", - progress: 56, + progress: 97, dir: "ltr", pluralFoodHandling: "always", }, @@ -101,119 +101,119 @@ export const LOCALES = [ { name: "Norsk (Norwegian)", value: "no-NO", - progress: 57, + progress: 94, dir: "ltr", pluralFoodHandling: "always", }, { name: "Nederlands (Dutch)", value: "nl-NL", - progress: 94, + progress: 95, dir: "ltr", pluralFoodHandling: "always", }, { name: "Latviešu (Latvian)", value: "lv-LV", - progress: 48, + progress: 91, dir: "ltr", pluralFoodHandling: "always", }, { name: "Lietuvių (Lithuanian)", value: "lt-LT", - progress: 36, + progress: 83, dir: "ltr", pluralFoodHandling: "always", }, { name: "한국어 (Korean)", value: "ko-KR", - progress: 48, + progress: 92, dir: "ltr", pluralFoodHandling: "never", }, { name: "日本語 (Japanese)", value: "ja-JP", - progress: 44, + progress: 89, dir: "ltr", pluralFoodHandling: "never", }, { name: "Italiano (Italian)", value: "it-IT", - progress: 80, + progress: 93, dir: "ltr", pluralFoodHandling: "always", }, { name: "Íslenska (Icelandic)", value: "is-IS", - progress: 52, + progress: 89, dir: "ltr", pluralFoodHandling: "always", }, { name: "Magyar (Hungarian)", value: "hu-HU", - progress: 61, + progress: 94, dir: "ltr", pluralFoodHandling: "always", }, { name: "Hrvatski (Croatian)", value: "hr-HR", - progress: 39, + progress: 86, dir: "ltr", pluralFoodHandling: "always", }, { name: "עברית (Hebrew)", value: "he-IL", - progress: 68, + progress: 91, dir: "rtl", pluralFoodHandling: "always", }, { name: "Galego (Galician)", value: "gl-ES", - progress: 46, + progress: 90, dir: "ltr", pluralFoodHandling: "always", }, { name: "Français (French)", value: "fr-FR", - progress: 82, + progress: 98, dir: "ltr", pluralFoodHandling: "always", }, { name: "Français canadien (Canadian French)", value: "fr-CA", - progress: 86, + progress: 91, dir: "ltr", pluralFoodHandling: "always", }, { name: "Belge (Belgian)", value: "fr-BE", - progress: 73, + progress: 90, dir: "ltr", pluralFoodHandling: "always", }, { name: "Suomi (Finnish)", value: "fi-FI", - progress: 94, + progress: 93, dir: "ltr", pluralFoodHandling: "always", }, { name: "Eesti (Estonian)", value: "et-EE", - progress: 52, + progress: 92, dir: "ltr", pluralFoodHandling: "always", }, @@ -234,63 +234,63 @@ export const LOCALES = [ { name: "British English", value: "en-GB", - progress: 36, + progress: 93, dir: "ltr", pluralFoodHandling: "without-unit", }, { name: "Ελληνικά (Greek)", value: "el-GR", - progress: 56, + progress: 98, dir: "ltr", pluralFoodHandling: "always", }, { name: "Deutsch (German)", value: "de-DE", - progress: 97, + progress: 93, dir: "ltr", pluralFoodHandling: "always", }, { name: "Dansk (Danish)", value: "da-DK", - progress: 100, + progress: 97, dir: "ltr", pluralFoodHandling: "always", }, { name: "Čeština (Czech)", value: "cs-CZ", - progress: 65, + progress: 93, dir: "ltr", pluralFoodHandling: "always", }, { name: "Català (Catalan)", value: "ca-ES", - progress: 53, + progress: 92, dir: "ltr", pluralFoodHandling: "always", }, { name: "Български (Bulgarian)", value: "bg-BG", - progress: 67, + progress: 92, dir: "ltr", pluralFoodHandling: "always", }, { name: "العربية (Arabic)", value: "ar-SA", - progress: 92, + progress: 91, dir: "rtl", pluralFoodHandling: "always", }, { name: "Afrikaans (Afrikaans)", value: "af-ZA", - progress: 31, + progress: 78, dir: "ltr", pluralFoodHandling: "always", },