This repository was archived by the owner on Sep 29, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcrowdin-init.sh
More file actions
executable file
·1955 lines (1533 loc) · 53.1 KB
/
Copy pathcrowdin-init.sh
File metadata and controls
executable file
·1955 lines (1533 loc) · 53.1 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 documentation project with its
# Crowdin project.
#
# Purpose:
# Creates or updates the Crowdin project backing a phpBB documentation
# project's localization (e.g. phpbbdocs-hugo), ensures phpBB's whole
# target language set (plus formal/casual honorific variants) exists
# on Crowdin - the same full set phpbb-crowdin offers phpBB extensions,
# since a docs project invites translation into any of them even
# before it has a directory for that language - then either uploads
# DocBook 4 XML source files to Crowdin or downloads completed
# translations back into the docs project, using the Crowdin CLI
# driven by a runtime-generated crowdin.yml built from
# crowdin.yml.template and fragments/. With -s/--seed-untranslated,
# also uploads this docs project'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] [DOCS_PATH]
#
# Run with -h/--help for the full option list. DOCS_PATH is the docs
# project directory (defaults to the current directory).
#
# Inputs:
# crowdin.conf (or the file given via -c/--config) - optional shared
# configuration (source language, source-sync toggle, CLI template
# path).
# docs-project.conf in the resolved docs project root - optional;
# overrides the project identifier/name/description otherwise
# derived from the docs project's git remote or directory name.
# 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 docs project, 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"
FRAGMENTS_DIR="$SCRIPT_DIR/fragments"
SOURCE_LANGUAGE="en"
PROJECT_NAME=""
PROJECT_IDENTIFIER=""
PROJECT_DESCRIPTION=""
PROJECT_VISIBILITY="open"
LANGUAGE_ACCESS_POLICY="open"
SOURCE_SYNC=true
DOCS_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
DOCS_ROOT=""
DOCS_PROJECT_FILE=""
PROJECT_ID=""
CURRENT_PROJECT=""
HTTP_STATUS=""
API_RESPONSE_FILE=""
RUNTIME_CROWDIN_CONFIG=""
# ==============================================================================
# Source Tree Fragments
# ==============================================================================
#
# Format:
#
# Fragment file|Detection path (relative to DOCS_ROOT, glob allowed)
#
# Each fragment is one files: list entry for crowdin.yml. A fragment is
# only included in the rendered config when its detection path actually
# matches something in the docs project - Crowdin CLI's own config lint
# hard-fails on a files: entry whose source glob matches zero files
# (confirmed live against a real docs project), so a source tree a
# given docs project doesn't have must never appear at all.
#
# ==============================================================================
SOURCE_TREE_FRAGMENTS=(
'proteus-book.yml.fragment|proteus_doc_en.xml'
'chapters.yml.fragment|content/en/chapters/*.xml'
'dev-docs-docbook.yml.fragment|dev-docs-docbook/en/**/*.dbk'
)
# ==============================================================================
# Custom phpBB Language Variants
# ==============================================================================
#
# Format:
#
# Crowdin ID|Name|Dialect Of|Three Letter Code
#
# Identical to phpbb-crowdin's own list (https://github.com/phpbbmodders/
# phpbb-crowdin) - a docs project invites translation into phpBB's whole
# language ecosystem, formal/casual honorific variants included, the same
# as a phpBB extension does, rather than only the languages it happens to
# have content for already.
#
# ==============================================================================
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:
#
# Docs_directory|Crowdin_language_ID|Display_name
#
# Matches phpbb-crowdin's PHPBB_LANGUAGES list, plus Czech ("cs" -
# confirmed via the real official Czech localization,
# https://github.com/R3gi/phpbb-cz - a gap in phpbb-crowdin's own list
# worth adding there too). A docs project's content/<lang>/ directory
# naming follows the same convention phpBB's own language packs use
# (confirmed for phpbbdocs-hugo's existing da/fr/de directories, which
# match this list exactly); a docs project need not have every
# directory yet for its Crowdin project to offer every target -
# translators can start on a language before a docs project has a
# directory for it, the same as phpbb-crowdin's own model.
#
# ==============================================================================
DOCS_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)'
'cs|cs|Czech'
'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 DOCS_TO_CROWDIN=()
declare -A CROWDIN_TO_DOCS=()
declare -A LANGUAGE_DISPLAY_NAME=()
DOCS_LANGUAGE_CODES=()
CROWDIN_TARGET_IDS=()
MATCHED_FRAGMENTS=()
# ==============================================================================
# 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] [DOCS_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 docs project's own
existing work instead of from scratch.
-h, --help Show help.
Arguments:
DOCS_PATH Documentation project 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-docs" -> "My Docs".
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 docs project path may be specified."
exit 1
fi
if ((${#POSITIONAL_ARGUMENTS[@]} == 1)); then
DOCS_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 Docs Project
# ==============================================================================
[[ -d "$DOCS_PATH" ]] || {
error "Docs project directory not found: $DOCS_PATH"
exit 1
}
DOCS_ROOT=$(
cd -- "$DOCS_PATH" &&
pwd
)
if command -v git >/dev/null 2>&1 &&
git -C "$DOCS_ROOT" \
rev-parse --show-toplevel >/dev/null 2>&1; then
DOCS_ROOT=$(
git -C "$DOCS_ROOT" \
rev-parse --show-toplevel
)
fi
DOCS_PROJECT_FILE="$DOCS_ROOT/docs-project.conf"
# ==============================================================================
# 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 "Docs 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
}
[[ -d "$FRAGMENTS_DIR" ]] || {
error "Fragments directory not found: $FRAGMENTS_DIR"
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' "Docs project directory:" "$DOCS_ROOT"
# ==============================================================================
# Docs Project Identity
# ==============================================================================
section "Docs Project Identity"
DEFAULT_IDENTIFIER=""
if command -v git >/dev/null 2>&1; then
REMOTE_URL=$(
git -C "$DOCS_ROOT" \
remote get-url origin 2>/dev/null || true
)
if [[ -n "$REMOTE_URL" ]]; then
DEFAULT_IDENTIFIER=$(basename -- "$REMOTE_URL")
DEFAULT_IDENTIFIER="${DEFAULT_IDENTIFIER%.git}"
fi
fi
[[ -n "$DEFAULT_IDENTIFIER" ]] ||
DEFAULT_IDENTIFIER=$(basename -- "$DOCS_ROOT")
DOCS_PROJECT_NAME=""
DOCS_PROJECT_IDENTIFIER=""
DOCS_PROJECT_DESCRIPTION=""
if [[ -f "$DOCS_PROJECT_FILE" ]]; then
success "Using docs project configuration: $DOCS_PROJECT_FILE"
while IFS= read -r line || [[ -n "$line" ]]; do
line=$(trim "$line")
[[ -n "$line" ]] || continue
[[ "$line" != \#* ]] || continue
[[ "$line" == *=* ]] || {
error "Invalid docs-project.conf line: $line"
exit 1
}
key=$(trim "${line%%=*}")
value=$(trim "${line#*=}")
value=$(strip_optional_quotes "$value")
case "$key" in
PROJECT_NAME)
DOCS_PROJECT_NAME="$value"
CONFIG_SET_PROJECT_NAME=true
;;
PROJECT_IDENTIFIER)
DOCS_PROJECT_IDENTIFIER="$value"
;;
PROJECT_DESCRIPTION)
DOCS_PROJECT_DESCRIPTION="$value"
CONFIG_SET_PROJECT_DESCRIPTION=true
;;
*)
error "Unknown docs-project.conf option: $key"
exit 1
;;
esac
done < "$DOCS_PROJECT_FILE"
else
warn "No docs-project.conf found. Deriving identity from git/directory."
fi
[[ -n "$PROJECT_IDENTIFIER" ]] ||
PROJECT_IDENTIFIER="$DOCS_PROJECT_IDENTIFIER"
[[ -n "$PROJECT_IDENTIFIER" ]] ||
PROJECT_IDENTIFIER="$DEFAULT_IDENTIFIER"
if [[ -z "$PROJECT_NAME" ]]; then
if [[ -n "$DOCS_PROJECT_NAME" ]]; then
PROJECT_NAME="$DOCS_PROJECT_NAME"
else
PROJECT_NAME=$(humanize_identifier "$PROJECT_IDENTIFIER")
fi
fi
[[ -n "$PROJECT_DESCRIPTION" ]] ||
PROJECT_DESCRIPTION="$DOCS_PROJECT_DESCRIPTION"
printf '%-24s %s\n' "Project identifier:" "$PROJECT_IDENTIFIER"
printf '%-24s %s\n' "Project name:" "$PROJECT_NAME"
printf '%-24s %s\n' "Source language:" "$SOURCE_LANGUAGE"
# ==============================================================================
# Detect Source Trees
# ==============================================================================
section "Source Trees"
printf '%-40s %s\n' "Fragment" "Status"
printf '%-40s %s\n' "----------------------------------------" "------"
for record in "${SOURCE_TREE_FRAGMENTS[@]}"; do
IFS='|' read -r \
fragment_file \
detection_glob \
<<< "$record"
# A leading '/' would make the glob absolute, which is not what we
# want here (it's always relative to DOCS_ROOT); shopt -s globstar
# is required for the dev-docs-docbook fragment's '**' pattern.
shopt -s globstar nullglob
# shellcheck disable=SC2206 # deliberate: $detection_glob is a glob
# pattern (e.g. "*.xml" or "**/*.dbk"), not a plain value to quote.
matches=("$DOCS_ROOT"/$detection_glob)
shopt -u globstar nullglob
if ((${#matches[@]} > 0)); then
printf '%-40s %s\n' "$fragment_file" "found (${#matches[@]})"
MATCHED_FRAGMENTS+=("$fragment_file")
else
printf '%-40s %s\n' "$fragment_file" "not present"
fi
done
if ((${#MATCHED_FRAGMENTS[@]} == 0)); then
error \
"None of this tool's known DocBook-XML source trees were found in '$DOCS_ROOT'."
error \
"This docs project doesn't match the convention phpbbdocs-crowdin targets - see README.md."
exit 1
fi
success "${#MATCHED_FRAGMENTS[@]} source tree(s) will be synced."
# ==============================================================================
# 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