diff --git a/iamai/cli.py b/iamai/cli.py index 47a621db..34f1ed3d 100644 --- a/iamai/cli.py +++ b/iamai/cli.py @@ -6,23 +6,27 @@ from .const import __version__ # Set up message catalog access -localedir = os.path.join(os.path.dirname(__file__), 'locale') -gettext.bindtextdomain('messages', localedir) -gettext.textdomain('messages') +localedir = os.path.join(os.path.dirname(__file__), "locale") +gettext.bindtextdomain("messages", localedir) +gettext.textdomain("messages") _ = gettext.gettext + @click.group() def cli(): pass + @cli.command() @click.argument("plugin_name", required=True) def install(plugin_name): click.echo(_("Installing plugin: {plugin_name}").format(plugin_name=plugin_name)) + @cli.command() def version(): click.echo(_("Current version: {version}").format(version=__version__)) + def cli_func(*args): - cli(*args) \ No newline at end of file + cli(*args) diff --git a/iamai/i18n.py b/iamai/i18n.py index 95082053..7da65af3 100644 --- a/iamai/i18n.py +++ b/iamai/i18n.py @@ -1,11 +1,49 @@ +import os +import polib import gettext +from typing import List -t = gettext.translation("iamai", "/locale") +localedir = os.path.join(os.path.dirname(__file__), "locale") +def setup_gettext( + domain: str = os.path.basename(__file__).strip(".py"), + localedir: str = localedir, + languages: List[str] = ["en"], +): + try: + # Try to bind the specified domain + translation = gettext.translation(domain, localedir, languages=languages) + compile_mo_files(localedir, domain) + print("translation found.") + except FileNotFoundError: + # Fallback to the default domain 'messages' if the specified domain is not found + _domain = "messages" + translation = gettext.translation( + _domain, localedir, languages=languages, fallback=True + ) + compile_mo_files(localedir, _domain) + print("translation not found, fallback to default domain 'messages'") -def _(message): - return t.gettext(message) + # Install the translation object globally + translation.install() + return translation.gettext + + +def compile_mo_files(localedir, domain): + for lang in os.listdir(localedir): + po_path = os.path.join(localedir, lang, "LC_MESSAGES", f"{domain}.po") + mo_path = os.path.join(localedir, lang, "LC_MESSAGES", f"{domain}.mo") + print(lang, mo_path, po_path) + if os.path.exists(po_path): + # Compile if .mo file doesn't exist or is older than the .po file + if not os.path.exists(mo_path) or os.path.getmtime( + po_path + ) > os.path.getmtime(mo_path): + po = polib.pofile(po_path) + po.save_as_mofile(mo_path) + print(f"Compiled {po_path} to {mo_path}") if __name__ == "__main__": - print(_("hello world")) + _ = setup_gettext(domain="1", languages=["zh"]) + print(_("hello {name}").format(name="baka")) diff --git a/iamai/locale/zh/LC_MESSAGES/i18n.po b/iamai/locale/zh/LC_MESSAGES/i18n.po new file mode 100644 index 00000000..eaf79b52 --- /dev/null +++ b/iamai/locale/zh/LC_MESSAGES/i18n.po @@ -0,0 +1,22 @@ +# Chinese translations for iamai package +# Copyright (C) 2024 ORGANIZATION +# This file is distributed under the same license as the iamai package. +msgid "" +msgstr "" +"Project-Id-Version: iamai\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-03-21 12:00+0800\n" +"PO-Revision-Date: 2024-03-21 12:00+0800\n" +"Last-Translator: FULL NAME \n" +"Language-Team: zh \n" +"Language: zh\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: manual\n" + +msgid "hello world" +msgstr "你好世界" + +msgid "hello {name}" +msgstr "你好 {name}" \ No newline at end of file diff --git a/iamai/locale/zh/LC_MESSAGES/messages.po b/iamai/locale/zh/LC_MESSAGES/messages.po index 74e9251d..5266f176 100644 --- a/iamai/locale/zh/LC_MESSAGES/messages.po +++ b/iamai/locale/zh/LC_MESSAGES/messages.po @@ -19,4 +19,8 @@ msgid "Installing plugin: {plugin_name}" msgstr "正在安装插件:{plugin_name}" msgid "Current version: {version}" -msgstr "当前版本:{version}" \ No newline at end of file +msgstr "当前版本:{version}" + + +msgid "hello {name}" +msgstr "你好 {name}" \ No newline at end of file diff --git a/pdm.lock b/pdm.lock index 2acdcf5d..34286b8e 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "adapters", "dev", "docs", "lint", "test", "typing"] strategy = ["inherit_metadata"] lock_version = "4.5.0" -content_hash = "sha256:03868646e319d375fd26c4ce6ddfec81261a3f49e6e81b0ba5556af44a9eda74" +content_hash = "sha256:1f2d8bbfedf7884ef1fbad0f4235e92742914f7c1d01608ea01934a130cb64ce" [[metadata.targets]] requires_python = ">=3.9" @@ -1303,6 +1303,16 @@ files = [ {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"}, ] +[[package]] +name = "polib" +version = "1.2.0" +summary = "A library to manipulate gettext files (po and mo files)." +groups = ["default"] +files = [ + {file = "polib-1.2.0-py2.py3-none-any.whl", hash = "sha256:1c77ee1b81feb31df9bca258cbc58db1bbb32d10214b173882452c73af06d62d"}, + {file = "polib-1.2.0.tar.gz", hash = "sha256:f3ef94aefed6e183e342a8a269ae1fc4742ba193186ad76f175938621dbfc26b"}, +] + [[package]] name = "propcache" version = "0.2.0" diff --git a/pyproject.toml b/pyproject.toml index cb159055..79e83519 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ dependencies = [ "requests>=2.31.0", "rich>=13.7.1", "click>=8.1.7", + "polib>=1.2.0", ] [project.optional-dependencies]