forked from wavelog/wavelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpo_gen.sh
103 lines (84 loc) · 3.61 KB
/
po_gen.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
#!/bin/bash
# Wavelog PO-File Generator
# COMPONENT: Main
#
# HB9HIL <[email protected]>, 2024
# This script is designed to help create consistent POT, PO, and MO files during Wavelog development.
# If you have added '__("english test strings")' within PHP files, you can run this script. It will automatically:
#
# - Scan all PHP files for gettext functions: __(), _ngettext(), _pgettext()
# - Recreate the POT file with the found strings
# - Merge the POT file into the PO files of each language
# - Compile MO files from the PO files
#
# It is important that this script is placed in the root directory of Wavelog.
########################## IMPORTANT ###########################
# #
# Do NOT include the compiled MO files in a Pull Request. #
# This can cause unsolvable merge conflicts. #
# We create the MO files directly within GitHub. #
# #
#################################################################
# Some variables. Usually you don't need to change them.
POT_FILE="assets/lang_src/messages.pot"
BUG_MAIL="[email protected]"
YEAR="$(date +"%Y")"
POT_TITLE_TEXT="WAVELOG PO FILE"
POT_COPYRIGHT_TEXT="Copyright (c) $YEAR Wavelog by DF2ET, DJ7NT, HB9HIL and LA8AJA."
POT_LICENCE_TEXT="This file is distributed under the MIT licence."
FOLDERS="application assets src system"
echo "####################################################"
echo " "
echo "Performing the PO Generator Script for the Main Code"
echo " "
# Find all PHP files and create a list in a temporary file
find $FOLDERS -name "*.php" > PHPFILESLIST
# Generate a new POT file to a temporary file
TEMP_POT_FILE=$(mktemp)
# Run the xgettext command with various options. Do not change these options to keep the POT/PO files consistent in Wavelog
xgettext -F \
-o $TEMP_POT_FILE \
--from-code=UTF-8 \
--keyword=__ \
--keyword=_ngettext:1,2 \
--keyword=_pgettext:1c,2 \
-L PHP \
--files-from=PHPFILESLIST \
--msgid-bugs-address=$BUG_MAIL
# After the xgettext command, we don't need the temporary file anymore
rm PHPFILESLIST
# Let's edit the header of the TEMP_POT_FILE file
sed -i "1s/.*/# $POT_TITLE_TEXT/" "$TEMP_POT_FILE"
sed -i "2s/.*/# $POT_COPYRIGHT_TEXT/" "$TEMP_POT_FILE"
sed -i "3s/.*/# $POT_LICENCE_TEXT/" "$TEMP_POT_FILE"
sed -i '4d' "$TEMP_POT_FILE"
sed -i '8d' "$TEMP_POT_FILE"
# Compare the new POT file with the existing one (excluding the POT Creation Date)
if ! diff -I 'POT-Creation-Date' "$TEMP_POT_FILE" "$POT_FILE" >/dev/null; then
echo "Updating POT file with new translations."
echo " "
mv "$TEMP_POT_FILE" "$POT_FILE"
else
echo "No changes detected in translations. POT file remains unchanged."
echo " "
rm "$TEMP_POT_FILE"
fi
# Extract the first three lines of the POT file to a temporary file
head -n 3 "$POT_FILE" > POT_HEADER
# Now we can merge the POT file (PO template) into each found PO file
for po in $(find $FOLDERS -name "*.po"); do
msgmerge --update -vv --backup=none --no-fuzzy-matching "$po" "$POT_FILE";
# Replace the first three lines of the PO file with the POT file header
sed -i '1,3d' "$po"
cat POT_HEADER "$po" > temp.po && mv temp.po "$po"
# Remove fuzzy markers from PO file
sed -i '/^#, fuzzy$/d' "$po"
done
# Clean up the temporary POT_HEADER file
rm POT_HEADER
# The last action is to create a MO file for each found PO file
for po in $(find $FOLDERS -name "*.po"); do
mo="${po%.po}.mo";
msgfmt -vv -o "$mo" "$po";
done
# END