-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
doc-translate.sh
133 lines (110 loc) · 4.86 KB
/
doc-translate.sh
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
#!/bin/bash
# SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
#
# SPDX-License-Identifier: AGPL-3.0-only
set -euo pipefail
po4afile="support/documentation/po/po4a.conf"
build_pot_in_folder="build/documentation/pot_in"
ignore_pattern='^#*\s*\{\{%\s*livechat_label[^\}]+\s*\}\}\s*$'
# Is po4a new enough?
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
required_version='0.69'
current_version=$(po4a --version | sed -En 's,po4a version ([0-9][0-9.]+[0-9]).*,\1,p')
if version_gt "$required_version" "$current_version"; then
echo "ERROR: po4a v$required_version or higher required. Current version: $current_version"
exit 1
fi
mkdir -p $build_pot_in_folder
mkdir -p "support/documentation/i18n/"
function getLangs() {
grep -P '\[Languages\..+\]' support/documentation/config.toml | sed -E 's/^.*\.(.+)\].*$/\1/' | grep -v en
}
function generatePo4aConf() {
echo "Creating $po4afile file..."
echo "" > $po4afile
# First, list all existing langs, from the config.toml file:
echo -n "[po4a_langs] " >> $po4afile
getLangs | while read lang; do
echo -n "$lang " >> $po4afile
done
echo "" >> $po4afile
# po4a_paths:
echo "[po4a_paths] support/documentation/po/livechat.en.pot \$lang:support/documentation/po/livechat.\$lang.po" >> $po4afile
# options:
echo -n "[options] " >> $po4afile
echo -n 'opt:"--addendum-charset=UTF-8" ' >> $po4afile
echo -n 'opt:"--localized-charset=UTF-8" ' >> $po4afile
echo -n 'opt:"--master-charset=UTF-8" ' >> $po4afile
echo -n 'opt:"--master-language=en_US" ' >> $po4afile
echo -n 'opt:"--msgmerge-opt=--no-wrap" ' >> $po4afile
echo -n 'opt:"--porefs=file" ' >> $po4afile
echo -n 'opt:"--wrap-po=newlines" ' >> $po4afile
echo -n 'opt:"--package-name=peertube-plugin-livechat-documentation" ' >> $po4afile
echo "" >> $po4afile
# Markdown related options:
echo -n '[po4a_alias:markdown] text ' >> $po4afile
echo -n 'opt:"--option markdown" ' >> $po4afile
# keys from the «Front Matter» section to translate:
echo -n 'opt:"--option yfm_keys=title,description" ' >> $po4afile
# # dont wrap lines:
# echo -n 'opt:"--option neverwrap" ' >> $po4afile
# handling hugo directive (when full line). For example: {{% notice tip %}}, {{% children %}}, ...
echo -n 'opt:"--option breaks=' >> $po4afile
echo -n "'" >> $po4afile
echo -n '^\{\{%.*%\}\}$' >> $po4afile
echo -n "'" >> $po4afile
echo -n '" ' >> $po4afile
echo -n 'opt:"--addendum-charset=UTF-8" ' >> $po4afile
echo -n 'opt:"--localized-charset=UTF-8" ' >> $po4afile
echo -n 'opt:"--master-charset=UTF-8" ' >> $po4afile
# generate translation even if incomplete:
echo -n 'opt:"--keep=0" ' >> $po4afile
echo "" >> $po4afile
echo "" >> $po4afile
# We must now list all english files to translate:
find support/documentation/content/en/ -name '*.md' | sort | while read source_file; do
target_file=$(echo "$source_file" | sed -E "s/\/content\/en\//\/content\/translations\/\$lang\//")
echo -n '[type: markdown] ' >> $po4afile
echo -n $source_file >> $po4afile
echo -n " " >> $po4afile
# Some files have strings to ignore. For example, when the whole line/title is using the livechat_label shortcode.
# To do so, we will use the pot_in option (see https://po4a.org/man/man1/po4a.1.php).
# We are generating a filtered file, that will be used.
# There is also a special case:
# If the file Yaml Font Matter contains the livechatnotranslation option, we just copy the file.
# To do so, we use the pot_in option, by generating an empty file.
pot_in_file=$(echo "$source_file" | sed -E 's/support\/documentation\/content\/en\//build\/documentation\/pot_in\//')
pot_in_file_dir=$(dirname "$pot_in_file")
use_pot_in=""
if grep -q -P '^livechatnotranslation\s*:\s*true\s*$' "$source_file"; then
echo "File $source_file must not be translated."
mkdir -p "$pot_in_file_dir"
echo "" > "$pot_in_file"
echo -n "pot_in:$pot_in_file " >> $po4afile
else
if grep -q -P "$ignore_pattern" $source_file; then
echo "File $source_file contains pattern to ignore, we must create a filtered pot_in file."
mkdir -p "$pot_in_file_dir"
grep -v -P "$ignore_pattern" $source_file > "$pot_in_file";
echo -n "pot_in:$pot_in_file " >> $po4afile
fi
fi
echo -n '$lang:'$target_file >> $po4afile
echo "" >> $po4afile
done
}
function runPo4a() {
echo "Running po4a..."
po4a $po4afile
}
function copyLivechatLanguages() {
echo "Copying livechat yml languages files to the hugo directory..."
find languages/ -name '*.yml' | while read file; do
# We need to rename .yml to .yaml... don't ask, hugo stuff...
new_filename=$(echo "$file" | sed -E "s/languages\/(.*)\.yml$/support\/documentation\/i18n\/\1.yaml/")
cp $file $new_filename
done
}
generatePo4aConf
copyLivechatLanguages
runPo4a