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 <action@github.com>
This commit is contained in:
4 files changed
+109
-41
No files matched your search
@@ -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()
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user