-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_storyboard_strings.sh
More file actions
84 lines (69 loc) · 2.58 KB
/
update_storyboard_strings.sh
File metadata and controls
84 lines (69 loc) · 2.58 KB
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
#!/bin/sh
# update_storyboard_strings.sh
#
#
# Created by Marc Zhao on 2018/8/16.
# Copyright © 2018年 Marc Zhao. All rights reserved.
storyboardExt=".storyboard"
stringsExt=".strings"
newStringsExt=".strings.new"
oldStringsExt=".strings.old"
localeDirExt=".lproj"
oldIFS=$IFS
IFS=$'\n'
# Find storyboard file full path inside project folder
for storyboardPath in `find . -name "*$storyboardExt" -print`
do
# Get Base strings file full path
baseStringsPath=$(echo "$storyboardPath" | sed "s/$storyboardExt/$stringsExt/")
# Create base strings file if it doesn't exist
if ! [ -f $baseStringsPath ]; then
touch -r $storyboardPath $baseStringsPath
# Make base strings file older than the storyboard file
touch -A -01 $baseStringsPath
fi
# Create strings file only when storyboard file newer
if find $storyboardPath -prune -newer $baseStringsPath -print | grep -q .; then
# Get storyboard file name and folder
storyboardFile=$(basename "$storyboardPath")
storyboardDir=$(dirname "$storyboardPath")
# Get New Base strings file full path and strings file name
newBaseStringsPath=$(echo "$storyboardPath" | sed "s/$storyboardExt/$newStringsExt/")
stringsFile=$(basename "$baseStringsPath")
ibtool --export-strings-file $newBaseStringsPath $storyboardPath
# ibtool sometimes fails for unknown reasons with "Interface Builder could not open
# the document XXX because it does not exist."
# (maybe because Xcode is writing to the file at the same time?)
# In that case, abort the script.
if [[ $? -ne 0 ]] ; then
echo "Exiting due to ibtool error. Please run `killall -9 ibtoold` and try again."
exit 1
fi
# Only run iconv if $newBaseStringsPath exists to avoid overwriting existing
if [ -f $newBaseStringsPath ]; then
iconv -f UTF-16 -t UTF-8 $newBaseStringsPath > $baseStringsPath
rm $newBaseStringsPath
fi
# Get all locale strings folder
for localeStringsDir in `find $storyboardPath -name "*$localeDirExt" -print`
do
# Skip Base strings folder
if [ $localeStringsDir != $storyboardDir ]; then
localeStringsPath=$localeStringsDir/$stringsFile
# Just copy base strings file on first time
if [ ! -e $localeStringsPath ]; then
cp $baseStringsPath $localeStringsPath
else
oldLocaleStringsPath=$(echo "$localeStringsPath" | sed "s/$stringsExt/$oldStringsExt/")
cp $localeStringsPath $oldLocaleStringsPath
# Merge baseStringsPath to localeStringsPath
awk 'NR == FNR && /^\/\*/ {x=$0; getline; a[x]=$0; next} /^\/\*/ {x=$0; print; getline; $0=a[x]?a[x]:$0; printf $0"\n\n"}' $oldLocaleStringsPath $baseStringsPath > $localeStringsPath
rm $oldLocaleStringsPath
fi
fi
done
else
echo "$storyboardPath file not modified."
fi
done
IFS=$oldIFS