2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2019-04-03 03:07:38 +00:00
|
|
|
import gettext
|
2018-05-02 01:21:07 +00:00
|
|
|
import os
|
2025-03-10 01:21:48 +00:00
|
|
|
from typing import Callable, Tuple
|
2019-04-03 03:07:38 +00:00
|
|
|
|
2024-11-18 11:56:38 +00:00
|
|
|
from .utils import cache, get_resource_dir
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-08-20 19:49:19 +00:00
|
|
|
# Use N_ to mark a string for translation but _not_ immediately translate it.
|
|
|
|
# reference: https://docs.python.org/3/library/gettext.html#deferred-translations
|
|
|
|
# Makefile configures pybabel to treat N_() the same as _()
|
2018-08-22 00:32:50 +00:00
|
|
|
|
|
|
|
|
2025-03-10 01:21:48 +00:00
|
|
|
def N_(message: str) -> str:
|
|
|
|
return message
|
2018-08-20 19:49:19 +00:00
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2025-03-10 01:21:48 +00:00
|
|
|
def localize(languages=None) -> Tuple[Callable[[str], str], gettext.NullTranslations]:
|
2024-11-18 11:56:38 +00:00
|
|
|
locale_dir = get_resource_dir('locales')
|
2018-05-02 01:21:07 +00:00
|
|
|
|
|
|
|
global translation, _
|
|
|
|
|
|
|
|
translation = gettext.translation("inkstitch", locale_dir, fallback=True)
|
2021-03-04 17:40:53 +00:00
|
|
|
_ = translation.gettext
|
2025-03-10 01:21:48 +00:00
|
|
|
return (_, translation)
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2019-04-03 03:07:38 +00:00
|
|
|
@cache
|
|
|
|
def get_languages():
|
|
|
|
"""return a list of languages configured by the user
|
|
|
|
|
|
|
|
I really wish gettext provided this as a function. Instead, we've duplicated
|
|
|
|
its code below.
|
|
|
|
"""
|
|
|
|
|
|
|
|
languages = []
|
|
|
|
|
|
|
|
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
|
|
|
|
val = os.environ.get(envar)
|
|
|
|
if val:
|
|
|
|
languages = val.split(':')
|
|
|
|
break
|
|
|
|
|
|
|
|
if 'C' not in languages:
|
|
|
|
languages.append('C')
|
|
|
|
|
|
|
|
return languages
|
|
|
|
|
|
|
|
|
2025-03-10 01:21:48 +00:00
|
|
|
_, translation = localize()
|