Last active
December 31, 2024 10:57
Create a sorted plain text dictionary from a .ts XML translation file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Convert a .ts XML translation file. | |
Output: | |
- a plain text file with Markdown context headings (extension ".md") | |
Usage: | |
ts_txt.py sourcefile | |
Copyright (c) 2024 Peter Triesberger | |
For further information see https://github.com/peter88213/ | |
Published under the MIT License (https://opensource.org/licenses/mit-license.php) | |
""" | |
import os | |
import sys | |
import xml.etree.ElementTree as ET | |
def read_ts_file(tsFile): | |
xmlRoot = ET.parse(tsFile).getroot() | |
translations = {} | |
for xmlContext in xmlRoot.iterfind('context'): | |
context = xmlContext.find('name').text | |
translations[context] = {} | |
for xmlMessage in xmlContext.iterfind('message'): | |
translations[context][xmlMessage.find('source').text] = xmlMessage.find('translation').text | |
return translations | |
def write_md_file(translations, mdFile): | |
lines = [] | |
for context in translations: | |
lines.append(f"\n\n# {context}\n") | |
# a Markdown heading for the context | |
contextLines = [] | |
for message in translations[context]: | |
line = f"{message}\t:\t{translations[context][message]}" | |
contextLines.append(line) | |
lines.extend(sorted(contextLines)) | |
with open(mdFile, 'w', encoding='utf-8') as f: | |
f.write('\n'.join(lines)) | |
def main(tsFile): | |
translations = read_ts_file(tsFile) | |
root, __ = os.path.splitext(tsFile) | |
write_md_file(translations, f'{root}.md') | |
if __name__ == '__main__': | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment