diff --git a/po/Reorganize-po-sequence.py b/po/Reorganize-po-sequence.py new file mode 100644 index 000000000..69b953a96 --- /dev/null +++ b/po/Reorganize-po-sequence.py @@ -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) diff --git a/scripts/Reorganize-po-sequence.sh b/scripts/Reorganize-po-sequence.sh new file mode 100644 index 000000000..98ff5bd94 --- /dev/null +++ b/scripts/Reorganize-po-sequence.sh @@ -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"