From e7c814932d338f9085429b5597663162c718d2d9 Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Thu, 24 Oct 2024 20:27:42 +0100 Subject: [PATCH] Improve typing of lang.py, and ensure Python 3.8 compatibility --- kanken_online/lang.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kanken_online/lang.py b/kanken_online/lang.py index 386b236..7fe81ea 100644 --- a/kanken_online/lang.py +++ b/kanken_online/lang.py @@ -1,10 +1,11 @@ import json import os from pathlib import Path +from typing import Dict, Optional import click from flask import Flask, session, current_app -LanguageMapping = dict[str, str] +LanguageMapping = Dict[str, str] def load_language(lang_code: str) -> LanguageMapping: 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 -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: preference = session.get("language", "ja") language = LANGUAGES.get(preference) - return language[text_id] + assert language is not None + return language.get(text_id, text_id) @click.command("add-string")