Multilang prerequisites (#24999)
	
		
	
				
					
				
			* some supporting code for multilang
* for now just english
* test for missing language files
* test for checking if ts file is up to date
* Skip test if causes exception, other test catches this
Test test
Should also work
should now fail
revert
rmn
* add to files_common
* fix files_common
* newlines
* no need to not update
* comment
* only english
* double quotes
* switch around
old-commit-hash: 7178800d84
			
			
				vw-mqb-aeb
			
			
		
							parent
							
								
									31fddf592f
								
							
						
					
					
						commit
						cb82e76c75
					
				
				 5 changed files with 94 additions and 4 deletions
			
			
		@ -0,0 +1,50 @@ | 
				
			||||
#!/usr/bin/env python3 | 
				
			||||
import json | 
				
			||||
import os | 
				
			||||
import unittest | 
				
			||||
 | 
				
			||||
from selfdrive.ui.update_translations import TRANSLATIONS_DIR, LANGUAGES_FILE, update_translations | 
				
			||||
 | 
				
			||||
 | 
				
			||||
class TestTranslations(unittest.TestCase): | 
				
			||||
  @classmethod | 
				
			||||
  def setUpClass(cls): | 
				
			||||
    with open(LANGUAGES_FILE, "r") as f: | 
				
			||||
      cls.translation_files = json.load(f) | 
				
			||||
 | 
				
			||||
  def test_missing_translation_files(self): | 
				
			||||
    for name, file in self.translation_files.items(): | 
				
			||||
      with self.subTest(name=name, file=file): | 
				
			||||
        if not len(file): | 
				
			||||
          self.skipTest(f"{name} translation has no file") | 
				
			||||
 | 
				
			||||
        self.assertTrue(os.path.exists(os.path.join(TRANSLATIONS_DIR, f"{file}.ts")), | 
				
			||||
                        f"{name} has no XML translation file, run selfdrive/ui/update_translations.py") | 
				
			||||
        self.assertTrue(os.path.exists(os.path.join(TRANSLATIONS_DIR, f"{file}.qm")), | 
				
			||||
                        f"{name} has no compiled QM translation file, run selfdrive/ui/update_translations.py --release") | 
				
			||||
 | 
				
			||||
  def test_translations_updated(self): | 
				
			||||
    suffix = "_test" | 
				
			||||
    update_translations(suffix=suffix) | 
				
			||||
 | 
				
			||||
    for name, file in self.translation_files.items(): | 
				
			||||
      with self.subTest(name=name, file=file): | 
				
			||||
        cur_tr_file = os.path.join(TRANSLATIONS_DIR, f"{file}.ts") | 
				
			||||
        new_tr_file = os.path.join(TRANSLATIONS_DIR, f"{file}{suffix}.ts") | 
				
			||||
 | 
				
			||||
        if not len(file): | 
				
			||||
          self.skipTest(f"{name} translation has no file") | 
				
			||||
        elif not os.path.exists(cur_tr_file): | 
				
			||||
          self.skipTest(f"{name} missing translation file")  # caught by test_missing_translation_files | 
				
			||||
 | 
				
			||||
        with open(cur_tr_file, "r") as f: | 
				
			||||
          cur_translations = f.read() | 
				
			||||
        with open(new_tr_file, "r") as f: | 
				
			||||
          new_translations = f.read() | 
				
			||||
 | 
				
			||||
        self.assertEqual(cur_translations, new_translations, | 
				
			||||
                         f"{name} translation file out of date. Run selfdrive/ui/update_translations.py to update the translation files") | 
				
			||||
 | 
				
			||||
 | 
				
			||||
if __name__ == "__main__": | 
				
			||||
  unittest.main() | 
				
			||||
@ -0,0 +1,3 @@ | 
				
			||||
{ | 
				
			||||
  "English": "" | 
				
			||||
} | 
				
			||||
@ -0,0 +1,37 @@ | 
				
			||||
#!/usr/bin/env python3 | 
				
			||||
import argparse | 
				
			||||
import os | 
				
			||||
import json | 
				
			||||
 | 
				
			||||
from common.basedir import BASEDIR | 
				
			||||
 | 
				
			||||
UI_DIR = os.path.join(BASEDIR, "selfdrive", "ui") | 
				
			||||
TRANSLATIONS_DIR = os.path.join(UI_DIR, "translations") | 
				
			||||
LANGUAGES_FILE = os.path.join(TRANSLATIONS_DIR, "languages.json") | 
				
			||||
 | 
				
			||||
 | 
				
			||||
def update_translations(release=False, suffix=""): | 
				
			||||
  with open(LANGUAGES_FILE, "r") as f: | 
				
			||||
    translation_files = json.load(f) | 
				
			||||
 | 
				
			||||
  for name, file in translation_files.items(): | 
				
			||||
    if not len(file): | 
				
			||||
      print(f"{name} has no translation file, skipping...") | 
				
			||||
      continue | 
				
			||||
 | 
				
			||||
    tr_file = os.path.join(TRANSLATIONS_DIR, f"{file}{suffix}.ts") | 
				
			||||
    ret = os.system(f"lupdate -recursive {UI_DIR} -ts {tr_file}") | 
				
			||||
    assert ret == 0 | 
				
			||||
 | 
				
			||||
    if release: | 
				
			||||
      ret = os.system(f"lrelease {tr_file}") | 
				
			||||
      assert ret == 0 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
if __name__ == "__main__": | 
				
			||||
  parser = argparse.ArgumentParser(description="Update translation files for UI", | 
				
			||||
                                   formatter_class=argparse.ArgumentDefaultsHelpFormatter) | 
				
			||||
  parser.add_argument("--release", action="store_true", help="Create compiled QM translation files used by UI") | 
				
			||||
  args = parser.parse_args() | 
				
			||||
 | 
				
			||||
  update_translations(args.release) | 
				
			||||
					Loading…
					
					
				
		Reference in new issue