-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-strings-from-xib-files.sh
92 lines (60 loc) · 3.34 KB
/
extract-strings-from-xib-files.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
#!/bin/bash
# extract-strings-from-xib-files.h
#
# Created by Lukasz Margielewski on 10/16/13.
#
# RUN THIS SCRIPT FROM THE DIRECTORY WHERE ALL YOUR XIB FILES RESIDE
# IT WILL GENERATE .string file in format expected by NSLocalizableStrings ready for translation
# and put them in XIB_CONCATENATED_FILE_PATH_TEXTS_ONLY file sorted alphabetically:
# 0. Output directories definitions:
TRANSLATION_OUTPUT_DIR="./TRANSLATION_RESOURCES_OUTPUT"
mkdir -p $TRANSLATION_OUTPUT_DIR
XIB_TRANSLATION_OUTPUT_DIR="$TRANSLATION_OUTPUT_DIR/XIB_strings"
mkdir -p $XIB_TRANSLATION_OUTPUT_DIR
XIB_TRANSLATION_RESOURCES_EXTRACTED_DIR="$XIB_TRANSLATION_OUTPUT_DIR/extracted"
mkdir -p $XIB_TRANSLATION_RESOURCES_EXTRACTED_DIR
XIB_PARTIAL_DIR="$XIB_TRANSLATION_RESOURCES_EXTRACTED_DIR/partial"
mkdir -p $XIB_PARTIAL_DIR
XIB_CONCATENATED_FILE_PATH="$XIB_TRANSLATION_RESOURCES_EXTRACTED_DIR/all.strings.txt"
XIB_CONCATENATED_FILE_PATH_TEXTS_ONLY="$XIB_TRANSLATION_RESOURCES_EXTRACTED_DIR/all-texts-only.strings.txt"
rm -fr "$XIB_CONCATENATED_FILE_PATH_TEXTS_ONLY"
#http://stackoverflow.com/questions/5743805/how-can-i-extract-all-localizable-strings-from-all-xib-files-into-one-file/7754884#7754884
# 1. Extract
find . -name \*.xib | xargs -t -I '{}' ibtool --generate-strings-file '{}'.strings.txt '{}'
#ibtool documentation: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ibtool.1.html
# 2. Joining:
find . -name \*.xib.strings.txt | xargs -t -I '{}' cat '{}' > $XIB_CONCATENATED_FILE_PATH
#3. Remove partiall files (optional, uncomment if you do not need files generated in 1st step):
#find . -name \*.xib.txt | xargs -t -I '{}' rm -f '{}'
#3.a Or move to the outtput designated directory (XIB_PARTIAL_DIR):
find . -name \*.xib.strings.txt -print0 | xargs -0 -J % mv % $XIB_PARTIAL_DIR
#4. CORE FUNCTION: transform all translation files generated by Apple's ibtool to the .strings expected by localization engine:
# NOTE 1: Does not copy comments (just the juice), and avoids duplicates (nice ;-)
# NOTE 2: I am not script expert (actually just the lame beginner), so this loop is slow and can be improved, but it works and avoids few encoding problems which were occuring when faster solutions applied:
comment_regex='/\*(.|[\r\n])*\*/'
text_regex='=[^"]+"([^"]+)";'
file_content=$(<"$XIB_CONCATENATED_FILE_PATH")
echo "$file_content" | while read line
do
if [[ $line =~ $comment_regex ]]
then
#echo "Skipping comment line: $line";
eeee=1
else
echo "Processing line: $line"
if [[ $line =~ $text_regex ]]
then
text="${BASH_REMATCH[1]}"
echo "Regex match: $text"
concatenated_content=$(<"$XIB_CONCATENATED_FILE_PATH_TEXTS_ONLY")
if [[ "$concatenated_content" == *"\"${text}\""* ]]
then
echo "${text} already present - skipping"
else
echo "\"${text}\"=\"${text}\";" >> "$XIB_CONCATENATED_FILE_PATH_TEXTS_ONLY"
fi
fi
fi
done #end inner (file lines loop)
# 5. Sorting al;habetically: comment if you want them in (random) order generated by Apple's ibtool
sort "$XIB_CONCATENATED_FILE_PATH_TEXTS_ONLY" -o "$XIB_CONCATENATED_FILE_PATH_TEXTS_ONLY"