forked from jkwiecien/LocoMobileExport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loco_exporter.sh
283 lines (240 loc) · 6.81 KB
/
loco_exporter.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
#localise.biz configuations
EXPORT_URL=https://localise.biz/api/export/archive/
EXPORT_FORMAT=
API_KEY=
TAG=
PLURALS=false
#default download directory
DOWNLOAD_DIR=/tmp
DOWNLOAD_FILE_NAME=tmp_localise_export
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
#execution options
OUTPUT_PATH=
FORCE_COPY=false
MAPS=()
#colors for echo
RED='\033[0;31m'
GREEN='\033[0;32m'
GRAY='\033[0;37m'
YELLOW='\033[1;33m'
PINK='\033[1;35m'
NC='\033[0m'
#CLASS VAR
total_exported=0
####FUNCTIONS####
# $0=exported langCode
# $1=target project langCode
function tryCopyLangFilesiOS {
#validate the parameters
exportedLang=$1
targetLang=$2
if [ -z $exportedLang ] || [ -z $targetLang ]; then
echo -e "${RED}Error! Unknown exported lang code or target lang code when trying to copy files"
exit 1
fi
cd $UNZIP_PATH; cd *
exportedStringsFile=
if [ $PLURALS == true ]; then
exportedStringsFile=$exportedLang".lproj/Localizable.stringsdict"
else
toConvertStringsFile=$exportedLang".lproj/Localizable.strings"
exportedStringsFile=$exportedLang".lproj/Fixed.strings"
iconv -f UTF-16BE -t UTF8 < $toConvertStringsFile > $exportedStringsFile
fi
#check if exported lang file exists and OUTPUT_PATH is set
if [ ! -z $OUTPUT_PATH ]; then
#then ensure the target lang dir exists and create it otherwise
targetLangDir=$SCRIPT_DIR/$OUTPUT_PATH"/"$targetLang".lproj"
targetLangFile=
if [ $PLURALS == true ]; then
targetLangFile=$targetLangDir"/Localizable.stringsdict"
else
targetLangFile=$targetLangDir"/Localizable.strings"
fi
if [ ! -d $targetLangDir ]; then
mkdir $targetLangDir
echo -e "No ${targetLang}.lproj folder, created a new one."
fi
cp $exportedStringsFile $targetLangFile
echo -e "${PINK}${targetLang} updated from localise.biz!\n"
fi
}
function tryCopyLangFilesAndroid {
#validate the parameters
exportedLang=$1
targetLang=$2
if [ -z $exportedLang ] || [ -z $targetLang ]; then
echo -e "${RED}Error! Unknown exported lang code or target lang code when trying to copy files"
exit 1
fi
# cd $UNZIP_PATH
cd $UNZIP_PATH
cd $(find . -type d -name "*xml-archive")
cd "res"
exportedStringsFile=$exportedLang"/strings.xml"
#check if exported lang file exists and OUTPUT_PATH is set
if [ ! -z $OUTPUT_PATH ]; then
#then ensure the target lang dir exists and create it otherwise
targetLangDir=$SCRIPT_DIR/$OUTPUT_PATH"/"$targetLang
targetLangFile=
if [ $PLURALS == true ]; then
targetLangFile=$targetLangDir"/plurals.xml"
else
targetLangFile=$targetLangDir"/strings.xml"
fi
echo -e "Lang file: $targetLangFile"
if [ ! -d $targetLangDir ]; then
mkdir $targetLangDir
echo -e "No ${targetLang} folder, created a new one."
fi
cp $exportedStringsFile $targetLangFile
echo -e "${PINK}${targetLang} updated from localise.biz!\n"
let "total_exported++"
fi
}
function cleanUp {
echo -e "${YELLOW}** Cleaning up...\n${NC}"
cd /tmp
rm -rf $UNZIP_PATH
rm -f $DOWNLOAD_FILE_NAME
}
#check platform
PLATFORM=
case $1 in
[iI][oO][sS])
PLATFORM="ios"
shift
;;
android)
PLATFORM="android"
shift
;;
*)
echo -e "${RED}Please specific which platform to export, ${GRAY}ios ${RED}or ${GRAY}android${RED}?${NC}"
exit 1
;;
esac
#check options
while getopts ":-:" opt; do
case $opt in
-)
case "${OPTARG}" in
key)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
API_KEY=$val
;;
tag)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
TAG=$val
;;
plurals)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
PLURALS=true
;;
output)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
echo -e "Output Path set to ${GRAY}${val}${NC}"
OUTPUT_PATH=$val
;;
force)
FORCE_COPY=true
echo "FORCE_COPY"
;;
map)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
MAPS+=($val)
;;
*)
echo -e "${RED}${OPTARG} is not an valid options"
exit 1
;;
esac
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "option: -$OPTARG requires an argument" >&2
;;
esac
done
#check for nessesory options
if [ -z $OUTPUT_PATH ]; then
echo -e "${RED}No output path set\n\nTerminated.${NC}"
exit 1
fi
if [ -z $API_KEY ]; then
echo -e "${RED}No API key set\n\nTerminated.${NC}"
exit 1
fi
#check for additional options
if [ ! -z $TAG ]; then
echo -e "${GREEN}TAG set: ${TAG}"
fi
#check if tmp directory exists
echo -e "${YELLOW}** Checking tmp directory...${NC}"
if [ ! -d $DOWNLOAD_DIR ]; then
echo -e "${RED}No /tmp directory available\n\nTerminated.${NC}"
else
echo -e "${GREEN}/tmp OK${NC}"
fi
#make up the export api
if [[ $PLATFORM == "ios" ]]; then
if [ $PLURALS == true ]; then
EXPORT_FORMAT="stringsdict.zip"
echo -e "${YELLOW}** Going to Export iOS PLURALS...${NC}"
else
EXPORT_FORMAT="strings.zip"
echo -e "${YELLOW}** Going to Export iOS...${NC}"
fi
elif [[ $PLATFORM == "android" ]]; then
EXPORT_FORMAT="xml.zip"
if [ $PLURALS == true ]; then
echo -e "${YELLOW}** Going to Export Android XML PLURALS...${NC}"
else
echo -e "${YELLOW}** Going to Android XML...${NC}"
fi
fi
EXPORT_URL_FINAL="${EXPORT_URL}${EXPORT_FORMAT}?key=${API_KEY}&index=id&order=id"
if [ ! -z $TAG ]; then
EXPORT_URL_FINAL="${EXPORT_URL_FINAL}&filter=${TAG}"
fi
DOWNLOAD_PATH_FINAL="${DOWNLOAD_DIR}/${DOWNLOAD_FILE_NAME}.zip"
echo -e "Export URL: ${GRAY}$EXPORT_URL_FINAL${NC}"
echo -e "Saving to: ${GRAY}$DOWNLOAD_PATH_FINAL${NC}"
#download, check, and unzip
curl "$EXPORT_URL_FINAL" "--output" "${DOWNLOAD_PATH_FINAL}"
if [ -e $DOWNLOAD_PATH_FINAL ]; then
echo -e "${GREEN}Download SUCCESS${NC}"
else
echo -e "${RED}Download FAILED\n\nTerminated.${NC}"
fi
echo -e "${YELLOW}** Unzipping export...${NC}"
UNZIP_PATH="$DOWNLOAD_DIR/$DOWNLOAD_FILE_NAME"
unzip -qo "$DOWNLOAD_PATH_FINAL" -d $UNZIP_PATH
if [ ! -d $UNZIP_PATH ]; then
echo -e "${RED}Export unzip FAILED\n\nTerminated.${NC}"
else
echo -e "${GREEN}Export Unzipped.${NC}"
fi
#check unzipped content for ios
echo -e "${YELLOW}** Inspecting exported files...\n${NC}"
cd $UNZIP_PATH
for i in "${MAPS[@]}"; do
IFS='=' read -ra mapKV <<< "$i"
if [ ! -z ${mapKV[0]} ] || [ ! -z ${mapKV[0]} ]; then
echo -e "${YELLOW}** Copying files from ${mapKV[0]} to ${mapKV[1]}...${NC}"
if [[ $PLATFORM == "ios" ]]; then
tryCopyLangFilesiOS ${mapKV[0]} ${mapKV[1]}
elif [[ $PLATFORM == "android" ]]; then
tryCopyLangFilesAndroid ${mapKV[0]} ${mapKV[1]}
fi
fi
done
#cleanUp
cleanUp
if [ $total_exported > 0 ]; then
echo -e "${GREEN}Export FINISH!! Total exported languages: ${total_exported}"
fi
exit 0