-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): enhance internationalization with polib and zh support
- Loading branch information
1 parent
a8957cd
commit 5e560fb
Showing
6 changed files
with
89 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters