Improve typing of lang.py, and ensure Python 3.8 compatibility

This commit is contained in:
Kiril Kovachev 2024-10-24 20:27:42 +01:00
parent ba1f8895a4
commit e7c814932d

View File

@ -1,10 +1,11 @@
import json import json
import os import os
from pathlib import Path from pathlib import Path
from typing import Dict, Optional
import click import click
from flask import Flask, session, current_app from flask import Flask, session, current_app
LanguageMapping = dict[str, str] LanguageMapping = Dict[str, str]
def load_language(lang_code: str) -> LanguageMapping: def load_language(lang_code: str) -> LanguageMapping:
with open(Path("kanken_online/static/lang", f"{lang_code}.json")) as f: with open(Path("kanken_online/static/lang", f"{lang_code}.json")) as f:
@ -29,11 +30,12 @@ update_languages()
# assert not any((((key in lang) and (key not in EXISTING_STRINGS)) for key in lang) for lang in LANGUAGES) # Ensure no languages have strings not specified by the main index # assert not any((((key in lang) and (key not in EXISTING_STRINGS)) for key in lang) for lang in LANGUAGES) # Ensure no languages have strings not specified by the main index
def localize(text_id: str, language: dict[str, str] = None) -> str: def localize(text_id: str, language: Optional[Dict[str, str]] = None) -> str:
if language is None: if language is None:
preference = session.get("language", "ja") preference = session.get("language", "ja")
language = LANGUAGES.get(preference) language = LANGUAGES.get(preference)
return language[text_id] assert language is not None
return language.get(text_id, text_id)
@click.command("add-string") @click.command("add-string")