-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrowdin-init.sh
More file actions
1869 lines (1464 loc) · 49.2 KB
/
Copy pathcrowdin-init.sh
File metadata and controls
1869 lines (1464 loc) · 49.2 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/bash
#
# crowdin-init.sh - Synchronize a phpBB extension with its Crowdin project.
#
# Purpose:
# Creates or updates the Crowdin project backing a phpBB extension's
# localization, ensures the custom phpBB language variants (formal /
# casual honorific dialects) and full phpBB target language set exist
# on Crowdin, then either uploads source strings to Crowdin or
# downloads completed translations back into the extension, using the
# Crowdin CLI driven by a runtime-generated crowdin.yml built from
# crowdin.yml.template. With -s/--seed-untranslated, also uploads
# this extension's own local translation for any language still at
# 0% on Crowdin, so translators start from real work instead of the
# bare English source.
#
# Usage:
# crowdin-init.sh [OPTIONS] [EXTENSION_PATH]
#
# Run with -h/--help for the full option list. EXTENSION_PATH is the
# phpBB extension directory (defaults to the current directory); the
# extension's composer.json supplies its identity (vendor/package,
# display name, description).
#
# Inputs:
# crowdin.conf (or the file given via -c/--config) - optional shared
# project configuration (name, identifier, description, visibility,
# language access policy, source sync toggle, CLI template path).
# composer.json in the resolved extension root - required; supplies
# defaults for project identifier/name/description.
# CROWDIN_API_TOKEN - required Crowdin Personal Access Token.
# CROWDIN_PROJECT_ID - optional numeric project ID override, used
# instead of looking the project up by identifier.
#
# Outputs:
# Colorized progress/status messages on stdout, errors on stderr.
# Source files are uploaded to Crowdin, or completed translations are
# downloaded into the extension tree, depending on the selected mode.
#
# Exit status:
# 0 on success, 1 on any validation, API, or Crowdin CLI failure.
#
# Dependencies:
# bash, curl, jq, and (for source sync / translation download) the
# Crowdin CLI (`crowdin`).
set -Eeuo pipefail
# ==============================================================================
# Constants
# ==============================================================================
CROWDIN_API_BASE="https://api.crowdin.com/api/v2"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# ==============================================================================
# Script Directory
# ==============================================================================
SCRIPT_DIR=$(
cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &&
pwd
)
# ==============================================================================
# Defaults
# ==============================================================================
DRY_RUN=false
DOWNLOAD_MODE=false
DOWNLOAD_DRY_RUN=false
SEED_UNTRANSLATED=false
CONFIG_FILE="$SCRIPT_DIR/crowdin.conf"
CROWDIN_CLI_TEMPLATE="$SCRIPT_DIR/crowdin.yml.template"
SOURCE_LANGUAGE="en"
PROJECT_NAME=""
PROJECT_IDENTIFIER=""
PROJECT_DESCRIPTION=""
PROJECT_VISIBILITY="open"
LANGUAGE_ACCESS_POLICY="open"
SOURCE_SYNC=true
EXTENSION_PATH="."
CROWDIN_API_TOKEN="${CROWDIN_API_TOKEN:-}"
CROWDIN_PROJECT_ID="${CROWDIN_PROJECT_ID:-}"
CONFIG_SET_PROJECT_NAME=false
CONFIG_SET_PROJECT_DESCRIPTION=false
CONFIG_SET_PROJECT_VISIBILITY=false
CONFIG_SET_LANGUAGE_ACCESS_POLICY=false
EXTENSION_ROOT=""
COMPOSER_FILE=""
COMPOSER_PACKAGE=""
COMPOSER_VENDOR=""
COMPOSER_IDENTIFIER=""
COMPOSER_DISPLAY_NAME=""
COMPOSER_DESCRIPTION=""
COMPOSER_TYPE=""
PROJECT_ID=""
CURRENT_PROJECT=""
HTTP_STATUS=""
API_RESPONSE_FILE=""
RUNTIME_CROWDIN_CONFIG=""
# ==============================================================================
# Custom phpBB Language Variants
# ==============================================================================
#
# Format:
#
# Crowdin ID|Name|Dialect Of|Three Letter Code
#
# These custom Crowdin languages allow phpBB variants to exist independently
# from their base languages.
#
# ==============================================================================
CUSTOM_LANGUAGES=(
'de-x-sie|German (Formal Honorifics)|de|des'
'es-x-tu|Spanish (Casual Honorifics)|es-ES|esx'
'nl-x-formal|Dutch (Formal Honorifics)|nl|nlx'
'hr-x-vi|Croatian (Formal Honorifics)|hr|hrx'
)
# ==============================================================================
# phpBB Target Languages
# ==============================================================================
#
# Format:
#
# phpBB_directory|Crowdin_language_ID|Display_name
#
# Base languages and phpBB variants are separate Crowdin targets.
#
# ==============================================================================
PHPBB_LANGUAGES=(
'en_us|en-US|American English'
'ar|ar|Arabic'
'az|az|Azerbaijani'
'pt_br|pt-BR|Brazilian Portuguese'
'bg|bg|Bulgarian'
'ca|ca|Catalan'
'hr|hr|Croatian'
'hr_x_vi|hr-x-vi|Croatian (Formal Honorifics)'
'da|da|Danish'
'nl|nl|Dutch'
'nl_x_formal|nl-x-formal|Dutch (Formal Honorifics)'
'et|et|Estonian'
'fi|fi|Finnish'
'fr|fr|French'
'gd|gd|Gaelic'
'de|de|German'
'de_x_sie|de-x-sie|German (Formal Honorifics)'
'el|el|Greek'
'he|he|Hebrew'
'it|it|Italian'
'ja|ja|Japanese'
'lt|lt|Lithuanian'
'zh_cmn_hans|zh-CN|Mandarin Chinese (Simplified Script)'
'zh_cmn_hant|zh-TW|Mandarin Chinese (Traditional Script)'
'fa|fa|Persian'
'pl|pl|Polish'
'pt|pt-PT|Portuguese (Formal)'
'ro|ro|Romanian'
'ru|ru|Russian'
'sr|sr|Serbian (Cyrillic Script)'
'sr_x_latin|sr-CS|Serbian (Latin Script)'
'sk|sk|Slovak'
'sl|sl|Slovenian'
'es|es-ES|Spanish'
'es_x_tu|es-x-tu|Spanish (Casual Honorifics)'
'tr|tr|Turkish'
'uk|uk|Ukrainian'
)
# ==============================================================================
# Language Runtime State
# ==============================================================================
declare -A PHPBB_TO_CROWDIN=()
declare -A CROWDIN_TO_PHPBB=()
declare -A LANGUAGE_DISPLAY_NAME=()
PHPBB_LANGUAGE_CODES=()
CROWDIN_TARGET_IDS=()
# ==============================================================================
# Output Helpers
# ==============================================================================
# Print a red "Error: $1" message to stderr.
error()
{
printf '%bError: %s%b\n' "$RED" "$1" "$NC" >&2
}
# Print a yellow "[!] $1" warning to stdout.
warn()
{
printf '%b[!] %s%b\n' "$YELLOW" "$1" "$NC"
}
# Print a green "[✓] $1" success message to stdout.
success()
{
printf '%b[✓] %s%b\n' "$GREEN" "$1" "$NC"
}
# Print a blue "[+] $1" in-progress action message to stdout.
action()
{
printf '%b[+] %s%b\n' "$BLUE" "$1" "$NC"
}
# Print a section banner: the title ($1) followed by an underline of
# matching length, e.g. for grouping related output under one heading.
section()
{
local title="$1"
local separator
printf '\n%s\n' "$title"
printf -v separator '%*s' "${#title}" ''
printf '%s\n' "${separator// /-}"
}
# ==============================================================================
# Usage
# ==============================================================================
usage()
{
cat <<EOF
Usage:
$(basename "$0") [OPTIONS] [EXTENSION_PATH]
Options:
-c, --config FILE Use alternate shared configuration.
-n, --dry-run Preview project changes and source upload.
--download Download completed translations from Crowdin.
--download-dry-run Preview translation download.
-s, --seed-untranslated Upload the local translation for any language
Crowdin still shows at 0% translated, so
translators start from the extension's own
existing work instead of from scratch.
-h, --help Show help.
Arguments:
EXTENSION_PATH phpBB extension directory.
Defaults to current directory.
Environment:
CROWDIN_API_TOKEN Crowdin Personal Access Token.
CROWDIN_PROJECT_ID Optional numeric project ID override.
EOF
}
# ==============================================================================
# Cleanup
# ==============================================================================
# Remove temp files created for this run, if any were created yet.
# Registered as an EXIT trap, so it always runs, including on early
# exits (e.g. --help) before the temp files exist.
cleanup()
{
[[ -n "$API_RESPONSE_FILE" &&
-f "$API_RESPONSE_FILE" ]] &&
rm -f "$API_RESPONSE_FILE"
[[ -n "$RUNTIME_CROWDIN_CONFIG" &&
-f "$RUNTIME_CROWDIN_CONFIG" ]] &&
rm -f "$RUNTIME_CROWDIN_CONFIG"
# Under `set -e`, the trap's own exit status would otherwise
# replace the script's real exit status whenever the last check
# above is false (nothing to remove yet). Force success here so
# cleanup never masks the actual exit code.
return 0
}
trap cleanup EXIT
# ==============================================================================
# Helpers
# ==============================================================================
# Strip leading and trailing whitespace from $1 and print the result.
trim()
{
local value="$1"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
printf '%s' "$value"
}
# If $1 is wrapped in a single matching pair of double or single quotes,
# strip them and print the inner value; otherwise print $1 unchanged.
strip_optional_quotes()
{
local value="$1"
if [[ ${#value} -ge 2 ]]; then
if [[ "${value:0:1}" == '"' &&
"${value: -1}" == '"' ]]; then
value="${value:1:${#value}-2}"
elif [[ "${value:0:1}" == "'" &&
"${value: -1}" == "'" ]]; then
value="${value:1:${#value}-2}"
fi
fi
printf '%s' "$value"
}
# Parse $1 as a boolean config value (case-insensitive true/yes/on/1 or
# false/no/off/0) and print the canonical 'true' or 'false'. Returns
# non-zero without printing anything if $1 is not a recognized boolean.
normalize_boolean()
{
local value
value=$(
printf '%s' "$1" |
tr '[:upper:]' '[:lower:]'
)
case "$value" in
true|yes|on|1)
printf 'true'
;;
false|no|off|0)
printf 'false'
;;
*)
return 1
;;
esac
}
# Turn a snake_case/kebab-case identifier ($1) into a human-readable,
# title-cased display name, e.g. "my-extension" -> "My Extension".
# Used as a fallback project name when composer.json has no
# extra.display-name and crowdin.conf sets no PROJECT_NAME.
humanize_identifier()
{
local identifier="$1"
printf '%s\n' "$identifier" |
tr '_-' ' ' |
awk '
{
for (i = 1; i <= NF; i++) {
$i = toupper(substr($i, 1, 1)) substr($i, 2)
}
print
}
'
}
# Return success (0) if $1 is present among the remaining arguments,
# failure (1) otherwise.
array_contains()
{
local wanted="$1"
shift
local item
for item in "$@"; do
[[ "$item" == "$wanted" ]] && return 0
done
return 1
}
# ==============================================================================
# Parse Arguments
# ==============================================================================
POSITIONAL_ARGUMENTS=()
while (($# > 0)); do
case "$1" in
-c|--config)
[[ $# -ge 2 ]] || {
error "$1 requires a file."
exit 1
}
CONFIG_FILE="$2"
shift 2
;;
-n|--dry-run)
DRY_RUN=true
shift
;;
--download)
DOWNLOAD_MODE=true
shift
;;
--download-dry-run)
DOWNLOAD_MODE=true
DOWNLOAD_DRY_RUN=true
shift
;;
-s|--seed-untranslated)
SEED_UNTRANSLATED=true
shift
;;
-h|--help)
usage
exit 0
;;
--)
# Everything after a bare '--' is treated as a positional
# argument, even if it looks like an option.
shift
while (($# > 0)); do
POSITIONAL_ARGUMENTS+=("$1")
shift
done
;;
-*)
error "Unknown option: $1"
exit 1
;;
*)
POSITIONAL_ARGUMENTS+=("$1")
shift
;;
esac
done
if ((${#POSITIONAL_ARGUMENTS[@]} > 1)); then
error "Only one extension path may be specified."
exit 1
fi
if ((${#POSITIONAL_ARGUMENTS[@]} == 1)); then
EXTENSION_PATH="${POSITIONAL_ARGUMENTS[0]}"
fi
if [[ "$DRY_RUN" == "true" &&
"$DOWNLOAD_MODE" == "true" ]]; then
error "--dry-run cannot be combined with --download."
printf '%s\n' \
"Use --download-dry-run to preview translation downloads." >&2
exit 1
fi
if [[ "$SEED_UNTRANSLATED" == "true" &&
"$DOWNLOAD_MODE" == "true" ]]; then
error "--seed-untranslated cannot be combined with --download."
exit 1
fi
# ==============================================================================
# Dependencies
# ==============================================================================
for command_name in curl jq; do
command -v "$command_name" >/dev/null 2>&1 || {
error "'$command_name' is required."
exit 1
}
done
# ==============================================================================
# Resolve Extension
# ==============================================================================
[[ -d "$EXTENSION_PATH" ]] || {
error "Extension directory not found: $EXTENSION_PATH"
exit 1
}
EXTENSION_ROOT=$(
cd -- "$EXTENSION_PATH" &&
pwd
)
if command -v git >/dev/null 2>&1 &&
git -C "$EXTENSION_ROOT" \
rev-parse --show-toplevel >/dev/null 2>&1; then
GIT_ROOT=$(
git -C "$EXTENSION_ROOT" \
rev-parse --show-toplevel
)
# If EXTENSION_PATH points into a subdirectory of a git repo whose
# top level has composer.json, use that top level as the extension
# root instead, so paths in the generated crowdin.yml stay relative
# to the repository root.
if [[ -f "$GIT_ROOT/composer.json" ]]; then
EXTENSION_ROOT="$GIT_ROOT"
fi
fi
COMPOSER_FILE="$EXTENSION_ROOT/composer.json"
# ==============================================================================
# Read Configuration
# ==============================================================================
section "Configuration"
if [[ -f "$CONFIG_FILE" ]]; then
success "Using shared configuration: $CONFIG_FILE"
# Read the config as simple KEY=value lines. Blank lines and lines
# starting with '#' are skipped; the trailing `|| [[ -n "$line" ]]`
# ensures a final line without a trailing newline is still read.
while IFS= read -r line || [[ -n "$line" ]]; do
line=$(trim "$line")
[[ -n "$line" ]] || continue
[[ "$line" != \#* ]] || continue
[[ "$line" == *=* ]] || {
error "Invalid configuration line: $line"
exit 1
}
key=$(trim "${line%%=*}")
value=$(trim "${line#*=}")
value=$(strip_optional_quotes "$value")
case "$key" in
PROJECT_NAME)
PROJECT_NAME="$value"
CONFIG_SET_PROJECT_NAME=true
;;
PROJECT_IDENTIFIER)
PROJECT_IDENTIFIER="$value"
;;
PROJECT_DESCRIPTION)
PROJECT_DESCRIPTION="$value"
CONFIG_SET_PROJECT_DESCRIPTION=true
;;
SOURCE_LANGUAGE)
SOURCE_LANGUAGE="$value"
;;
PROJECT_VISIBILITY)
PROJECT_VISIBILITY="$value"
CONFIG_SET_PROJECT_VISIBILITY=true
;;
LANGUAGE_ACCESS_POLICY)
LANGUAGE_ACCESS_POLICY="$value"
CONFIG_SET_LANGUAGE_ACCESS_POLICY=true
;;
SOURCE_SYNC)
SOURCE_SYNC=$(normalize_boolean "$value") || {
error "Invalid SOURCE_SYNC value: $value"
exit 1
}
;;
CROWDIN_CLI_TEMPLATE)
if [[ "$value" = /* ]]; then
CROWDIN_CLI_TEMPLATE="$value"
else
CROWDIN_CLI_TEMPLATE="$SCRIPT_DIR/$value"
fi
;;
*)
error "Unknown configuration option: $key"
exit 1
;;
esac
done < "$CONFIG_FILE"
else
warn "No crowdin.conf found. Using defaults."
fi
[[ "$SOURCE_LANGUAGE" == "en" ]] || {
error "phpBB source language must be 'en'."
exit 1
}
if [[ "$SOURCE_SYNC" == "true" ||
"$DOWNLOAD_MODE" == "true" ]]; then
[[ -f "$CROWDIN_CLI_TEMPLATE" ]] || {
error "Crowdin template not found: $CROWDIN_CLI_TEMPLATE"
exit 1
}
command -v crowdin >/dev/null 2>&1 || {
error "Crowdin CLI was not found."
exit 1
}
fi
printf '%-24s %s\n' "Tools directory:" "$SCRIPT_DIR"
printf '%-24s %s\n' "Extension directory:" "$EXTENSION_ROOT"
# ==============================================================================
# Composer Identity
# ==============================================================================
section "Extension Identity"
[[ -f "$COMPOSER_FILE" ]] || {
error "composer.json not found."
exit 1
}
jq -e . "$COMPOSER_FILE" >/dev/null 2>&1 || {
error "composer.json contains invalid JSON."
exit 1
}
COMPOSER_PACKAGE=$(jq -r '.name // empty' "$COMPOSER_FILE")
COMPOSER_DESCRIPTION=$(jq -r '.description // empty' "$COMPOSER_FILE")
COMPOSER_TYPE=$(jq -r '.type // empty' "$COMPOSER_FILE")
COMPOSER_DISPLAY_NAME=$(
jq -r '.extra["display-name"] // empty' "$COMPOSER_FILE"
)
[[ "$COMPOSER_PACKAGE" == */* ]] || {
error "composer.json name must use vendor/package format."
exit 1
}
COMPOSER_VENDOR="${COMPOSER_PACKAGE%%/*}"
COMPOSER_IDENTIFIER="${COMPOSER_PACKAGE#*/}"
if [[ -n "$COMPOSER_TYPE" &&
"$COMPOSER_TYPE" != "phpbb-extension" ]]; then
warn "composer.json type is '$COMPOSER_TYPE', not 'phpbb-extension'."
fi
[[ -n "$PROJECT_IDENTIFIER" ]] ||
PROJECT_IDENTIFIER="$COMPOSER_IDENTIFIER"
if [[ -z "$PROJECT_NAME" ]]; then
if [[ -n "$COMPOSER_DISPLAY_NAME" ]]; then
PROJECT_NAME="$COMPOSER_DISPLAY_NAME"
else
PROJECT_NAME=$(humanize_identifier "$PROJECT_IDENTIFIER")
fi
fi
[[ -n "$PROJECT_DESCRIPTION" ]] ||
PROJECT_DESCRIPTION="$COMPOSER_DESCRIPTION"
printf '%-24s %s\n' "Composer package:" "$COMPOSER_PACKAGE"
printf '%-24s %s\n' "Composer vendor:" "$COMPOSER_VENDOR"
printf '%-24s %s\n' "Project identifier:" "$PROJECT_IDENTIFIER"
printf '%-24s %s\n' "Project name:" "$PROJECT_NAME"
printf '%-24s %s\n' "Source language:" "$SOURCE_LANGUAGE"
# ==============================================================================
# Credentials
# ==============================================================================
[[ -n "$CROWDIN_API_TOKEN" ]] || {
error "CROWDIN_API_TOKEN is not set."
exit 1
}
# ==============================================================================
# Temporary Files
# ==============================================================================
API_RESPONSE_FILE=$(mktemp)
RUNTIME_CROWDIN_CONFIG=$(mktemp)
# ==============================================================================
# API Helper
# ==============================================================================
# Issue an authenticated Crowdin API v2 request.
#
# Arguments:
# $1 - HTTP method (GET, POST, PATCH, ...)
# $2 - API path, relative to $CROWDIN_API_BASE (e.g. "/projects")
# $3 - optional JSON request body
#
# Side effects:
# Writes the raw response body to $API_RESPONSE_FILE and the HTTP
# status code to $HTTP_STATUS. Does not itself treat a non-2xx status
# as fatal; callers check $HTTP_STATUS/report_api_error as needed.
api_request()
{
local method="$1"
local path="$2"
local body="${3:-}"
local -a args
: > "$API_RESPONSE_FILE"
args=(
--silent
--show-error
--connect-timeout 10
--max-time 30
--output "$API_RESPONSE_FILE"
--write-out '%{http_code}'
--request "$method"
--header "Authorization: Bearer $CROWDIN_API_TOKEN"
--header "Accept: application/json"
)
if [[ -n "$body" ]]; then
args+=(
--header "Content-Type: application/json"
--data "$body"
)
fi
HTTP_STATUS=$(
curl "${args[@]}" "$CROWDIN_API_BASE$path"
)
}
# Print an error message ($1) annotated with the last $HTTP_STATUS, and
# dump the last Crowdin API response body (pretty-printed if it is
# JSON) to stderr for diagnosis. Expects api_request() to have run first.
report_api_error()
{
error "$1 (HTTP $HTTP_STATUS)."
if [[ -s "$API_RESPONSE_FILE" ]]; then
printf '\nCrowdin response:\n' >&2
jq . "$API_RESPONSE_FILE" >&2 2>/dev/null ||
cat "$API_RESPONSE_FILE" >&2
fi
}
# ==============================================================================
# Custom Crowdin Languages
# ==============================================================================
# Idempotently ensure one custom Crowdin language variant exists,
# creating it if necessary by cloning plural categories and text
# direction from its base dialect.
#
# Arguments:
# $1 - custom Crowdin language ID (e.g. "de-x-sie")
# $2 - display name (e.g. "German (Formal Honorifics)")
# $3 - base language it is a dialect of (e.g. "de")
# $4 - three-letter language code required by the Crowdin API
#
# Returns non-zero (without creating anything) if the language already
# exists under a different dialectOf, if the API lookup/creation fails,
# or if --download is active and the language is missing.
ensure_custom_language()
{
local custom_id="$1"
local custom_name="$2"
local dialect_of="$3"
local three_letters="$4"
local dialect_data
local plural_categories
local text_direction
local body
api_request GET "/languages/$custom_id"
if [[ "$HTTP_STATUS" == "200" ]]; then
local existing_name
local existing_dialect
existing_name=$(
jq -r '.data.name // empty' "$API_RESPONSE_FILE"
)
existing_dialect=$(
jq -r '.data.dialectOf // empty' "$API_RESPONSE_FILE"
)
[[ "$existing_dialect" == "$dialect_of" ]] || {
error \
"Custom language '$custom_id' exists but dialectOf is '$existing_dialect', expected '$dialect_of'."
return 1
}
success "$existing_name already exists."
return 0
fi
if [[ "$HTTP_STATUS" != "404" ]]; then
report_api_error \
"Failed to check custom language '$custom_id'"
return 1
fi
if [[ "$DRY_RUN" == "true" ]]; then
action \
"Would create custom language '$custom_name' ($custom_id)."
return 0
fi
if [[ "$DOWNLOAD_MODE" == "true" ]]; then
error \
"Required custom language '$custom_id' does not exist."
error \
"Run crowdin-init.sh without --download first."
return 1
fi
# Custom languages must be created with valid plural categories and
# text direction, so borrow them from the base dialect rather than
# guessing.
api_request GET "/languages/$dialect_of"
[[ "$HTTP_STATUS" == "200" ]] || {
report_api_error \
"Unable to retrieve dialect base '$dialect_of'"
return 1
}
dialect_data=$(jq -c '.data' "$API_RESPONSE_FILE")
plural_categories=$(
jq -c '.pluralCategoryNames' <<< "$dialect_data"
)
text_direction=$(
jq -r '.textDirection // "ltr"' <<< "$dialect_data"
)
body=$(
jq -n \
--arg name "$custom_name" \
--arg code "$custom_id" \
--arg locale "$custom_id" \
--arg three_letters "$three_letters" \
--arg text_direction "$text_direction" \
--arg dialect "$dialect_of" \
--argjson plural_categories "$plural_categories" \
'
{
name: $name,
code: $code,
localeCode: $locale,
threeLettersCode: $three_letters,
textDirection: $text_direction,
pluralCategoryNames: $plural_categories,
dialectOf: $dialect
}
'
)
action \
"Creating custom language '$custom_name'..."
api_request POST "/languages" "$body"
[[ "$HTTP_STATUS" == "201" ]] || {
report_api_error \
"Failed to create custom language '$custom_name'"
return 1
}
local created_id
created_id=$(
jq -r '.data.id // empty' "$API_RESPONSE_FILE"
)
[[ "$created_id" == "$custom_id" ]] || {
error \
"Crowdin created unexpected language ID '$created_id'; expected '$custom_id'."
return 1
}
success \
"Created custom language '$custom_name' ($custom_id)."
}
section "Custom Crowdin Languages"
for record in "${CUSTOM_LANGUAGES[@]}"; do
IFS='|' read -r \
custom_id \
custom_name \
dialect_of \
three_letters \
<<< "$record"
ensure_custom_language \
"$custom_id" \
"$custom_name" \
"$dialect_of" \
"$three_letters" || exit 1
done
# ==============================================================================
# Validate Crowdin Languages
# ==============================================================================
section "phpBB Languages"
printf '%-22s %-20s %s\n' \
"phpBB directory" \
"Crowdin ID" \
"Language"
printf '%-22s %-20s %s\n' \
"----------------------" \
"--------------------" \