API Reference¶
fontfinder¶
- default_filter(font: PurePath, suffixes={'.otf', '.ttc', '.ttf', '.woff', '.woff2'}) bool¶
The default filter for
enum_pre_installed_fonts().Returns True if
fonthas one of the specified suffixes and its filename does not contain"fallback".
- enum_langs() Iterator[str]¶
Available languages for
font_supports_lang().
- enum_pre_installed_fonts(*, filter: ~collections.abc.Callable[[~pathlib.PurePath], bool] = <function default_filter>) Iterator[Path]¶
- font_provides_glyphs(font: str | Path, glyphs: str) bool¶
Whether a specified
fontprovides all of the givenglyphs.- Parameters:
glyphs – A string consisting of three or more unique characters.
from kivy_garden.i18n.fontfinder import font_provides_glyphs as f # Roboto, Kivy's default font, lacks CJK glyphs. assert f("Roboto", "ABC") assert not f("Roboto", "漢字한글ひら") assert not f("Roboto", "漢字한글ひらABC") # NotoSerifCJK provides ASCII and CJK glyphs. assert f("NotoSerifCJK-Regular.ttc", "ABC") assert f("NotoSerifCJK-Regular.ttc", "漢字한글ひら") assert f("NotoSerifCJK-Regular.ttc", "漢字한글ひらABC") # Fallback fonts may lack ASCII glyphs. assert not f("DroidSansFallbackFull.ttf", "ABC") assert f("DroidSansFallbackFull.ttf", "漢字한글ひら") assert not f("DroidSansFallbackFull.ttf", "漢字한글ひらABC")
Warning
This function does not produce 100% accurate results. Providing more glyphs improves accuracy at the cost of execution time.
- font_supports_lang(font: str | Path, lang: str) bool¶
Whether a specified
fontprovides the glyphs required for a specifiedlang.from kivy_garden.i18n.fontfinder import font_supports_lang as f assert not f("Roboto", "zh") assert not f("Roboto", "ko") assert not f("Roboto", "ja") assert f("NotoSerifCJK-Regular.ttc", "zh") assert f("NotoSerifCJK-Regular.ttc", "ko") assert f("NotoSerifCJK-Regular.ttc", "ja") # A font that lacks ASCII characters is considered unable to support any language. assert not f("DroidSansFallbackFull.ttf", "zh") assert not f("DroidSansFallbackFull.ttf", "ko") assert not f("DroidSansFallbackFull.ttf", "ja")
Warning
This function does not produce 100% accurate results.
- register_lang(lang: str, discriminant: str)¶
Enables a language in the
font_supports_lang().register_lang('th', "ราชอAB") # Thai language
localizer¶
- exception FontNotFoundError¶
- class Localizer¶
- _()¶
(read-only) A callable object that takes a
msgid, and returns the correspongingmsgstraccording to the “current language”.loc = Localizer(...) loc.lang = "en" print(loc._("app title")) # => "My First Kivy Program" loc.lang = "ja" print(loc._("app title")) # => "初めてのKivyプログラム"
- __init__(translator_factory: Callable[[str], Callable[[str], str]] = None, *, lang: str = 'en', font_picker: Callable[[str], str] = None)¶
- font_name: str¶
(read-only) A font that provides the glyphs required for rendering the “current language”.
loc = Localizer(...) loc.lang = "en" print(loc.font_name) # => "Roboto" loc.lang = "ko" print(loc.font_name) # => "<a pre-installed Korean font>"
- install(*, name)¶
Makes the localizer accessble from kv without any import-statements.
loc = Localizer(...) loc.install(name='l')
Label: font_name: l.font_name text: l._("msgid")
- Raises:
ValueError – if the
namehas already been used.
- uninstall(*, name)¶