Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-doc
Path: blob/main/tools/translate.sh
18080 views
1
#!/bin/sh
2
#
3
# Copyright (c) 2021 Danilo G. Baio <[email protected]>
4
# Copyright (c) 2021 Fernando Apesteguia <[email protected]>
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are met:
8
#
9
# 1. Redistributions of source code must retain the above copyright notice, this
10
# list of conditions and the following disclaimer.
11
#
12
# 2. Redistributions in binary form must reproduce the above copyright notice,
13
# this list of conditions and the following disclaimer in the documentation
14
# and/or other materials provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27
#########################################################
28
# This is a temporary fix for po4a-translate command #
29
# po4a: Fix YAML Front Matter / tags and trademarks #
30
# https://wiki.freebsd.org/Doc/IdeaList#Translation #
31
# $1: File to fix #
32
#########################################################
33
fixup_lists()
34
{
35
sed -i '' -E -e "s/(tags|trademarks).*'\[(.*)]'/\1: [\2]/g" "${1}"
36
}
37
38
#########################################################
39
# Fix includes. In a few cases we want to include the #
40
# master (aka English) version of the includes #
41
# $1: file to fix #
42
# $2: language #
43
#########################################################
44
fixup_includes()
45
{
46
# Replace ...shared/en/... with shared/$LANGUAGE
47
# content/en with content/$LANGUAGE in includes
48
sed -i '' -E -e "s,include::(.*)shared/en/,include::\1shared/${2}/," \
49
-e "s,\{include-path\}(contrib*),content/en/articles/contributors/\1," \
50
-e "s,include-path: content/en/,include-path: content/${2}/," \
51
-e "s,(include::.*)contrib-develinmemoriam(.*),include::{include-path}contrib-develinmemoriam\2," \
52
-e "s,(:chapters-path: |include::)content/en/books,\1content/${2}/books," \
53
"${1}"
54
}
55
56
if [ "$1" = "" ] || [ "$2" = "" ]; then
57
echo "Need to inform component and language:"
58
echo " $0 documentation es"
59
echo "A third (optional) argument can be informed to translate only a specific document:"
60
echo " $0 documentation pt-br articles/bsdl-gpl"
61
exit 1
62
fi
63
64
COMPONENT="$1"
65
LANGUAGE="$2"
66
SEARCH_RESTRICT="$3"
67
68
# po4a-translate option: -k, --keep
69
# Minimal threshold for translation percentage to keep (i.e. write)
70
# the resulting file (default: 80). I.e. by default, files have to be
71
# translated at least at 80% to get written.
72
# # KEEP_ENV=10 ./tools/translate.sh documentation pt_BR
73
KEEP="${KEEP_ENV:-80}"
74
75
if [ "$LANGUAGE" = "en" ]; then
76
echo "Language 'en' can't be translated."
77
exit 1
78
fi
79
80
if [ ! -d "$COMPONENT/content/$LANGUAGE" ]; then
81
echo "$COMPONENT/content/$LANGUAGE does not exist."
82
exit 1
83
fi
84
85
for pofile in $(find "$COMPONENT/content/$LANGUAGE/$SEARCH_RESTRICT" -name "*.po"); do
86
name=$(basename -s .po "$pofile")
87
if [ "$name" = "chapters-order" ]; then
88
continue
89
fi
90
91
dirbase=$(dirname "$pofile")
92
adoc_lang="$dirbase/$name.adoc"
93
adoc_orig=$(echo "$adoc_lang" | sed s,$COMPONENT/content/$LANGUAGE,$COMPONENT/content/en,)
94
95
echo "....."
96
echo "$pofile"
97
98
po4a-updatepo \
99
--format asciidoc \
100
--option compat=asciidoctor \
101
--option tablecells=1 \
102
--option yfm_keys=title,part,description \
103
--master "$adoc_orig" \
104
--master-charset "UTF-8" \
105
--copyright-holder "The FreeBSD Project" \
106
--package-name "FreeBSD Documentation" \
107
--po "$pofile"
108
if [ -f "${pofile}~" ]; then
109
rm -f "${pofile}~"
110
fi
111
112
po4a-translate \
113
--format asciidoc \
114
--option compat=asciidoctor \
115
--option tablecells=1 \
116
--option yfm_keys=title,part,description \
117
--master "$adoc_orig" \
118
--master-charset "UTF-8" \
119
--po "$pofile" \
120
--localized "$adoc_lang" \
121
--localized-charset "UTF-8" \
122
--keep "$KEEP"
123
124
fixup_lists "${adoc_lang}"
125
fixup_includes "${adoc_lang}" "${LANGUAGE}"
126
done
127
128
129