Set console font automatically when selecting language

Add console_font field to languages.json and Language dataclass.
When a language is activated, setfont is called automatically,
falling back to default8x16 on error or for languages without
a custom font. Also activate translation when loading language
from config file.
This commit is contained in:
Softer 2026-04-02 06:19:05 +03:00
parent deb8080322
commit f6f6b4fbf1
4 changed files with 32 additions and 5 deletions

View File

@ -142,6 +142,7 @@ class ArchConfig:
if archinstall_lang := args_config.get('archinstall-language', None):
arch_config.archinstall_language = translation_handler.get_language_by_name(archinstall_lang)
translation_handler.activate(arch_config.archinstall_language)
if disk_config := args_config.get('disk_config', {}):
enc_password = args_config.get('encryption_password', '')

View File

@ -106,9 +106,9 @@ async def select_archinstall_language(languages: list[Language], preset: Languag
group = MenuItemGroup(items, sort_items=True)
group.set_focus_by_value(preset)
title = 'NOTE: If a language can not displayed properly, a proper font must be set manually in the console.\n'
title += 'All available fonts can be found in "/usr/share/kbd/consolefonts"\n'
title += 'e.g. setfont LatGrkCyr-8x16 (to display latin/greek/cyrillic characters)\n'
title = 'NOTE: Console font will be set automatically for supported languages.\n'
title += 'For other languages, fonts can be found in "/usr/share/kbd/consolefonts"\n'
title += 'and set manually with: setfont <fontname>\n'
result = await Selection[Language](
header=title,

View File

@ -2,6 +2,7 @@ import builtins
import gettext
import json
import os
import subprocess
from dataclasses import dataclass
from pathlib import Path
from typing import override
@ -14,6 +15,7 @@ class Language:
translation: gettext.NullTranslations
translation_percent: int
translated_lang: str | None
console_font: str | None = None
@property
def display_name(self) -> str:
@ -31,6 +33,28 @@ class Language:
return self.name_en
_DEFAULT_FONT = 'default8x16'
def _set_console_font(font_name: str | None) -> None:
"""
Set the console font via setfont.
Falls back to default8x16 if font_name is None or setfont fails.
"""
target = font_name or _DEFAULT_FONT
try:
result = subprocess.run(['setfont', target], capture_output=True, timeout=10)
if result.returncode != 0 and target != _DEFAULT_FONT:
subprocess.run(['setfont', _DEFAULT_FONT], capture_output=True, timeout=10)
except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
if target != _DEFAULT_FONT:
try:
subprocess.run(['setfont', _DEFAULT_FONT], capture_output=True, timeout=10)
except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
pass
class TranslationHandler:
def __init__(self) -> None:
self._base_pot = 'base.pot'
@ -57,6 +81,7 @@ class TranslationHandler:
abbr = mapping_entry['abbr']
lang = mapping_entry['lang']
translated_lang = mapping_entry.get('translated_lang', None)
console_font = mapping_entry.get('console_font', None)
try:
# get a translation for a specific language
@ -71,7 +96,7 @@ class TranslationHandler:
# prevent cases where the .pot file is out of date and the percentage is above 100
percent = min(100, percent)
language = Language(abbr, lang, translation, percent, translated_lang)
language = Language(abbr, lang, translation, percent, translated_lang, console_font)
languages.append(language)
except FileNotFoundError as err:
raise FileNotFoundError(f"Could not locate language file for '{lang}': {err}")
@ -133,6 +158,7 @@ class TranslationHandler:
"""
# The install() call has the side effect of assigning GNUTranslations.gettext to builtins._
language.translation.install()
_set_console_font(language.console_font)
def _get_locales_dir(self) -> Path:
"""

View File

@ -169,7 +169,7 @@
{"abbr": "tr", "lang": "Turkish", "translated_lang" : "Türkçe"},
{"abbr": "tw", "lang": "Twi"},
{"abbr": "ug", "lang": "Uighur"},
{"abbr": "uk", "lang": "Ukrainian"},
{"abbr": "uk", "lang": "Ukrainian", "console_font": "UniCyr_8x16"},
{"abbr": "ur", "lang": "Urdu", "translated_lang": "اردو"},
{"abbr": "uz", "lang": "Uzbek", "translated_lang": "O'zbek"},
{"abbr": "ve", "lang": "Venda"},