-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 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
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. | ||
|
||
|
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 @@ | ||
hello: hello |
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 @@ | ||
hello: 你好 |
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,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) | ||
|