diff --git a/Makefile b/Makefile index 63a80e0..6277e05 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ LANGUAGES := en zh_CN zh_TW fr_FR ja_JP ko_KR de_DE es_ES -POT_FILE := locale/beanbot.pot -PO_FILES := $(foreach lang,$(LANGUAGES),locale/$(lang)/LC_MESSAGES/beanbot.po) -MO_FILES := $(foreach lang,$(LANGUAGES),locale/$(lang)/LC_MESSAGES/beanbot.mo) +DOMAIN := beanbot +POT_FILE := locale/$(DOMAIN).pot +PO_FILES := $(foreach lang,$(LANGUAGES),locale/$(lang)/LC_MESSAGES/$(DOMAIN).po) +MO_FILES := $(foreach lang,$(LANGUAGES),locale/$(lang)/LC_MESSAGES/$(DOMAIN).mo) -.PHONY: all gentranslations compiletranslations clean +.PHONY: all gentranslations compiletranslations clean lint all: gentranslations compiletranslations @@ -13,10 +14,10 @@ gentranslations: $(PO_FILES) compiletranslations: $(MO_FILES) $(POT_FILE): **/*.py - xgettext -d beanbot -o $@ $^ + xgettext -d $(DOMAIN) -o $@ $^ define po_rule -locale/$(1)/LC_MESSAGES/beanbot.po: $(POT_FILE) +locale/$(1)/LC_MESSAGES/$(DOMAIN).po: $(POT_FILE) @mkdir -p $$(dir $$@) @if [ ! -f $$@ ]; then \ msginit -i $$< -o $$@ -l $(1); \ @@ -32,3 +33,6 @@ $(foreach lang,$(LANGUAGES),$(eval $(call po_rule,$(lang)))) # clean: # rm -f $(POT_FILE) $(PO_FILES) $(MO_FILES) + +lint: + @ruff check diff --git a/bean_utils/bean.py b/bean_utils/bean.py index 932f6cc..9fe3199 100644 --- a/bean_utils/bean.py +++ b/bean_utils/bean.py @@ -2,7 +2,7 @@ from pathlib import Path from datetime import datetime from decimal import Decimal -from gettext import gettext as _ +from conf.i18n import gettext as _ import re import shlex import subprocess diff --git a/bots/controller.py b/bots/controller.py index 3baf1ec..5fa8466 100644 --- a/bots/controller.py +++ b/bots/controller.py @@ -1,6 +1,6 @@ from datetime import date from dataclasses import dataclass -from gettext import gettext as _ +from conf.i18n import gettext as _ from typing import List, Union, Any from beancount.core.inventory import Inventory import requests diff --git a/bots/mmbot.py b/bots/mmbot.py index ef98114..ade2c3f 100644 --- a/bots/mmbot.py +++ b/bots/mmbot.py @@ -1,6 +1,6 @@ import click from datetime import datetime, timedelta -from gettext import gettext as _ +from conf.i18n import gettext as _ from mmpy_bot import Bot, Settings from mmpy_bot import Plugin, listen_to, listen_webhook from mmpy_bot.plugins.base import PluginManager diff --git a/bots/telegram_bot.py b/bots/telegram_bot.py index df2326d..c1422f7 100644 --- a/bots/telegram_bot.py +++ b/bots/telegram_bot.py @@ -1,6 +1,6 @@ # coding: utf-8 import time -from gettext import gettext as _ +from conf.i18n import gettext as _ import logging from datetime import timedelta, datetime import telegram diff --git a/conf/__init__.py b/conf/__init__.py new file mode 100644 index 0000000..e572d0a --- /dev/null +++ b/conf/__init__.py @@ -0,0 +1,13 @@ +from .i18n import init_locale +from .config_data import Config + + +__all__ = ['config', 'init_locale', "load_config"] + + +config = None + + +def load_config(config_path): + global config + config = Config(config_path) diff --git a/conf.py b/conf/config_data.py similarity index 68% rename from conf.py rename to conf/config_data.py index a351bd3..027cce9 100644 --- a/conf.py +++ b/conf/config_data.py @@ -1,4 +1,3 @@ -import os import yaml @@ -53,20 +52,3 @@ def from_dict(cls, dictionary): config = cls.__new__(cls) config._config = ImmutableDict(dictionary) # noqa: SLF001 return config - - -config = None - - -def load_config(config_path): - global config - config = Config(config_path) - - -def set_locale(): - if config.get("language") is not None: - # Only in this way can I override default language for gettext.gettext - os.environ['LANGUAGE'] = config.get("language") - # `install` also works, but it's not recommended after python 3.8, and it will cause lint error - # gettext.translation("beanbot", config.get("language"), fallback=True).install() - # Otherwise, I can write my own `_` function, which can take from `some_translation.gettext` diff --git a/conf/i18n.py b/conf/i18n.py new file mode 100644 index 0000000..bbd58fb --- /dev/null +++ b/conf/i18n.py @@ -0,0 +1,24 @@ +from pathlib import Path +import gettext as _gettext + + +_DOMAIN = 'beanbot' + +gettext = _gettext.gettext + + +def init_locale(): + """Initialize the locale translation.""" + from . import config + locale_dir = Path(__file__).parent.parent / 'locale' + language = config.get('language') + if language is not None: + # Use custom translation + translation = _gettext.translation(_DOMAIN, locale_dir, [language], fallback=True) + global gettext + gettext = translation.gettext + else: + # Use default translation, and load locale from env + _gettext.bindtextdomain(_DOMAIN, locale_dir) + _gettext.textdomain(_DOMAIN) + diff --git a/main.py b/main.py index fc14771..aa7d187 100644 --- a/main.py +++ b/main.py @@ -1,17 +1,9 @@ import argparse -import gettext -import pathlib import conf import logging from bean_utils.bean import init_bean_manager -def _init_locale(): - locale_dir = pathlib.Path(__file__).parent / 'locale' - gettext.bindtextdomain('beanbot', locale_dir) - gettext.textdomain('beanbot') - - if __name__ == "__main__": # Init logging logging.basicConfig(level=logging.INFO) @@ -30,8 +22,7 @@ def _init_locale(): if args.command is not None: conf.load_config(args.c) # Init i18n - conf.set_locale() - _init_locale() + conf.init_locale() init_bean_manager() if args.command == "telegram":