Skip to content

Commit 83aea33

Browse files
committed
Rebuild lang:(var<load_mode> refactored to class:enum.Enum<LoadMode>),
_errors:class<LangDictSearchError> no longer inherits from class<enum.Enum>; Fixed lang:(The imported language dictionary files will be created automatically if the file does not exist, and the file lock is enabled. Unable to parse full form expression. When arg<None> is passed in, conversion language setting cannot be set to the universal word in method<default_lang> & method<str> of class<Lang>), _errors:property<word> of class<LangDictSearchError> return itself; Update README.md; New doc,examples
1 parent 4eeb4ee commit 83aea33

11 files changed

+303
-31
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
# OOPMultilang
22

3-
A Python package that supports multi-language conversion of the object-oriented paradigm.
3+
[![PyPI Latest Release](https://img.shields.io/pypi/v/oopmultilang.svg)](https://pypi.org/project/oopmultilang/)
4+
[![Package Status](https://img.shields.io/pypi/status/oopmultilang.svg)](https://pypi.org/project/oopmultilang/)
5+
[![License](https://img.shields.io/pypi/l/oopmultilang.svg)](https://github.com/leoweyr/Python-OOPMultilang/blob/main/LICENSE)
6+
[![Downloads](https://static.pepy.tech/personalized-badge/oopmultilang?period=total&units=international_system&left_color=grey&right_color=green&left_text=pypi%20downloads)](https://pepy.tech/project/oopmultilang)
7+
8+
A Python package that supports multi-language conversion of the object-oriented paradigm.
9+
10+
## 💿Obtainment
11+
12+
The source code is currently hosted on Github at: https://github.com/leoweyr/Python-OOPMultilang
13+
14+
Binary installers for the latest released version are available at the [Python Package Index (PyPI)](https://pypi.org/project/oopmultilang/).
15+
16+
```sh
17+
# PyPI
18+
pip install oopmultilang
19+
```
20+
21+
## 🔗Dependencies
22+
23+
- [Easierfile - An easier-to-use Python package that object-oriented encapsulates Python traditional built-in file operations](https://github.com/leoweyr/Python-Easierfile)
24+
25+
## ⚖️License
26+
27+
[MIT](https://github.com/leoweyr/Python-OOPMultilang/blob/main/LICENSE)
28+
29+
## 📗Documentation
30+
31+
The documentation is hosted on Github repository: https://github.com/leoweyr/Python-OOPMultilang/blob/main/doc

doc/API reference.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
## LoadMode
2+
3+
An enumeration representing two loading modes.
4+
5+
| **Enum** | Description |
6+
| -------- | ------------------------------------------------------------ |
7+
| REALTIME | Static data files are read for each conversion. |
8+
| ONETIME | Static data files are only read once when the object is instantiated for the first time. |
9+
10+
11+
12+
## Lang
13+
14+
A working class for language conversion.
15+
16+
### Constructor
17+
18+
```python
19+
Lang(include, load_mode)
20+
```
21+
22+
| Parameter | Type | Default | Description |
23+
| --------- | -------- | ----------------- | ------------------------------------------------------------ |
24+
| include | tuple | () | A tuple of expressions which are importing language dictionaries.<br>There are two forms of this expression, for example: <br>1. full: `"./en_us.lang as en_us"`<br>2. short: `"./en_us.lang"`<br>Note that when using the short form, the name of the associated language will be the name of the file. |
25+
| load_mode | LoadMode | LoadMode.REALTIME | The option for loading mode. |
26+
27+
> The detail of the data format supported by imported language dictionaries is in [doc](https://github.com/leoweyr/Python-OOPMultilang/blob/main/doc/data%20standard.md#supportdataformat).
28+
29+
### Default setting
30+
31+
```
32+
Lang().default_lang(source_lang, target_lang)
33+
```
34+
35+
| Parameter | Type | Default | Description |
36+
| ----------- | ------ | ------- | ----------------------------------------------------------- |
37+
| source_lang | string | "" | The option for the default source language to convert from. |
38+
| target_lang | string | "" | The option for the default target language to convert to. |
39+
40+
> `None` represents it is the universal word, not a specific language.
41+
42+
### Conversion
43+
44+
> The description of the universal word and the native word is in [doc](https://github.com/leoweyr/Python-OOPMultilang/blob/main/doc/data%20standard.md).
45+
46+
#### Lang().str(word, source_lang, target_lang)
47+
48+
String conversion method.
49+
50+
| Parameter | Type | Default | Description |
51+
| ----------- | ------ | ------- | ------------------------------------ |
52+
| word | string | | A word to be converted. |
53+
| source_lang | string | "" | The source language to convert from. |
54+
| target_lang | string | "" | The target language to convert to. |
55+
56+
> `""` represents it is the default language set in `Lang().default_lang()`.
57+
58+
#### Lang().universal
59+
60+
Object-oriented paradigm conversion method, only supports converting a universal word into other native word.
61+
62+
There are two syntax statements:
63+
64+
```python
65+
# Pure object-oriented paradigm suffix.
66+
Lang().universal.*
67+
68+
# End of suffix with the target language to convert to.
69+
Lang().universal.*.target_lang
70+
71+
# `*` represents a specific universal word.
72+
```
73+
74+
> For the difference between two methods in practical application, please see [example](https://github.com/leoweyr/Python-OOPMultilang/tree/main/examples).
75+
76+
77+
78+
## LangDictSearchError
79+
80+
An exception class thrown when language conversion error occurs.
81+
82+
### Attributes
83+
84+
<table>
85+
<tr>
86+
<td><code>LangDictSearchError().type</code></td>
87+
<td>Return the type of error reason.</br>● LangDictSearchError.UNIVERSAL_WORD_EXIST_ERROR</br>● LangDictSearchError.SOURCE_LANG_DICT_MATCH_ERROR</br>● LangDictSearchError.TARGET_LANG_DICT_MATCH_ERROR</td>
88+
</tr>
89+
<tr>
90+
<td><code>LangDictSearchError().word</code></td>
91+
<td>Return the word being converted that caused the error to occur.</td>
92+
</tr>
93+
<tr>
94+
<td><code>LangDictSearchError().position</code></td>
95+
<td>Return the name of language associated with language dictionary which caused the error to occur.</td>
96+
</tr>
97+
</table>

doc/data standard.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
The data structure of each language dictionary is uniformly composed of pairs, each consisting of a universal word and its corresponding native word.
2+
3+
<table>
4+
<tr>
5+
<th>Universal word</th>
6+
<td>Similar to the object-oriented usage, generally expressed in English.</td>
7+
</tr>
8+
<tr>
9+
<th>Native word</th>
10+
<td>Native word of the language associated with the lang dict.</td>
11+
</tr>
12+
</table>
13+
14+
## Conversion search principle
15+
16+
```mermaid
17+
graph TB
18+
NO1["Input native word: search for the universal word in source language dictionary"] --> NO2["Intermediate universal word: search for the native word in target language dictionary"] --> NO3["Output native word"]
19+
```
20+
21+
> [class:Exception\<LangDictSearchError>](https://github.com/leoweyr/Python-OOPMultilang/blob/main/doc/API%20reference.md#LangDictSearchError) will be thrown when the search process goes error.
22+
23+
## Support data format
24+
25+
The following are the supported file types for storing language dictionary data:
26+
27+
| File type | State |
28+
| --------- | ----- |
29+
| lang | done |
30+
| json | to do |
31+
| ... | ... |
32+
33+
### .lang
34+
35+
Commonly found in language pack files of Microsoft software.
36+
37+
The characteristic of this file type is that each line is an independent key-value pair-like, as the key and value are connected by an equal sign, and `#` is the comment symbol.
38+
39+
For example:
40+
41+
```lang
42+
UI.login = user login # It is a button, and this is a comment.
43+
UI.register = user register
44+
```
45+

examples/lang/en_us.lang

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
UI.login = user login # It is a button, and this is a comment.
2+
UI.register = user register

examples/lang/zh_cn.lang

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
UI.login = 用户登录 # 它是一个按钮,而这里是注释
2+
UI.register = 用户注册

examples/language_conversion.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import oopmultilang
2+
3+
4+
"""
5+
Define a class<Lang> instance object dedicated to ui function description in the global.
6+
7+
In the argument passed in param<include>:
8+
9+
1. ``"./lang/en_us.lang as en_us"``: Expression using the full-form kind.
10+
2. ``"./lang/zh_cn.lang"``: Expression using the short-form kind. The file name "zh_cn" will be used as the name of the
11+
language associated with this language dictionary.
12+
"""
13+
ui_alt_lang = oopmultilang.Lang(
14+
include=(
15+
"./lang/en_us.lang as en_us",
16+
"./lang/zh_cn.lang"
17+
),
18+
load_mode=oopmultilang.LoadMode.REALTIME
19+
)
20+
21+
22+
def main():
23+
"""
24+
Use the object-oriented paradigm conversion method to obtain the "en_us" native word whose universal word is
25+
"UI.login".
26+
"""
27+
ui_login_alt_english = ui_alt_lang.universal.UI.login.en_us # user login
28+
29+
"""
30+
First set the default target language to "en_us", then use the string conversion method to obtain the "en_us" native
31+
word whose universal word is "UI.login".
32+
"""
33+
ui_alt_lang.default_lang(target_lang="en_us")
34+
ui_login_alt_english = ui_alt_lang.universal.UI.login # user login
35+
36+
"""
37+
Use the string conversion method to obtain the "zh_cn" native word whose "en_us" native word is "user login".
38+
"""
39+
ui_login_alt_chinese = ui_alt_lang.str("user login", "en_us", "zh_cn") # 用户登录
40+
41+
"""
42+
First set the default source language to "en_us", and the default target language to "the universal word".
43+
And then use the string conversion method obtain the universal word whose "en_us" native word is "user login".
44+
Finally, use the built-in eval function to execute the statement using the object-oriented paradigm conversion
45+
method to obtain the "zh_cn" native word whose universal word is "UI.login".
46+
"""
47+
ui_alt_lang.default_lang(source_lang="en_us", target_lang=None)
48+
ui_login_alt_chinese = eval("lang_obj.universal.{}.zh_cn".format(ui_alt_lang.str("user login"))) # UI.login -> 用户登录
49+
50+
"""
51+
Try to use the string conversion method to obtain the "en_us" native word whose universal word is non-existent
52+
"UI.title", catch the exception and print out the word being converted that caused the error to occur.
53+
"""
54+
try:
55+
ui_title_alt_english = ui_alt_lang.str("UI.title", None, "en_us")
56+
except oopmultilang.LangDictSearchError as error:
57+
if error.type == oopmultilang.LangDictSearchError.UNIVERSAL_WORD_EXIST_ERROR:
58+
print(f"{error.word} is not a universal word.")
59+
60+
"""
61+
Try to use the string conversion method to obtain the "en_us" native word whose "zh_cn" native word is non-existent
62+
"界面标题", catch the exception and print out message that the word does not exist in the corresponding language
63+
dictionary.
64+
"""
65+
try:
66+
ui_title_alt_english = ui_alt_lang.str("界面标题", "zh_cn", "en_us")
67+
except oopmultilang.LangDictSearchError as error:
68+
print(f"{error.word} is not in {error.position} dictionary.")
69+
70+
71+
if __name__ == "__main__":
72+
main()

oopmultilang/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from .lang import (
2+
LoadMode,
23
Lang,
3-
load_mode,
44
LangDictSearchError
55
)
66

7-
__all__ = ["Lang", "load_mode", "LangDictSearchError"]
7+
8+
__all__ = ["LoadMode", "Lang", "LangDictSearchError"]

oopmultilang/_errors.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import inspect
22
import os
33
import sys
4-
from enum import Enum
54

65

76
class ExpressionError(Exception):
@@ -27,7 +26,7 @@ def __str__(self):
2726
return error_message
2827

2928

30-
class LangDictSearchError(Exception, Enum):
29+
class LangDictSearchError(Exception):
3130
# Error type.
3231
UNIVERSAL_WORD_EXIST_ERROR = 1
3332
SOURCE_LANG_DICT_MATCH_ERROR = 2
@@ -39,12 +38,12 @@ def __init__(self, error_type, error_word, source_lang, target_lang):
3938
self.__m_error_position = None
4039
self.__m_message = ""
4140

42-
# Different error conditions.
41+
# Different error situations.
4342
if self.__m_error_type == LangDictSearchError.UNIVERSAL_WORD_EXIST_ERROR:
44-
self.__m_message = f"'{self.__m_error_word}' is not the universal word in language dictionaries"
43+
self.__m_message = f"'{self.__m_error_word}' is not a universal word in language dictionaries"
4544
elif self.__m_error_type == LangDictSearchError.SOURCE_LANG_DICT_MATCH_ERROR:
4645
self.__m_error_position = source_lang
47-
self.__m_message = f"'{self.__m_error_word}' is not the native word in '{self.__m_error_position}' dictionary"
46+
self.__m_message = f"'{self.__m_error_word}' is not a native word in '{self.__m_error_position}' dictionary"
4847
elif self.__m_error_type == LangDictSearchError.TARGET_LANG_DICT_MATCH_ERROR:
4948
self.__m_error_position = target_lang
5049
self.__m_message = f"'{self.__m_error_word}' can not be converted to the native word in '{self.__m_error_position}' dictionary"
@@ -58,7 +57,7 @@ def type(self):
5857

5958
@property
6059
def word(self):
61-
return self.word
60+
return self.__m_error_word
6261

6362
@property
6463
def position(self):

oopmultilang/_lang_dict_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
class LangDictParser:
22
"""
3-
Each lang dict data structure is uniformly composed of universal word and native word in pairs.
3+
The data structure of each language dictionary is uniformly composed of pairs, each consisting of a universal word
4+
and its corresponding native word.
45
56
- ``universal word``: Similar to the object-oriented usage, generally expressed in English.
67
- ``native word``: Native word of the language associated with the lang dict.
78
"""
9+
810
@classmethod
911
def loads(cls, lang_dict):
1012
pass

0 commit comments

Comments
 (0)