forked from linuxmint/mintstick
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-po.sh
More file actions
executable file
·428 lines (363 loc) · 13 KB
/
Copy pathupdate-po.sh
File metadata and controls
executable file
·428 lines (363 loc) · 13 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/bin/bash
# Universal translation management script
# Automatically detects project info and manages translations
# Function to display help
usage() {
echo "Usage: $(basename "$0") [options]"
echo
echo "This script manages gettext translation files (.po, .pot)."
echo
echo "Options:"
echo " --debug Show detailed lists of untranslated and fuzzy strings for each language."
echo " -h, --help Display this help message and exit."
}
# Parse command line arguments
DEBUG_MODE=false
for arg in "$@"; do
case $arg in
-h | --help)
usage
exit 0
;;
--debug)
DEBUG_MODE=true
shift # Shift arguments
;;
esac
done
SCRIPT_DIR="$(dirname "$(readlink -f "${0}")")"
cd "$SCRIPT_DIR"
# Auto-detect configuration
if [ -f "debian/changelog" ]; then
APPNAME=$(head -n1 debian/changelog | cut -d' ' -f1)
VERSION=$(head -n1 debian/changelog | sed 's/.*(\([^)]*\)).*/\1/' | sed 's/-.*//')
else
APPNAME=$(basename "$PWD")
VERSION="unknown"
fi
POTFILE="po/messages.pot"
# Configuration
BUGS_EMAIL="support@minios.dev"
COPYRIGHT_HOLDER="MiniOS Linux"
# Auto-detect languages from existing .po files
if [ -d "po" ]; then
LANGUAGES=($(find po -maxdepth 1 -name "*.po" -exec basename {} .po \; | sort))
fi
# Default languages if none found
if [ ${#LANGUAGES[@]} -eq 0 ]; then
LANGUAGES=("ru" "pt" "pt_BR" "it" "id" "fr" "es" "de")
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Helper functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
show_stats() {
if [ ! -f "$POTFILE" ]; then
log_warning "Translation template not found, skipping statistics"
return 1
fi
log_info "Translation statistics for $APPNAME:"
# Get total number of messages
local total=$(msgcat "$POTFILE" | grep -c '^msgid ' 2>/dev/null || echo "0")
if [ "$total" -eq 0 ]; then
log_warning "No translatable messages found"
return 1
fi
printf "%-8s %-12s %-12s %-8s\n" "Lang" "Translated" "Fuzzy" "Percent"
echo "----------------------------------------"
for lang in "${LANGUAGES[@]}"; do
local pofile="po/${lang}.po"
if [ -f "$pofile" ]; then
local stats=$(msgfmt --statistics -o /dev/null "$pofile" 2>&1)
local translated=$(echo "$stats" | grep -o '[0-9]\+ translated' | grep -o '[0-9]\+' || echo "0")
local fuzzy=$(echo "$stats" | grep -o '[0-9]\+ fuzzy' | grep -o '[0-9]\+' || echo "0")
local percent=0
if [ "$total" -gt 0 ]; then
percent=$((translated * 100 / total))
fi
printf "%-8s %-12s %-12s %-8s%%\n" "$lang" "$translated" "$fuzzy" "$percent"
else
printf "%-8s %-12s %-12s %-8s\n" "$lang" "missing" "-" "-"
fi
done
echo "----------------------------------------"
log_info "Total messages: $total"
# Show summary of issues
local has_issues=false
for lang in "${LANGUAGES[@]}"; do
local pofile="po/${lang}.po"
if [ -f "$pofile" ]; then
# We use msgfmt for general statistics, not msgattrib, as it is faster
local stats=$(msgfmt --statistics -o /dev/null "$pofile" 2>&1)
local untranslated=$(echo "$stats" | grep -o '[0-9]\+ untranslated' | grep -o '[0-9]\+' || echo "0")
local fuzzy=$(echo "$stats" | grep -o '[0-9]\+ fuzzy' | grep -o '[0-9]\+' || echo "0")
if [ "$untranslated" -gt 0 ] || [ "$fuzzy" -gt 0 ]; then
if [ "$has_issues" = false ]; then
echo
log_info "Translation issues found:"
has_issues=true
fi
local issues=""
[ "$untranslated" -gt 0 ] && issues="$untranslated untranslated"
[ "$fuzzy" -gt 0 ] && issues="$issues${issues:+, }$fuzzy fuzzy"
echo " $lang: $issues"
fi
fi
done
if [ "$has_issues" = true ]; then
echo
log_info "Use 'msgattrib --untranslated po/LANG.po' to see untranslated strings"
log_info "Use 'msgattrib --only-fuzzy po/LANG.po' to see fuzzy translations"
log_info "Or run this script with the --debug option for a full report."
fi
}
generate_pot() {
log_info "Generating translation template for $APPNAME..."
# Auto-detect source files by type
local python_sources=$(find . -maxdepth 3 -type f -name "*.py" -not -path "./generate_additional_files.py" 2>/dev/null)
local glade_sources=$(find . -maxdepth 3 -type f \( -name "*.glade" -o -name "*.ui" \) 2>/dev/null)
local desktop_sources=$(find . -maxdepth 3 -type f -name "*.desktop" 2>/dev/null)
local polkit_sources=$(find . -maxdepth 4 -type f -name "*.policy" 2>/dev/null)
if [ -z "$python_sources" ] && [ -z "$glade_sources" ] && [ -z "$desktop_sources" ] && [ -z "$polkit_sources" ]; then
log_error "No source files found for translation (.py, .glade, .desktop, .policy)"
return 1
fi
# Create po directory and ensure we start with a clean POT file
mkdir -p po
rm -f "$POTFILE"
local pot_created=false
local success=true
if [ -n "$python_sources" ]; then
log_info "Scanning Python sources..."
xgettext --language=Python \
--keyword=_ \
--keyword=N_ \
--add-comments=TRANSLATORS \
--from-code=UTF-8 \
--package-name="$APPNAME" \
--package-version="$VERSION" \
--msgid-bugs-address="$BUGS_EMAIL" \
--copyright-holder="$COPYRIGHT_HOLDER" \
--output="$POTFILE" \
$python_sources
if [ $? -eq 0 ]; then
log_success "Processed Python sources."
pot_created=true
else
log_error "Failed to process Python sources."
success=false
fi
fi
if [ "$success" = true ] && [ -n "$glade_sources" ]; then
log_info "Scanning Glade sources..."
local join_opt=""
if [ "$pot_created" = true ]; then
join_opt="--join-existing"
fi
xgettext --language=Glade \
$join_opt \
--add-comments=TRANSLATORS \
--from-code=UTF-8 \
--package-name="$APPNAME" \
--package-version="$VERSION" \
--msgid-bugs-address="$BUGS_EMAIL" \
--copyright-holder="$COPYRIGHT_HOLDER" \
--output="$POTFILE" \
$glade_sources
if [ $? -eq 0 ]; then
log_success "Processed Glade sources."
pot_created=true
else
log_error "Failed to process Glade sources."
success=false
fi
fi
if [ "$success" = true ] && [ -n "$desktop_sources" ]; then
log_info "Scanning .desktop sources..."
local join_opt=""
if [ "$pot_created" = true ]; then
join_opt="--join-existing"
fi
xgettext --language=Desktop \
$join_opt \
--add-comments=TRANSLATORS \
--from-code=UTF-8 \
--package-name="$APPNAME" \
--package-version="$VERSION" \
--msgid-bugs-address="$BUGS_EMAIL" \
--copyright-holder="$COPYRIGHT_HOLDER" \
--output="$POTFILE" \
$desktop_sources
if [ $? -eq 0 ]; then
log_success "Processed .desktop sources."
pot_created=true
else
log_error "Failed to process .desktop sources."
success=false
fi
fi
if [ "$success" = true ] && [ -n "$polkit_sources" ]; then
log_info "Scanning Polkit policy sources..."
local join_opt=""
if [ "$pot_created" = true ]; then
join_opt="--join-existing"
fi
# Check for .policy.template files first (untranslated versions)
local template_sources=""
local temp_dir=""
local cleanup_needed=false
for policy_file in $polkit_sources; do
local template_file="${policy_file%.policy}.policy.template"
if [ -f "$template_file" ]; then
if [ -z "$temp_dir" ]; then
temp_dir=$(mktemp -d)
cleanup_needed=true
fi
# Create temporary .policy file from template (xgettext doesn't recognize .template extension)
local temp_policy="$temp_dir/$(basename "${template_file%.template}")"
cp "$template_file" "$temp_policy"
template_sources="$template_sources $temp_policy"
fi
done
# Use template files if available, otherwise use original policy files
local sources_to_use=""
if [ -n "$template_sources" ]; then
sources_to_use="$template_sources"
log_info "Using .policy.template files for string extraction."
else
sources_to_use="$polkit_sources"
fi
xgettext \
$join_opt \
--add-comments=TRANSLATORS \
--from-code=UTF-8 \
--package-name="$APPNAME" \
--package-version="$VERSION" \
--msgid-bugs-address="$BUGS_EMAIL" \
--copyright-holder="$COPYRIGHT_HOLDER" \
--output="$POTFILE" \
$sources_to_use
if [ $? -eq 0 ]; then
log_success "Processed Polkit policy sources."
pot_created=true
else
log_error "Failed to process Polkit policy sources."
success=false
fi
# Clean up temporary files
if [ "$cleanup_needed" = true ]; then
rm -rf "$temp_dir"
fi
fi
if [ "$success" = true ] && [ -f "$POTFILE" ]; then
log_success "Translation template generated: $POTFILE"
msguniq --sort-output "$POTFILE" -o "$POTFILE"
log_info "Setting POT file charset to UTF-8..."
sed -i 's/charset=CHARSET/charset=UTF-8/' "$POTFILE"
return 0
else
log_error "Failed to generate translation template."
rm -f "$POTFILE" 2>/dev/null
return 1
fi
}
update_po_files() {
log_info "Updating .po files for languages: ${LANGUAGES[*]}"
if [ ! -f "$POTFILE" ]; then
log_error "Translation template not found: $POTFILE"
return 1
fi
local updated=0
local created=0
for lang in "${LANGUAGES[@]}"; do
local pofile="po/${lang}.po"
if [ ! -f "$pofile" ]; then
log_warning "Creating new translation file for $lang..."
if msginit --input="$POTFILE" --locale="$lang" --output-file="$pofile" --no-translator; then
log_success "Created: $pofile"
((created++))
else
log_error "Failed to create: $pofile"
fi
else
if msgmerge --update --backup=off "$pofile" "$POTFILE"; then
log_success "Updated: $pofile"
((updated++))
else
log_error "Failed to update: $pofile"
fi
fi
done
if [ $created -gt 0 ] || [ $updated -gt 0 ]; then
log_info "Summary: $created created, $updated updated"
else
log_warning "No files were created or updated"
fi
}
# New function for detailed .po file checks
run_debug_checks() {
if [ "$DEBUG_MODE" = false ]; then
return 0
fi
echo
log_info "--- Running Detailed Debug Checks ---"
for lang in "${LANGUAGES[@]}"; do
local pofile="po/${lang}.po"
if [ ! -f "$pofile" ]; then
continue
fi
local has_output=false
# Check for untranslated strings
local untranslated_output
untranslated_output=$(msgattrib --untranslated "$pofile" 2>/dev/null)
if [ -n "$untranslated_output" ]; then
echo
log_warning "--- Untranslated strings for '$lang' ---"
echo "$untranslated_output"
has_output=true
fi
# Check for fuzzy translations
local fuzzy_output
fuzzy_output=$(msgattrib --only-fuzzy "$pofile" 2>/dev/null)
if [ -n "$fuzzy_output" ]; then
echo
log_warning "--- Fuzzy strings for '$lang' ---"
echo "$fuzzy_output"
has_output=true
fi
if [ "$has_output" = false ]; then
log_success "No issues found for '$lang'."
fi
done
log_info "--- End of Detailed Debug Checks ---"
}
# Main execution
log_info "Starting translation workflow for $APPNAME (version $VERSION)..."
if [ "$DEBUG_MODE" = true ]; then # Message about debug mode
log_info "Debug mode is ON. Detailed report will be shown at the end."
fi
log_info "Languages: ${LANGUAGES[*]}"
if generate_pot; then
update_po_files
show_stats
run_debug_checks # Call new function
log_success "Translation workflow completed!"
else
log_error "Workflow failed during template generation"
exit 1
fi