123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- '''
- languageExport.py [--single]
- Export LCD language strings to CSV files for easier translation.
- Use languageImport.py to import CSV into the language files.
- Use --single to export all languages to a single CSV file.
- '''
- import re
- from pathlib import Path
- from sys import argv
- from languageUtil import namebyid
- LANGHOME = "Marlin/src/lcd/language"
- MULTISHEET = '--single' not in argv[1:]
- OUTDIR = 'out-csv'
- if not Path(LANGHOME).is_dir():
- print("Error: Couldn't find the '%s' directory." % LANGHOME)
- print("Edit LANGHOME or cd to the root of the repo before running.")
- exit(1)
- LIMIT = 0
- language_strings = { 'en': {} }
- names = {}
- langfiles = sorted(list(Path(LANGHOME).glob('language_*.h')))
- for langfile in langfiles:
-
- langcode = langfile.name.replace('language_', '').replace('.h', '')
-
- if langcode in ['test']: continue
-
- f = open(langfile, 'r', encoding='utf-8')
- if not f: continue
-
- wideflag, tallflag = False, False
-
- stringcount = 0
-
- strings = { 'narrow': {}, 'wide': {}, 'tall': {} }
-
- for line in f:
-
- line = line.split("//")[0].strip()
- if line.endswith(';'): line = line[:-1].strip()
-
- if line.startswith("#endif") or line.startswith("#else"):
- wideflag, tallflag = False, False
- elif re.match(r'#if.*WIDTH\s*>=?\s*2[01].*', line): wideflag = True
- elif re.match(r'#if.*LCD_HEIGHT\s*>=?\s*4.*', line): tallflag = True
-
- match = re.match(r'LSTR\s+([A-Z0-9_]+)\s*=\s*(.+)\s*', line)
- if match:
-
- name, value = match.group(1), match.group(2).replace('\\"', '$$$')
-
- value = re.sub(r'_UxGT\((".*?")\)', r'\1', value)
-
- multiline = 0
- multimatch = re.match(r'.*MSG_(\d)_LINE\s*\(\s*(.+?)\s*\).*', value)
- if multimatch:
- multiline = int(multimatch.group(1))
- value = '|' + re.sub(r'"\s*,\s*"', '|', multimatch.group(2))
-
- value = re.sub(r' *([A-Z0-9]+_[A-Z0-9_]+) *', r'(\1)', value)
-
- value = re.sub(r'"(.*?)"', r'\1', value).replace('$$$', '""')
-
- names[name] = 1
-
- strings['tall' if tallflag else 'wide' if wideflag else 'narrow'][name] = value
-
- stringcount += 1
-
- if LIMIT and stringcount >= LIMIT: break
-
- f.close()
-
- language_strings[langcode] = strings
- langcodes = list(language_strings.keys())
- print("Found %s distinct LCD strings." % len(names))
- def write_csv_lang(f, strings, name):
- f.write(',')
- if name in strings['narrow']: f.write('"%s"' % strings['narrow'][name])
- f.write(',')
- if name in strings['wide']: f.write('"%s"' % strings['wide'][name])
- f.write(',')
- if name in strings['tall']: f.write('"%s"' % strings['tall'][name])
- if MULTISHEET:
-
-
-
- Path.mkdir(Path(OUTDIR), exist_ok=True)
- for lang in langcodes:
- with open("%s/language_%s.csv" % (OUTDIR, lang), 'w', encoding='utf-8') as f:
- lname = lang + ' ' + namebyid(lang)
- header = ['name', lname, lname + ' (wide)', lname + ' (tall)']
- f.write('"' + '","'.join(header) + '"\n')
- for name in names.keys():
- f.write('"' + name + '"')
- write_csv_lang(f, language_strings[lang], name)
- f.write('\n')
- else:
-
-
-
- with open("languages.csv", 'w', encoding='utf-8') as f:
- header = ['name']
- for lang in langcodes:
- lname = lang + ' ' + namebyid(lang)
- header += [lname, lname + ' (wide)', lname + ' (tall)']
- f.write('"' + '","'.join(header) + '"\n')
- for name in names.keys():
- f.write('"' + name + '"')
- for lang in langcodes: write_csv_lang(f, language_strings[lang], name)
- f.write('\n')
|