-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
prepare-multi-lang.sh
executable file
·95 lines (83 loc) · 2.41 KB
/
prepare-multi-lang.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
#!/usr/bin/env bash
#
# Prepare the site to be used by apache with mod_negotiation.
#
# Takes a file tree that looks like this:
# _site/
# en/
# index.html
# packages/
# org.fdroid.fdroid/index.html
# ...
# fr/
# index.html
# packages/
# org.fdroid.fdroid/index.html
# ...
#
# And transforms it into this:
#
# _site/
# index.html.en -> en/index.html
# index.html.fr -> fr/index.html
# packages/
# org.fdroid.fdroid/index.html.en -> en/packages/org.fdroid.fdroid/index.html
# org.fdroid.fdroid/index.html.fr -> fr/packages/org.fdroid.fdroid/index.html
# ...
# en/
# index.html
# packages/
# org.fdroid.fdroid/index.html
# ...
# fr/
# index.html
# packages/
# org.fdroid.fdroid/index.html
# ...
#
# This is the format used by Apache's MultiViews directive, meaning we don't need to write out Type Maps for each file.
# Note that it doesn't try to i18n the following files: .htaccess, *.css, *.js.
# These are just copied from the en/ subdirectory into the webroot.
#
# Fail on errors.
set -eu -o pipefail
function assert_file {
if [[ ! -f ${1} ]]; then
echo "Expected file at ${1}, but not found."
exit 1
fi
}
#
# When in _site and creating a symlink from assets/blah.png.fr to fr/assets/blah.png, the link needs
# to point to a relative path (i.e. ln -s ../fr/assets.blah.png assets/blah.png.fr).
# This function uses `realpath --relative-to` to ensure that the relative path is used.
#
function relative_symlink {
SRC=$1
DEST=$2
DEST_DIR=`dirname ${DEST}`
SRC_RELATIVE_TO_DEST=`realpath --relative-to="${DEST_DIR}" "${SRC}"`
ln -s ${SRC_RELATIVE_TO_DEST} ${DEST}
}
LANGS="fr"
cd _site
find en/ -type f -print0 | while IFS= read -r -d '' FILE_PATH
do
DIR=${FILE_PATH#*/} # Strip off the leading "en/" from the path
DIR=`dirname ${DIR}`
FILE=`basename ${FILE_PATH}`
EXTENSION=${FILE_PATH##*.}
mkdir -p ${DIR}
if [[ ${FILE} =~ ^(\.htaccess|.*\.css|.*\.js)$ ]]; then
echo "Not generating i18n version of ${DIR}/${FILE}"
cp ${FILE_PATH} ${DIR}/${FILE}
continue;
fi
echo "Processing ${DIR}/${FILE}"
for LANG in en $LANGS; do
SRC_I18N_FILE=${LANG}/${DIR}/${FILE}
DEST_I18N_FILE=${DIR}/${FILE}.${LANG}
assert_file ${SRC_I18N_FILE}
relative_symlink ${SRC_I18N_FILE} ${DEST_I18N_FILE}
done
done