Skip to content

Commit

Permalink
l10n suppport
Browse files Browse the repository at this point in the history
  • Loading branch information
StdioA committed Aug 22, 2024
1 parent 88bab5d commit 10bd617
Show file tree
Hide file tree
Showing 19 changed files with 349 additions and 12 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

LANGUAGES := en zh_CN zh_TW fr_FR ja_JP ko_KR

gentranslations:
xgettext -d beanbot -o locale/beanbot.pot **/*.py
for lang in $(LANGUAGES); do \
mkdir -p locale/$$lang/LC_MESSAGES; \
if [ -f locale/$$lang/LC_MESSAGES/beanbot.po ]; then \
msgmerge --update locale/$$lang/LC_MESSAGES/beanbot.po locale/beanbot.pot; \
else \
msginit -i locale/beanbot.pot -o locale/$$lang/LC_MESSAGES/beanbot.po -l $$lang; \
fi; \
done

compiletranslations:
for lang in $(LANGUAGES); do \
msgfmt -o locale/$$lang/LC_MESSAGES/beanbot.mo locale/$$lang/LC_MESSAGES/beanbot.po; \
done
10 changes: 5 additions & 5 deletions bots/mmbot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click
from datetime import datetime, timedelta
from gettext 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
Expand Down Expand Up @@ -78,8 +79,8 @@ async def render(self, message: Message):
"attachments": [
{
"actions": [
self.gen_action("submit", "提交", tx_content),
self.gen_action("cancel", "取消", tx_content),
self.gen_action("submit", _("Submit"), tx_content),
self.gen_action("cancel", _("Cancel"), tx_content),
]
}
]
Expand Down Expand Up @@ -107,9 +108,8 @@ async def submit_listener(self, event: WebHookEvent):
"data": {"post": {"id": post_id}},
}), reaction)

# 需要改 listen_to 实现中对 reg 的替换代码,以支持单独命令输入
@listen_to("bill", direct_only=True, allowed_users=[OWNER_NAME])
@click.command(help="查询账户变动")
@click.command(help=_("Query account changes"))
@click.option("-l", "--level", default=2, type=int)
@click.argument("date", nargs=-1, type=str)
def bill(self, message: Message, level: int, date: str):
Expand All @@ -126,7 +126,7 @@ def bill(self, message: Message, level: int, date: str):
self.driver.reply_to(message, f"**{resp_table.title}**\n\n{result}")

@listen_to("expense", direct_only=True, allowed_users=[OWNER_NAME])
@click.command(help="查询支出")
@click.command(help=_("Query expenses"))
@click.option("-l", "--level", default=2, type=int)
@click.argument("args", nargs=-1, type=str)
def expense(self, message: Message, level: int, args: str):
Expand Down
13 changes: 7 additions & 6 deletions bots/telegram_bot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# coding: utf-8
import time
import telegram
from gettext import gettext as _
import logging
from datetime import timedelta, datetime
import telegram
from telegram import Update
from telegram.ext import (
Application, filters,
Expand Down Expand Up @@ -103,8 +104,8 @@ async def expense(update, context):


_button_list = [
telegram.InlineKeyboardButton("提交", callback_data="提交"),
telegram.InlineKeyboardButton("取消", callback_data="取消"),
telegram.InlineKeyboardButton(_("Submit"), callback_data="submit"),
telegram.InlineKeyboardButton(_("Cancel"), callback_data="cancel"),
]
_pending_txs_reply_markup = telegram.InlineKeyboardMarkup([_button_list])

Expand Down Expand Up @@ -133,12 +134,12 @@ async def callback(update, context):
query = update.callback_query
await query.answer()

if choice == "提交":
result_msg = "已提交 ✅"
if choice == "submit":
result_msg = _("Submitted ✅")
bean_manager.commit_trx(trx)
logging.info("Commit transaction: %s\n", trx)
else:
result_msg = "已取消 ❌"
result_msg = _("Cancelled ❌")
logging.info("Cancel transaction")

if result_msg:
Expand Down
10 changes: 9 additions & 1 deletion conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import yaml
import gettext


_ImmutableError = TypeError("This dictionary is immutable")
Expand Down Expand Up @@ -35,7 +37,7 @@ def __init__(self, config_path):
def __bool__(self):
return bool(self._config)

def get(self, key, default):
def get(self, key, default=None):
return self._config.get(key, default)

def __getattr__(self, key):
Expand All @@ -60,3 +62,9 @@ def from_dict(cls, dictionary):
def load_config(config_path):
global config
config = Config(config_path)


def set_locale():
if config.get("language") is not None:
os.environ['LANGUAGE'] = config.get("language")
gettext.translation("beanbot", config.get("language"), fallback=True).install()
2 changes: 2 additions & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: zh_CN # If set, it can override the default locale from environment variables

beancount:
filename: main.bean # The entrypoint for all transactions, and generated transaction will also append to this file
currency: CNY
Expand Down
42 changes: 42 additions & 0 deletions locale/beanbot.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-22 21:08+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: bots/mmbot.py:82 bots/telegram_bot.py:107
msgid "Submit"
msgstr ""

#: bots/mmbot.py:83 bots/telegram_bot.py:108
msgid "Cancel"
msgstr ""

#: bots/mmbot.py:112
msgid "Query account changes"
msgstr ""

#: bots/mmbot.py:129
msgid "Query expenses"
msgstr ""

#: bots/telegram_bot.py:138
msgid "Submitted ✅"
msgstr ""

#: bots/telegram_bot.py:142
msgid "Cancelled ❌"
msgstr ""
Binary file added locale/en/LC_MESSAGES/beanbot.mo
Binary file not shown.
42 changes: 42 additions & 0 deletions locale/en/LC_MESSAGES/beanbot.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# English translations for PACKAGE package.
# Copyright (C) 2024 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# David Dai <[email protected]>, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-22 20:40+0800\n"
"PO-Revision-Date: 2024-08-22 20:40+0800\n"
"Last-Translator: David Dai <[email protected]>\n"
"Language-Team: English\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: bots/mmbot.py:82 bots/telegram_bot.py:107
msgid "Submit"
msgstr "Submit"

#: bots/mmbot.py:83 bots/telegram_bot.py:108
msgid "Cancel"
msgstr "Cancel"

#: bots/mmbot.py:112
msgid "Query account changes"
msgstr "Query account changes"

#: bots/mmbot.py:129
msgid "Query expenses"
msgstr "Query expenses"

#: bots/telegram_bot.py:138
msgid "Submitted ✅"
msgstr "Submitted ✅"

#: bots/telegram_bot.py:142
msgid "Cancelled ❌"
msgstr "Cancelled ❌"
Binary file added locale/fr_FR/LC_MESSAGES/beanbot.mo
Binary file not shown.
43 changes: 43 additions & 0 deletions locale/fr_FR/LC_MESSAGES/beanbot.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# French translations for PACKAGE package
# Traductions françaises du paquet PACKAGE.
# Copyright (C) 2024 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# David Dai <[email protected]>, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-22 21:07+0800\n"
"PO-Revision-Date: 2024-08-22 21:07+0800\n"
"Last-Translator: David Dai <[email protected]>\n"
"Language-Team: French <[email protected]>\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: bots/mmbot.py:82 bots/telegram_bot.py:107
msgid "Submit"
msgstr "Soumettre"

#: bots/mmbot.py:83 bots/telegram_bot.py:108
msgid "Cancel"
msgstr "Annuler"

#: bots/mmbot.py:112
msgid "Query account changes"
msgstr "Interroger les changements de compte"

#: bots/mmbot.py:129
msgid "Query expenses"
msgstr "Interroger les dépenses"

#: bots/telegram_bot.py:138
msgid "Submitted ✅"
msgstr "Soumis ✅"

#: bots/telegram_bot.py:142
msgid "Cancelled ❌"
msgstr "Annulé ❌"
Binary file added locale/ja_JP/LC_MESSAGES/beanbot.mo
Binary file not shown.
43 changes: 43 additions & 0 deletions locale/ja_JP/LC_MESSAGES/beanbot.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Japanese translations for PACKAGE package
# PACKAGE �ѥå��������Ф������.
# Copyright (C) 2024 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# David Dai <[email protected]>, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-22 21:07+0800\n"
"PO-Revision-Date: 2024-08-22 21:07+0800\n"
"Last-Translator: David Dai <[email protected]>\n"
"Language-Team: Japanese <[email protected]>\n"
"Language: ja\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"

#: bots/mmbot.py:82 bots/telegram_bot.py:107
msgid "Submit"
msgstr "送信"

#: bots/mmbot.py:83 bots/telegram_bot.py:108
msgid "Cancel"
msgstr "キャンセル"

#: bots/mmbot.py:112
msgid "Query account changes"
msgstr "口座変更の照会"

#: bots/mmbot.py:129
msgid "Query expenses"
msgstr "経費の照会"

#: bots/telegram_bot.py:138
msgid "Submitted ✅"
msgstr "送信済み ✅"

#: bots/telegram_bot.py:142
msgid "Cancelled ❌"
msgstr "キャンセル済み ❌"
Binary file added locale/ko_KR/LC_MESSAGES/beanbot.mo
Binary file not shown.
43 changes: 43 additions & 0 deletions locale/ko_KR/LC_MESSAGES/beanbot.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Korean translations for PACKAGE package
# PACKAGE ��Ű���� ���� �ѱ��� ������.
# Copyright (C) 2024 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# David Dai <[email protected]>, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-22 21:08+0800\n"
"PO-Revision-Date: 2024-08-22 21:08+0800\n"
"Last-Translator: David Dai <[email protected]>\n"
"Language-Team: Korean <[email protected]>\n"
"Language: ko\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"

#: bots/mmbot.py:82 bots/telegram_bot.py:107
msgid "Submit"
msgstr "제출"

#: bots/mmbot.py:83 bots/telegram_bot.py:108
msgid "Cancel"
msgstr "취소"

#: bots/mmbot.py:112
msgid "Query account changes"
msgstr "계정 변경 조회"

#: bots/mmbot.py:129
msgid "Query expenses"
msgstr "지출 조회"

#: bots/telegram_bot.py:138
msgid "Submitted ✅"
msgstr "제출됨 ✅"

#: bots/telegram_bot.py:142
msgid "Cancelled ❌"
msgstr "취소됨 ❌"
Binary file added locale/zh_CN/LC_MESSAGES/beanbot.mo
Binary file not shown.
42 changes: 42 additions & 0 deletions locale/zh_CN/LC_MESSAGES/beanbot.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Chinese translations for PACKAGE package
# PACKAGE 软件包的简体中文翻译.
# Copyright (C) 2024 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# David Dai <[email protected]>, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-22 20:40+0800\n"
"PO-Revision-Date: 2024-08-22 20:40+0800\n"
"Last-Translator: David Dai <[email protected]>\n"
"Language-Team: Chinese (simplified) <[email protected]>\n"
"Language: zh_CN\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: bots/mmbot.py:82 bots/telegram_bot.py:107
msgid "Submit"
msgstr "提交"

#: bots/mmbot.py:83 bots/telegram_bot.py:108
msgid "Cancel"
msgstr "取消"

#: bots/mmbot.py:112
msgid "Query account changes"
msgstr "查询账户变更"

#: bots/mmbot.py:129
msgid "Query expenses"
msgstr "查询支出"

#: bots/telegram_bot.py:138
msgid "Submitted ✅"
msgstr "已提交 ✅"

#: bots/telegram_bot.py:142
msgid "Cancelled ❌"
msgstr "已取消 ❌"
Binary file added locale/zh_TW/LC_MESSAGES/beanbot.mo
Binary file not shown.
Loading

0 comments on commit 10bd617

Please sign in to comment.