Skip to content

Commit 91fbc93

Browse files
fujicoinmatejcik
authored andcommitted
Undo list_languages and detect_language
1 parent f098bf1 commit 91fbc93

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

mnemonic/mnemonic.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def __init__(self, language: str):
8282
def _get_directory() -> str:
8383
return os.path.join(os.path.dirname(__file__), "wordlist")
8484

85+
@classmethod
86+
def list_languages(cls) -> List[str]:
87+
return [
88+
f.split(".")[0]
89+
for f in os.listdir(cls._get_directory())
90+
if f.endswith(".txt")
91+
]
92+
8593
@staticmethod
8694
def normalize_string(txt: AnyStr) -> str:
8795
if isinstance(txt, bytes):
@@ -93,6 +101,19 @@ def normalize_string(txt: AnyStr) -> str:
93101

94102
return unicodedata.normalize("NFKD", utxt)
95103

104+
@classmethod
105+
def detect_language(cls, code: str) -> str:
106+
code = cls.normalize_string(code)
107+
first = code.split(" ")[0]
108+
languages = cls.list_languages()
109+
110+
for lang in languages:
111+
mnemo = cls(lang)
112+
if first in mnemo.wordlist:
113+
return lang
114+
115+
raise ConfigurationError("Language not detected")
116+
96117
def generate(self, strength: int = 128) -> str:
97118
if strength not in [128, 160, 192, 224, 256]:
98119
raise ValueError(

0 commit comments

Comments
 (0)