-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_to_atom
executable file
·224 lines (197 loc) · 6.2 KB
/
export_to_atom
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
#
# Export XLIFF files translated by Weblate into an AtoM repository
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
usage() {
echo "Exports XLIFF from the Weblate repository to AtoM."
echo ""
echo "$0"
echo " -h --help"
echo " --atom-dir=<path>"
echo " --weblate-dir=<path>"
echo " [--approved]"
echo " [--language=<language code>]"
echo " [--commit-with-message=<message>]"
echo " [--no-pull]"
echo " [--no-push]"
echo " [--skip-version-check]"
echo ""
echo "The --commit-with-message will automatically commit any XLIFF changed"
echo " using the specified message."
echo
echo "The --no-pull option skips optional git pulls."
}
# Parse command-line options
LANG_CODE="all"
APPROVED=false
APPROVED_OPTION=""
NO_PULL=false
NO_PUSH=false
SKIP_VERSION_CHECK=false
COMMIT_WITH_MESSAGE=false
ATOM_SOURCE_DIR=".tx/i18n"
while [ "$1" != "" ]; do
# Attempt to parse out value
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
# Handle when equal sign wasn't used to set option value
if [ -n "$CURRENT_OPTION" ]; then
VALUE=$PARAM
PARAM=$CURRENT_OPTION
unset CURRENT_OPTION
fi
# Process option
case $PARAM in
-h | --help)
usage
exit
;;
--atom-dir)
ATOM_DIR=$VALUE
;;
--weblate-dir)
WEBLATE_DIR=$VALUE
;;
--language)
LANG_CODE=$VALUE
;;
--commit-with-message)
COMMIT_WITH_MESSAGE=$VALUE
;;
--approved)
APPROVED=true
VALUE=true
;;
--no-pull)
NO_PULL=true
VALUE=true
;;
--no-push)
NO_PUSH=true
VALUE=true
;;
--skip-version-check)
SKIP_VERSION_CHECK=true
VALUE=true
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
# Handle null values
if [ -z "$VALUE" ]; then
CURRENT_OPTION=$PARAM
fi
shift
done
if [ "$APPROVED" = true ]; then
APPROVED_OPTION="--approved"
else
echo "*******************************************************************"
echo "WARNING: If --approved flag isn't set, even unapproved translation"
echo " units will be exported."
echo "*******************************************************************"
echo
fi
# Require specification of both the AtoM repo directory and the Weblate repo directory
if [ -z $ATOM_DIR ] || [ -z $WEBLATE_DIR ]; then
usage
exit 1
fi
# Note initial directory
INITIAL_DIRECTORY=`pwd`
# Set default commit message (if .git/hooks/prepare-commit-msg is set to prepend it)
export GIT_COMMIT_PREPEND="Imported XLIFF files from Weblate."
# Get absolute path to AtoM dir and abort if directory doesn't exist
ATOM_DIR=`realpath $ATOM_DIR`
if [ ! -d $ATOM_DIR ] ; then
echo "The AtoM directory specified doesn't exist."
exit
fi
# Get absolute path to Weblate dir and abort if directory doesn't exist
WEBLATE_DIR=`realpath $WEBLATE_DIR`
if [ ! -d $WEBLATE_DIR ] ; then
echo "The Weblate directory specified doesn't exist."
exit
fi
# Change to AtoM directory, take note of branch, and guess version
cd $ATOM_DIR
ATOM_BRANCH=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
ATOM_VERSION=`basename $ATOM_BRANCH`
echo "Currently in the '$ATOM_BRANCH' branch of the AtoM repo..."
# Take note of Weblate repo's branch
cd $WEBLATE_DIR
WEBLATE_BRANCH=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
echo "Currently in the '$WEBLATE_BRANCH' branch of the Weblate repo..."
echo
# Exit if estimated AtoM version doesn't match name of Weblate branch
if [ "$ATOM_VERSION" != "$WEBLATE_BRANCH" ] && [ "$SKIP_VERSION_CHECK" = false ]; then
echo "Estimated AtoM version (using basename of AtoM branch) is '$ATOM_VERSION' and doesn't match name of checked out Weblate branch."
exit 1
fi
if [ "$NO_PULL" = false ]; then
# Offer to update the Weblate repo
while true; do
read -p "Do you wish to 'git pull --rebase' to update the Weblate repo before exporting from it? " yn
case $yn in
[Yy]* ) git pull --rebase; echo; break;;
[Nn]* ) echo; break;;
* ) echo "Please answer yes or no.";;
esac
done
# Offer to update the AtoM repo
cd $ATOM_DIR
while true; do
read -p "Do you wish to 'git pull --rebase' to update the AtoM repo before exporting to it? " yn
case $yn in
[Yy]* ) git pull --rebase; echo; break;;
[Nn]* ) echo; break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
# Extract all translations to .tx folder before comparison to Weblate's strings.
phing i18n-push-translations
# Export XLIFF to AtoM. XLIFF will be merged into what is currently in .tx folder.
$SCRIPT_DIR/scripts/migrate --export --merge --language="$LANG_CODE" $APPROVED_OPTION $WEBLATE_DIR/i18n $ATOM_DIR/$ATOM_SOURCE_DIR
# Split .tx files into /apps/qubit/i18n and plugin i18n folders.
phing i18n-pull-translations
#######################################
# Commit and push imported XLIFF files.
# Optional parameter: commit message
#######################################
function commit_and_push () {
git add apps/qubit/i18n
git add plugins
git add data/fixtures
if [[ ${!1} ]];then
git commit -m "$1"
else
git commit;
fi
git push
}
# Offer to commit and push changes if XLIFF was imported successfully
if [ $? -eq 0 ]
then
cd $ATOM_DIR
echo
if [ "$NO_PUSH" = false ]; then
if [ "$COMMIT_WITH_MESSAGE" = false ]; then
while true; do
read -p "Do you wish to commit the changes to the AtoM repo and push them? " yn
case $yn in
[Yy]* ) commit_and_push; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
commit_and_push "$COMMIT_WITH_MESSAGE"
fi
fi
fi
# Return to inital directory
cd $INITIAL_DIRECTORY