Skip to content

Commit

Permalink
feat(i18n): enhance internationalization with polib and zh support
Browse files Browse the repository at this point in the history
  • Loading branch information
HsiangNianian committed Nov 9, 2024
1 parent a8957cd commit 5e560fb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 10 deletions.
12 changes: 8 additions & 4 deletions iamai/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
cli(*args)
46 changes: 42 additions & 4 deletions iamai/i18n.py
Original file line number Diff line number Diff line change
@@ -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"))
22 changes: 22 additions & 0 deletions iamai/locale/zh/LC_MESSAGES/i18n.po
Original file line number Diff line number Diff line change
@@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: zh <[email protected]>\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}"
6 changes: 5 additions & 1 deletion iamai/locale/zh/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ msgid "Installing plugin: {plugin_name}"
msgstr "正在安装插件:{plugin_name}"

msgid "Current version: {version}"
msgstr "当前版本:{version}"
msgstr "当前版本:{version}"


msgid "hello {name}"
msgstr "你好 {name}"
12 changes: 11 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies = [
"requests>=2.31.0",
"rich>=13.7.1",
"click>=8.1.7",
"polib>=1.2.0",
]

[project.optional-dependencies]
Expand Down

0 comments on commit 5e560fb

Please sign in to comment.