Skip to content

Commit

Permalink
add i18n demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Jul 12, 2024
1 parent b18533d commit 34ca30f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/gui/i18n_demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
i18n demo
=====

Please **Open this forder** in MaixVision to run, the `locales` dir is important.

## Usage

1. Coding like `main.py`, use `tr` to translate strings.
2. Execute command `maixtool i18n -d . -r` in this directory, this command will scan all source code and find all strings you want to translate and generate translation files in `locales` dir, like `en.yaml` or `zh.yaml`, the locale name can be found in [here](https://www.science.co.il/language/Locale-codes.php) or [wikipedia](https://en.wikipedia.org/wiki/Language_localisation), all letters use lower case.
3. Translate yaml files mannually.
4. Run this project.


1 change: 1 addition & 0 deletions examples/gui/i18n_demo/locales/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello: hello
1 change: 1 addition & 0 deletions examples/gui/i18n_demo/locales/zh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello: 你好
37 changes: 37 additions & 0 deletions examples/gui/i18n_demo/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'''
Please read README.md of this dir fist!!!!!!!
'''

from maix import i18n, image, display, app, time, err

trans = i18n.Trans()
tr = trans.tr

# load translations from translation files
e = trans.load("locales")
err.check_raise(e, "load translation yamls failed")

# load Chinese font
image.load_font("sourcehansans", "/maixapp/share/font/SourceHanSansCN-Regular.otf", size = 32)
image.set_default_font("sourcehansans")

disp = display.Display()

img = image.Image(disp.width(), disp.height())

# draw hello with system setting language
print("system locale setting:", trans.get_locale())
img.draw_string(2, 2, tr("hello"), image.Color.from_rgb(255, 0, 0))

# manually set locale to zh
trans.set_locale("zh")
img.draw_string(2, 40, tr("hello"), image.Color.from_rgb(255, 0, 0))

# manually set locale to en
trans.set_locale("en")
img.draw_string(2, 80, tr("hello"), image.Color.from_rgb(255, 0, 0))

disp.show(img)
while not app.need_exit():
time.sleep(1)

0 comments on commit 34ca30f

Please sign in to comment.