This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.sh
executable file
·134 lines (107 loc) · 3.37 KB
/
i18n.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
134
#!/bin/bash
#
# This script helps to add and update gettext translations to onegov modules.
#
MODULE="${1/-/_}"
LANGUAGE="${2}"
set -eu
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
DOMAIN="${MODULE}"
MODULE_PATH="${SCRIPTPATH}/src/${1}"
SEARCH_PATH="${MODULE_PATH}/${MODULE//.//}"
LOCALE_PATH="${SEARCH_PATH}/locale"
POT_FILE="${LOCALE_PATH}/${DOMAIN}.pot"
POT_CREATE="${VIRTUAL_ENV}/bin/pot-create"
if [ "${LANGUAGE}" != "" ]; then
if echo "${LANGUAGE}" | egrep -v '^[a-z]{2}(_[A-Z]{2})?$' > /dev/null; then
echo "Invalid language code, format like this: de_CH, en_GB, en, de, ..."
echo "the language code is lowercased, the optional country code is uppercased"
exit 1
fi
fi
function show_usage() {
echo "Usage: bash i18n.sh module-name [language]"
echo ""
echo "Example adding a language to a module:"
echo "bash i18n.sh onegov.town fr"
echo ""
echo "Example updating the pofiles in a module:"
echo "bash i18n.sh onegov.town"
echo ""
exit 1
}
function die() {
echo "${1}"
exit 1
}
command_exists () {
type "$1" &> /dev/null;
}
if [ "${MODULE}" = "" ]; then
show_usage
fi
# try to find the gettext tools we need - on linux they should be available,
# on osx we try to get them from homebrew
if command_exists brew; then
if brew list | grep gettext -q; then
BREW_PREFIX=$(brew --prefix)
GETTEXT_PATH=$(brew info gettext | grep "${BREW_PREFIX}" | awk '{print $1; exit}')
MSGINIT="${GETTEXT_PATH}/bin/msginit"
MSGMERGE="${GETTEXT_PATH}/bin/msgmerge"
MSGFMT="${GETTEXT_PATH}/bin/msgfmt"
else
echo "Homebrew was found but gettext is not installed"
die "Install gettext using 'brew install gettext'"
fi
else
MSGINIT=$(which msginit)
MSGMERGE=$(which msgmerge)
MSGFMT=$(which msgfmt)
fi
if [ ! -e "${MSGINIT}" ]; then
die "msginit command could not be found, be sure to install gettext"
fi
if [ ! -e "${MSGMERGE}" ]; then
die "msgmerge command could not be found, be sure to install gettext"
fi
if [ ! -e "${MSGFMT}" ]; then
die "msgfmt command could not be found, be sure to install gettext"
fi
if [ ! -d "${MODULE_PATH}" ]; then
die "${MODULE_PATH} does not exist"
fi
if [ ! -d "${SEARCH_PATH}" ]; then
die "${SEARCH_PATH} does not exist"
fi
if [ ! -e "${POT_CREATE}" ]; then
die "${POT_CREATE} does not exist"
fi
if [ ! -e "${LOCALE_PATH}" ]; then
echo "${LOCALE_PATH} does not exist, creating..."
mkdir "${LOCALE_PATH}"
fi
if [ ! -e "${POT_FILE}" ]; then
echo "${POT_FILE} does not exist, creating..."
touch "${POT_FILE}"
fi
# language given, create catalog
if [ -n "${LANGUAGE}" ]; then
echo "Creating language ${LANGUAGE}"
if [ -e "${LOCALE_PATH}/${LANGUAGE}/LC_MESSAGES" ]; then
die "Cannot initialize language '${LANGUAGE}', it exists already!"
fi
cd "${LOCALE_PATH}"
mkdir -p "${LANGUAGE}/LC_MESSAGES"
domainfile="${LANGUAGE}/LC_MESSAGES/${DOMAIN}.po"
$MSGINIT -i "${DOMAIN}.pot" -o "${domainfile}" -l "${LANGUAGE}"
fi
echo "Extract messages"
$POT_CREATE "${SEARCH_PATH}" -o "${POT_FILE}"
echo "Update translations"
for po in "${LOCALE_PATH}"/*/LC_MESSAGES/$DOMAIN.po; do
$MSGMERGE --no-location --no-fuzzy-matching -o "${po}" "${po}" "${POT_FILE}"
done
echo "Compile message catalogs"
for po in "${LOCALE_PATH}"/*/LC_MESSAGES/*.po; do
$MSGFMT -o "${po%.*}.mo" "$po"
done