Skip to content

Commit ecec750

Browse files
committed
Fixed LangSwitcher:When the tuple has only one element, parse it as a single string.
1 parent 24ceba4 commit ecec750

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

oopmultilang/lang_switcher.py

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,49 +56,55 @@ def __init__(self, include=(), load_mode=LoadMode.REALTIME):
5656
self.__m_default_source_lang = None
5757
self.__m_default_target_lang = None
5858

59-
if len(include) != 0:
59+
# Avoid parsing a tuple with only one element as a single string.
60+
if isinstance(include, tuple):
6061
for expression in include:
61-
"""
62-
Parses expressions imported into language dictionaries via param<include>.
62+
self.__parse_expression(expression)
63+
else:
64+
self.__parse_expression(include)
6365

64-
There are two forms of this expression, one is full and the other is short.
66+
# One-time loading mode.
67+
if self._m_load_mode == LoadMode.ONETIME:
68+
self.__load_lang_dict()
6569

66-
For example:
70+
def __parse_expression(self,expression):
71+
"""
72+
Parses expressions imported into language dictionaries via param<include>.
73+
74+
There are two forms of this expression, one is full and the other is short.
6775
68-
1. ``full``: './en_us.lang as en_us'
69-
2. ``short``: './en_us.lang'
76+
For example:
7077
71-
Note that when using the short form, the name of the associated language will be the name of the file.
78+
1. ``full``: './en_us.lang as en_us'
79+
2. ``short``: './en_us.lang'
80+
81+
Note that when using the short form, the name of the associated language will be the name of the file.
82+
"""
83+
if expression.find("as") != -1:
84+
if expression.count("as") == 1:
7285
"""
73-
if expression.find("as") != -1:
74-
if expression.count("as") == 1:
75-
"""
76-
A list of strings representing the distinct terms of the expression after it is parsed.
77-
78-
- ``expression_parsing[0]``: The file path of the language dictionary.
79-
- ``expression_parsing[1]``: The associated language of the language dictionary.
80-
"""
81-
expression_parsing = [expression_unit.strip() for expression_unit in expression.split("as")]
82-
dictionary_file = File(expression_parsing[0], False, False)
83-
dictionary_associated_lang = expression_parsing[1]
84-
else:
85-
raise ExpressionError(expression)
86-
else:
87-
dictionary_file = File(expression, False, False)
88-
dictionary_associated_lang = dictionary_file.info["name"]
86+
A list of strings representing the distinct terms of the expression after it is parsed.
8987
90-
# Store language dictionary file information for different languages into member:private<self.__m_dict>.
91-
if not dictionary_file.state["exist"]:
92-
raise FileNotFoundError("Language dictionary not found: " + dictionary_file.info["path"])
93-
else:
94-
if dictionary_associated_lang in self.__m_dict.keys():
95-
self.__m_dict[dictionary_associated_lang].append(dictionary_file.info["path"])
96-
else:
97-
self.__m_dict[dictionary_associated_lang] = [dictionary_file.info["path"]]
88+
- ``expression_parsing[0]``: The file path of the language dictionary.
89+
- ``expression_parsing[1]``: The associated language of the language dictionary.
90+
"""
91+
expression_parsing = [expression_unit.strip() for expression_unit in expression.split("as")]
92+
dictionary_file = File(expression_parsing[0], False, False)
93+
dictionary_associated_lang = expression_parsing[1]
94+
else:
95+
raise ExpressionError(expression)
96+
else:
97+
dictionary_file = File(expression, False, False)
98+
dictionary_associated_lang = dictionary_file.info["name"]
9899

99-
# One-time loading mode.
100-
if self._m_load_mode == LoadMode.ONETIME:
101-
self.__load_lang_dict()
100+
# Store language dictionary file information for different languages into member:private<self.__m_dict>.
101+
if not dictionary_file.state["exist"]:
102+
raise FileNotFoundError("Language dictionary not found: " + dictionary_file.info["path"])
103+
else:
104+
if dictionary_associated_lang in self.__m_dict.keys():
105+
self.__m_dict[dictionary_associated_lang].append(dictionary_file.info["path"])
106+
else:
107+
self.__m_dict[dictionary_associated_lang] = [dictionary_file.info["path"]]
102108

103109
def __load_lang_dict(self):
104110
new_dict = self.__m_dict.copy()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "oopmultilang"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
description = "A Python package that supports multi-language conversion of the object-oriented paradigm."
55
license = "MIT"
66
authors = ["leoweyr <leoweyr@foxmail.com>"]

0 commit comments

Comments
 (0)