mirror of
https://github.com/tildearrow/furnace.git
synced 2024-11-01 02:22:39 +00:00
Merge PO files conveniently via scripts (#2070)
* Add files via upload This script can merge the post-translation order wrong PO files in the correct order! * Update and rename merge.py to MERGE.py Changed the name to all caps to make it prominent * Update MERGE.py * Update and rename MERGE.py to Reorganize-po-sequence.py rename * Reorganize-po-sequence Usage: Reorganize-po-sequence.sh base_file new_file output_file base.po is the PO file within the repo with the correct order new.po is your translated PO file but may not be in the correct order
This commit is contained in:
parent
2dac603f17
commit
272fcfccca
2 changed files with 96 additions and 0 deletions
42
po/Reorganize-po-sequence.py
Normal file
42
po/Reorganize-po-sequence.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
def merge_po_files_preserve_format(base_file, new_file, output_file):
|
||||
with open(base_file, 'r', encoding='utf-8') as f1, open(new_file, 'r', encoding='utf-8') as f2:
|
||||
base_lines = f1.readlines()
|
||||
new_lines = f2.readlines()
|
||||
|
||||
# Traverse the new_lines and extract msgid and msgstr
|
||||
new_translations = {}
|
||||
i = 0
|
||||
while i < len(new_lines):
|
||||
if new_lines[i].startswith('msgid '):
|
||||
msgid_start = i
|
||||
while not new_lines[i].startswith('msgstr '):
|
||||
i += 1
|
||||
msgid = ''.join(new_lines[msgid_start:i])
|
||||
msgstr = new_lines[i].split('msgstr ')[1].strip()
|
||||
new_translations[msgid] = msgstr
|
||||
i += 1
|
||||
|
||||
# Open the output_file and write the merged content
|
||||
with open(output_file, 'w', encoding='utf-8') as output:
|
||||
i = 0
|
||||
while i < len(base_lines):
|
||||
if base_lines[i].startswith('msgid '):
|
||||
msgid_start = i
|
||||
while not base_lines[i].startswith('msgstr '):
|
||||
i += 1
|
||||
msgid = ''.join(base_lines[msgid_start:i])
|
||||
if msgid in new_translations:
|
||||
output.write(f'{msgid}')
|
||||
output.write(f'msgstr {new_translations[msgid]}\n')
|
||||
else:
|
||||
output.write(base_lines[i])
|
||||
i += 1
|
||||
else:
|
||||
output.write(base_lines[i])
|
||||
i += 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
base_file = 'base.po'
|
||||
new_file = 'new.po'
|
||||
output_file = 'output.po'
|
||||
merge_po_files_preserve_format(base_file, new_file, output_file)
|
54
scripts/Reorganize-po-sequence.sh
Normal file
54
scripts/Reorganize-po-sequence.sh
Normal file
|
@ -0,0 +1,54 @@
|
|||
#!/bin/bash
|
||||
|
||||
merge_po_files_preserve_format() {
|
||||
base_file="base.po"
|
||||
new_file="new.po"
|
||||
output_file="output.po"
|
||||
|
||||
declare -A new_translations
|
||||
|
||||
# Read new_file and extract msgid and msgstr
|
||||
msgid=""
|
||||
while IFS= read -r line; do
|
||||
if [[ $line == msgid* ]]; then
|
||||
msgid="$line"
|
||||
msgstr=""
|
||||
elif [[ $line == msgstr* ]]; then
|
||||
msgstr="$line"
|
||||
new_translations["$msgid"]="$msgstr"
|
||||
elif [[ $line == "\"\"" ]]; then
|
||||
msgid+="$line"
|
||||
fi
|
||||
done < "$new_file"
|
||||
|
||||
# Open output_file and write merged content
|
||||
msgid=""
|
||||
while IFS= read -r line; do
|
||||
if [[ $line == msgid* ]]; then
|
||||
msgid="$line"
|
||||
msgstr=""
|
||||
elif [[ $line == msgstr* ]]; then
|
||||
msgstr="$line"
|
||||
if [[ -n ${new_translations["$msgid"]} ]]; then
|
||||
echo "$msgid" >> "$output_file"
|
||||
echo "${new_translations["$msgid"]}" >> "$output_file"
|
||||
else
|
||||
echo "$msgid" >> "$output_file"
|
||||
echo "$msgstr" >> "$output_file"
|
||||
fi
|
||||
msgid=""
|
||||
msgstr=""
|
||||
elif [[ $line == "\"\"" ]]; then
|
||||
msgid+="$line"
|
||||
else
|
||||
echo "$line" >> "$output_file"
|
||||
fi
|
||||
done < "$base_file"
|
||||
}
|
||||
|
||||
if [[ $# -ne 3 ]]; then
|
||||
echo "Usage: $0 base_file new_file output_file"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
merge_po_files_preserve_format "base.po" "new.po" "output.po"
|
Loading…
Reference in a new issue