Add sanity check for syntax errors in translations (#1978)
The pull request should be blocked on basic typos breaking the SMC file.
This commit is contained in:
		
							parent
							
								
									b1b993989a
								
							
						
					
					
						commit
						4e6774befb
					
				
							
								
								
									
										29
									
								
								.github/workflows/translations_sanitycheck.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								.github/workflows/translations_sanitycheck.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,29 @@
 | 
			
		||||
name: Sanity check translation phrases
 | 
			
		||||
on:
 | 
			
		||||
  push:
 | 
			
		||||
    branches:
 | 
			
		||||
      - master
 | 
			
		||||
    paths:
 | 
			
		||||
      - 'translations/**'
 | 
			
		||||
  pull_request:
 | 
			
		||||
    branches:
 | 
			
		||||
     - master
 | 
			
		||||
    paths:
 | 
			
		||||
      - 'translations/**'
 | 
			
		||||
  workflow_dispatch:
 | 
			
		||||
jobs:
 | 
			
		||||
  check_translations:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v3
 | 
			
		||||
 | 
			
		||||
      - uses: actions/setup-python@v4
 | 
			
		||||
        name: Setup Python 3.10
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.10"
 | 
			
		||||
 | 
			
		||||
      - name: Check translation phrases syntax
 | 
			
		||||
        working-directory: tools/language_check
 | 
			
		||||
        run: |
 | 
			
		||||
          python ./sanity_check.py 
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										42
									
								
								tools/language_check/sanity_check.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								tools/language_check/sanity_check.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
			
		||||
#!/usr/bin/python3
 | 
			
		||||
# Copyright (c) 2023 Peace-Maker
 | 
			
		||||
import pathlib
 | 
			
		||||
from smc_parser import smc_string_to_dict
 | 
			
		||||
 | 
			
		||||
# Parse the languages.cfg file to know which languages could be available
 | 
			
		||||
had_problem = False
 | 
			
		||||
file_count = 0
 | 
			
		||||
languages_cfg = smc_string_to_dict(
 | 
			
		||||
    pathlib.Path('../../configs/languages.cfg').read_text('utf-8'))
 | 
			
		||||
print(f'Checking {len(languages_cfg["Languages"][0])} languages...')
 | 
			
		||||
 | 
			
		||||
# Try to parse all the files as a simple smoke test for syntax errors
 | 
			
		||||
for langid, lang in languages_cfg['Languages'][0].items():
 | 
			
		||||
    if langid == 'en':
 | 
			
		||||
        path = '../../translations'
 | 
			
		||||
    else:
 | 
			
		||||
        path = f'../../translations/{langid}'
 | 
			
		||||
 | 
			
		||||
    for file in pathlib.Path(path).glob('*.txt'):
 | 
			
		||||
        if not file.is_file():
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        file_count += 1
 | 
			
		||||
        try:
 | 
			
		||||
            phrases = smc_string_to_dict(file.read_text('utf-8'))
 | 
			
		||||
            if 'Phrases' not in phrases:
 | 
			
		||||
                print(
 | 
			
		||||
                    f'Error in {langid}/{file.name}: File does not start with a "Phrases" section'
 | 
			
		||||
                )
 | 
			
		||||
                had_problem = True
 | 
			
		||||
                continue
 | 
			
		||||
        except Exception as ex:
 | 
			
		||||
            print(f'Error in {langid}/{file.name}: Error parsing: {ex}')
 | 
			
		||||
            had_problem = True
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
print(f'Checked {file_count} files.')
 | 
			
		||||
 | 
			
		||||
if had_problem:
 | 
			
		||||
    print('Sanity check failed!')
 | 
			
		||||
    exit(1)
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user