| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
merge_po_files_preserve_format() { |
| 4 |
base_file="base.po" |
| 5 |
new_file="new.po" |
| 6 |
output_file="output.po" |
| 7 |
|
| 8 |
declare -A new_translations |
| 9 |
|
| 10 |
# Read new_file and extract msgid and msgstr |
| 11 |
msgid="" |
| 12 |
while IFS= read -r line; do |
| 13 |
if [[ $line == msgid* ]]; then |
| 14 |
msgid="$line" |
| 15 |
msgstr="" |
| 16 |
elif [[ $line == msgstr* ]]; then |
| 17 |
msgstr="$line" |
| 18 |
new_translations["$msgid"]="$msgstr" |
| 19 |
elif [[ $line == "\"\"" ]]; then |
| 20 |
msgid+="$line" |
| 21 |
fi |
| 22 |
done < "$new_file" |
| 23 |
|
| 24 |
# Open output_file and write merged content |
| 25 |
msgid="" |
| 26 |
while IFS= read -r line; do |
| 27 |
if [[ $line == msgid* ]]; then |
| 28 |
msgid="$line" |
| 29 |
msgstr="" |
| 30 |
elif [[ $line == msgstr* ]]; then |
| 31 |
msgstr="$line" |
| 32 |
if [[ -n ${new_translations["$msgid"]} ]]; then |
| 33 |
echo "$msgid" >> "$output_file" |
| 34 |
echo "${new_translations["$msgid"]}" >> "$output_file" |
| 35 |
else |
| 36 |
echo "$msgid" >> "$output_file" |
| 37 |
echo "$msgstr" >> "$output_file" |
| 38 |
fi |
| 39 |
msgid="" |
| 40 |
msgstr="" |
| 41 |
elif [[ $line == "\"\"" ]]; then |
| 42 |
msgid+="$line" |
| 43 |
else |
| 44 |
echo "$line" >> "$output_file" |
| 45 |
fi |
| 46 |
done < "$base_file" |
| 47 |
} |
| 48 |
|
| 49 |
if [[ $# -ne 3 ]]; then |
| 50 |
echo "Usage: $0 base_file new_file output_file" |
| 51 |
exit 1 |
| 52 |
fi |
| 53 |
|
| 54 |
merge_po_files_preserve_format "base.po" "new.po" "output.po" |