-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff2po.sh
executable file
·217 lines (190 loc) · 5.59 KB
/
diff2po.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
#!/bin/bash
display_help() {
cat <<EOF
Script transforms diff to PO file.
Usage: diff2po [-db|--dbcheck] [-dbro|--dbro]
[-dbu|--dbuser user] [-dbp|--dbpass pass]
[-dbn|--dbname name]
[-dbh|--dbhost host[=localhost]] [-dbpr|--dbport port[=3306]]
[-h|--help] INPUT_FILE
It may work with usual stdin flow.
If you need to check translation strings in the DB(mysql) use -db param and set access credentials.
If your DB works in read-only mode use -dbro parameter.
EOF
}
# Read arguments.
while :
do
case "$1" in
-db|--dbcheck)
DB_CHECK=1
;;
-dbro|--dbro)
DB_RO=1
;;
-dbu|--dbuser)
DB_USER="$2"
shift
;;
-dbp|--dbpass)
DB_PASS="$2"
shift
;;
-dbn|--dbname)
DB_NAME="$2"
shift
;;
-dbh|--dbhost)
DB_HOST="$2"
shift
;;
-dbpr|--dbport)
DB_PORT="$2"
shift
;;
-h|--help)
display_help
exit 1;
;;
*)
INPUT_FILE=$1
break
;;
esac
shift
done
# Check DB settings.
if [ ! -z "$DB_CHECK" ]; then
if [ -z "$DB_USER" ] || [ -z "$DB_PASS" ] || [ -z "$DB_NAME" ]; then
echo "Please enter proper DB credentials."
echo
display_help
exit 1;
fi
if [ -z "$DB_SERVER" ]; then
DB_SERVER="localhost"
fi
if [ -z "$DB_PORT" ]; then
DB_PORT="3306"
fi
fi
# Get messages from file of stdin.
if [ ! -z "$INPUT_FILE" -a -f "$INPUT_FILE" ]; then
messages=$(cat $INPUT_FILE)
else
messages=$(cat -)
fi
if [ -z "$messages" ]; then
echo "There is nothing to process."
echo
exit 1
fi
# Regex matches all quoted strings.
# Take a look at http://www.metaltoad.com/blog/regex-quoted-string-escapable-quotes
read -r -d '' regex_string <<EOF
( # Capturing group 1 for single or double quotes without slash before them
(?<![\\]) # No slash before next symbol
[\x27"] # Single or double quote symbols
) # End of group 1
( # Capturing group 2 for the string
(?: # Non-capturing group needs for
.(?! # continue matching any characters which are not followed by
(?<![\\])\1 # the string matched in the first backreference without a slash
) #
)* #
.? # Grab the last symbol before ending quote
) # End of group 2
\1 # Ending quote
EOF
read -r -d '' regex_t <<EOF
/
[[:space:]\.=] # Only space, dot or equal sign should be before function
t # Function name
\s*\(\s* # Opening parenthesis of the function
$regex_string # Search for quoted strings
/gmx # global, multi-line, extended
EOF
read -r -d '' regex_format_plural <<EOF
/
[[:space:]\.=] # Only space, dot or equal sign should be before function
[format_plural|formatPlural] # Function name
\s*\(\s* # Opening parenthesis of the function
.*?,\s* # Any characters in first parameter of the function
$regex_string # Search for quoted strings in second parameter
\s*,\s* # Comma separated parameters
$regex_string # Search for quoted strings in third parameter
/gmx # global, multi-line, extended
EOF
messages=$(
echo "${messages}" |
sed '/^[^\+].*/d'
)
# Extract messages from t() functions.
messages_t=$(
echo "${messages}" |
perl -ne 'while (/[[:space:]\.=]t\s*\(\s*((?<![\\])[\x27"])((?:.(?!(?<![\\])\1))*.?)\1/mgx) {print "$2\n"}'
)
# Extract messages from format_plural() functions.
messages_plural=$(
echo "${messages}" |
perl -ne 'while (/[[:space:]\.=](format_plural|formatPlural)\s*\(\s*.*?,\s*((?<![\\])[\x27"])((?:.(?!(?<![\\])\2))*.?)\2\s*,\s*((?<![\\])[\x27"])((?:.(?!(?<![\\])\4))*.?)\4/mgx) {print "$3\n$5\n"}'
)
# Merge 2 lists and sort them.
messages=$(
echo "$messages_t
$messages_plural" |
sort -u
)
# Check strings in the database.
if [ ! -z "$DB_CHECK" ]; then
db_connection="mysql\
--user=$DB_USER --password=$DB_PASS --database=$DB_NAME\
--host=$DB_HOST --port=$DB_PORT\
--skip-column-names --execute"
if [ -z "$DB_RO" ]; then
# Create temporary table for all strings
tableName="UntranslatedStrings${RANDOM}"
query="CREATE TEMPORARY TABLE ${tableName}(string TEXT);"
while read message
do
# Escape double quotes.
message=${message//\"/\\\"}
# Insert strings into temporary table.
query+="INSERT INTO ${tableName} VALUES (\"${message}\");";
done <<< "$messages"
# Try to find strings, which are not added to the DB.
query+="SELECT us.string s FROM ${tableName} us WHERE us.string NOT IN (SELECT source FROM locales_source);";
messages=$(${db_connection} "${query}")
else
# IF DB works in RO mode, check strings one by one.
messages_processed=""
while read message
do
# Escape double quotes.
message=${message//\"/\\\"}
query="SELECT ls.source FROM locales_source ls WHERE ls.source = \"${message}\";";
result=$(${db_connection} "${query}")
if [ -z "$result" ]; then
messages_processed+=${message}
messages_processed+=$IFS
fi
done <<< "$messages"
messages=$messages_processed
fi
fi
# Output untranslated string to PO file.
if [ ! -z "$messages" ]; then
while read message
do
if [ -z "$message" ]; then
continue
fi
# Escape double quote.
message=${message//\"/\\\"}
message=${message//\"/\\\"}
printf "msgid \"$message\"\nmsgstr \"\"\n\n"
done <<< "$messages"
else
echo "Nothing to output."
exit
fi