-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Author: Hikari16665 Co-authored-by: awaTsuki
- Loading branch information
Hikari
committed
Oct 18, 2024
1 parent
50f0754
commit c5acdc6
Showing
14 changed files
with
525 additions
and
183 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
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 |
---|---|---|
|
@@ -23,4 +23,6 @@ hsl-config.json | |
*.build/ | ||
*.dist/ | ||
nuitka-crash-report.xml | ||
*.bin | ||
*.bin | ||
TODO | ||
Dockerfile |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"version": 16, | ||
"minor": 5 | ||
"version": 17, | ||
"minor": 0 | ||
} |
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
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
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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
class NoSuchServerException(Exception): | ||
pass | ||
class LanguageNotSupportedException(Exception): | ||
pass |
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,45 @@ | ||
# HSL Locale Module | ||
from typing import Any, Union | ||
from hsl.core.exceptions import LanguageNotSupportedException | ||
import os | ||
import sys | ||
import json | ||
from rich.console import Console | ||
from hsl.core.config import Config | ||
console = Console() | ||
class Locale(): | ||
def __init__(self): | ||
self.config = Config().load() | ||
self.language = self.config.language | ||
self.debug = self.config.debug | ||
self.language_key = {} | ||
self.set_language(self.language) | ||
def set_language(self, language: str) -> None: | ||
self.language = language | ||
self.packaged = bool(getattr(sys, '_MEIPASS', False)) | ||
base_path = sys._MEIPASS if self.packaged else os.path.abspath(".") # type: ignore | ||
file_path = os.path.join(base_path, 'lang', f'{self.language}.json') | ||
if self.debug: | ||
console.log(f'Loading language file {file_path}') | ||
try: | ||
with open(file_path, 'r', encoding='utf-8') as f: | ||
self.language_key = json.load(f) | ||
except FileNotFoundError as e: | ||
raise LanguageNotSupportedException( | ||
f'Language {self.language} is not supported.' | ||
) from e | ||
def replace(self, text: str, replaces: dict[str, str] = {}) -> str: | ||
for k, v in replaces.items(): | ||
text = text.replace(f'{{{k}}}', v) | ||
return text | ||
def trans_key(self, key: Union[str, list[str]], /, **replaces: str) -> Any: # type: ignore | ||
if isinstance(key, str): | ||
#console.log(f'Translating key {key} in {self.language} replaces: {replaces}') | ||
return self.replace(self.language_key.get(key, key), replaces) | ||
if isinstance(key, list): | ||
_texts = [] | ||
for k in key: | ||
#console.log(f'Translating key {k} in {self.language} replaces: {replaces}') | ||
_texts.append(self.language_key.get(k, k)) | ||
return [self.replace(text, replaces) for text in _texts] | ||
return None |
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
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
SPONSOR = [ | ||
"Hikari<[email protected]>", | ||
"awatsuki", | ||
"BenXong" | ||
"BenXong", | ||
"Graphite_sm" | ||
] | ||
def get_sponsor_list() -> list: | ||
return SPONSOR |
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 |
---|---|---|
@@ -1,17 +1,19 @@ | ||
from noneprompt import ListPrompt, InputPrompt, Choice | ||
from hsl.core.locale import Locale | ||
import asyncio | ||
OPTIONS_YN = ['是', '否'] | ||
locale = Locale() | ||
OPTIONS_YN = locale.trans_key(['yes','no']) | ||
async def promptSelect(options: list,prompt: str) -> int: | ||
choices = [Choice(options[i],data=i) for i in range(len(options))] | ||
prompt_task = asyncio.create_task(ListPrompt(prompt, choices=choices).prompt_async()) | ||
prompt_task = asyncio.create_task(ListPrompt(prompt, choices=choices,annotation=locale.trans_key('choice-prompt-annotation')).prompt_async()) | ||
select = await asyncio.gather(prompt_task) | ||
return select[0].data | ||
async def promptInput(prompt: str) -> str: | ||
prompt_task = asyncio.create_task(InputPrompt(prompt, validator=lambda string: True).prompt_async()) | ||
input = await asyncio.gather(prompt_task) | ||
return input[0] | ||
_input = await asyncio.gather(prompt_task) | ||
return _input[0] | ||
async def promptConfirm(prompt: str) -> bool: | ||
# prompt_task = asyncio.create_task(ConfirmPrompt(prompt,default_choice=False).prompt_async()) | ||
# confirm = await asyncio.gather(prompt_task) | ||
# return confirm[0] | ||
return True if await promptSelect(OPTIONS_YN,prompt) == 0 else False | ||
return bool(await promptSelect(OPTIONS_YN,prompt) == 0) |
Oops, something went wrong.