-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n
executable file
·153 lines (127 loc) · 4.31 KB
/
i18n
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
#!/usr/bin/env bash
## Copyright (C) 2020 Ulises Jeremias Cornejo Fandos
## Licensed under GPL v3.0
##
## This script helps to add and update gettext translations to ArchRoyal modules.
##
## @script.name [OPTION] ARGUMENTS...
##
## Options:
##
## --project=PROJECT_NAME Project name for internal setup. "generic" by default
## --module=MODULE_NAME Module name
## --lang=LANGUAGE_CODE Language code. The language code is lowercased
## and the optional country code is uppercased.
## --project-dir=DIR_PATH Project path. ./src by default
##
## Usage:
##
## @script.name --module=module.name --lang=fr [Example adding a language to a module]
## @script.name --module=module.name [Example updating the pofiles in a module]
##
ROOT=$(dirname $0)
source "${ROOT}/util/opts/opts.sh" || exit
source "${ROOT}/util/logs.sh" || exit
LANGUAGE=${lang:-""}
PROJECT_NAME=${project:-"generic"}
PROJECT_DIR=${project_dir:-"./src"}
MODULE=${module:-""}
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
if [ "${MODULE}" = "" ]; then
[[ -z "$documentation" ]] && parse_documentation
echo "$documentation"
fi
DOMAIN="${MODULE/-/_}"
MODULE_PATH="${PROJECT_DIR}/${MODULE}"
SEARCH_PATH="${MODULE_PATH}/${DOMAIN//.//}"
LOCALE_PATH="${SEARCH_PATH}/locale"
POT_FILE="${LOCALE_PATH}/${DOMAIN}.pot"
die() {
log_failed "${1}"
exit 1
}
command_exists() {
type "$1" &> /dev/null;
}
# 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"
XGETTEXT="${gettext_path}/bin/xgettext"
else
die "Homebrew was found but gettext is not installed. Install gettext using 'brew install gettext'"
fi
else
MSGINIT=$(which msginit)
MSGMERGE=$(which msgmerge)
MSGFMT=$(which msgfmt)
XGETTEXT=$(which xgettext)
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 [ ! -e "${XGETTEXT}" ]; then
die "xgettext 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 "${LOCALE_PATH}" ]; then
log_warn "${LOCALE_PATH} does not exist"
describe "creating"
mkdir "${LOCALE_PATH}"
fi
if [ ! -e "${POT_FILE}" ]; then
log_warn "${POT_FILE} does not exist"
describe "creating"
touch "${POT_FILE}"
fi
# language given, create catalog
if [ -n "${LANGUAGE}" ]; then
describe "Creating language ${LANGUAGE}"
if [ -e "${LOCALE_PATH}/${LANGUAGE}/LC_MESSAGES" ]; then
die "Cannot initialize language '${LANGUAGE}', it exists already!"
fi
mkdir -p "${LOCALE_PATH}/${LANGUAGE}/LC_MESSAGES"
DOMAIN_FILE="${LOCALE_PATH}/${LANGUAGE}/LC_MESSAGES/${DOMAIN}.po"
$MSGINIT -i "${LOCALE_PATH}/${DOMAIN}.pot" -o "${DOMAIN_FILE}" -l "${LANGUAGE}"
fi
describe "Extract messages"
echo
for po in "${LOCALE_PATH}"/*/LC_MESSAGES/$DOMAIN.po; do
log_warn "Extracting messages from ${po}"
$XGETTEXT "${po}" -o "${POT_FILE}"
done
log_success "Extracted!"
describe "Update translations"
echo
for po in "${LOCALE_PATH}"/*/LC_MESSAGES/$DOMAIN.po; do
log_warn "Updating ${po}"
$MSGMERGE --no-location --no-fuzzy-matching -o "${po}" "${po}" "${POT_FILE}"
done
log_success "Updated!"
describe "Compile message catalogs"
for po in "${LOCALE_PATH}"/*/LC_MESSAGES/*.po; do
$MSGFMT -o "${po%.*}.mo" "$po"
done
log_success "Finished :D"