Repository navigation
118 lines (105 loc) · 4.37 KB
/
Copy pathhide-strings.yml
File metadata and controls
118 lines (105 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Example workflow for CONTENT repositories (not this tool repo).
#
# Hides structural markers from translators in Crowdin. The list of markers is
# generated by NTTT from its marker registry (markers.yml), covering both the
# legacy (--- task ---) and title-less RFM (> [!TASK]) syntaxes. Titled RFM
# alerts stay visible so translators can translate the title. This replaces the
# previous hand-written grep/awk/sed pipeline.
#
# Copy this into the content repo's .github/workflows/ directory.
name: Crowdin Hide Strings
on:
workflow_run:
workflows: ["Crowdin Upload Action"]
types:
- completed
jobs:
crowdin-hide:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install NTTT
run: |
python -m pip install --upgrade pip
pip install git+https://github.com/raspberrypilearning/nttt.git
- name: Install Crowdin CLI
run: |
curl -L https://github.com/crowdin/crowdin-cli/releases/latest/download/crowdin-cli.zip -o crowdin-cli.zip
unzip crowdin-cli.zip -d crowdin-cli
mkdir -p ~/bin
mv crowdin-cli/*/crowdin ~/bin/crowdin
cp crowdin-cli/*/crowdin-cli.jar ~/bin/crowdin-cli.jar
chmod +x ~/bin/crowdin
echo "PATH=$HOME/bin:$PATH" >> $GITHUB_ENV
- name: Set string visibility
run: |
set -euo pipefail
crowdin --version
crowdin string list --verbose --no-progress > crowdin-strings.txt
# NTTT reads the Crowdin listing and prints IDs to hide or unhide.
# Hide structural-only markers; unhide titled RFM alerts so translators
# can translate the title.
nttt --hide-strings < crowdin-strings.txt > hide_ids.txt
nttt --unhide-strings < crowdin-strings.txt > unhide_ids.txt
hide_count=$(grep -c . hide_ids.txt || true)
unhide_count=$(grep -c . unhide_ids.txt || true)
echo "Hiding $hide_count strings"
echo "Unhiding $unhide_count strings"
# Batch updates via API are much faster than one `crowdin string edit` per ID.
batch_size=100
set_hidden_state() {
local ids_file value action past_tense response_file
ids_file="$1"
value="$2"
action="$3"
past_tense="$4"
response_file="/tmp/crowdin-${action}-response.json"
if [[ ! -s "$ids_file" ]]; then
echo "No strings to $action"
return
fi
local batch=()
local changed=0
local total
total=$(grep -c . "$ids_file" || true)
patch_batch() {
local patch http_code batch_count
patch=$(printf '%s\n' "${batch[@]}" | jq -R --argjson value "$value" 'select(length > 0) | {op: "replace", path: ("/" + . + "/isHidden"), value: $value}' | jq -s '.')
http_code=$(curl -sS -o "$response_file" -w "%{http_code}" \
-X PATCH "https://api.crowdin.com/api/v2/projects/${CROWDIN_PROJECT_ID}/strings" \
-H "Authorization: Bearer ${CROWDIN_API_TOKEN}" \
-H "Content-Type: application/json" \
-d "$patch")
if [[ "$http_code" -ge 300 ]]; then
echo "Batch $action failed (HTTP $http_code):"
cat "$response_file"
exit 1
fi
batch_count=${#batch[@]}
changed=$((changed + batch_count))
echo "$past_tense $changed / $total strings"
batch=()
}
while read -r id; do
[[ -z "$id" ]] && continue
batch+=("$id")
if ((${#batch[@]} >= batch_size)); then
patch_batch
fi
done < "$ids_file"
if ((${#batch[@]} > 0)); then
patch_batch
fi
echo "Done - $past_tense $changed strings"
}
set_hidden_state hide_ids.txt true "hide" "Hid"
set_hidden_state unhide_ids.txt false "unhide" "Unhid"
env:
CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
CROWDIN_API_TOKEN: ${{ secrets.CROWDIN_API_TOKEN }}