2023-05-11 22:55:43 +03:00
|
|
|
from os import listdir
|
|
|
|
from pathlib import Path
|
2023-06-30 11:31:49 +03:00
|
|
|
from typing import Any, Dict, Union
|
2023-05-11 22:55:43 +03:00
|
|
|
|
2023-08-06 13:42:37 +03:00
|
|
|
import libbot
|
|
|
|
from libbot.i18n import sync
|
2023-06-20 13:30:27 +03:00
|
|
|
from libbot.i18n.classes.bot_locale import BotLocale
|
2023-05-11 22:55:43 +03:00
|
|
|
|
|
|
|
|
2023-06-30 11:31:49 +03:00
|
|
|
async def _(
|
|
|
|
key: str,
|
|
|
|
*args: str,
|
2023-08-06 13:50:34 +03:00
|
|
|
locale: Union[str, None] = "en",
|
2023-06-30 11:31:49 +03:00
|
|
|
locales_root: Union[str, Path] = Path("locale"),
|
|
|
|
) -> Any:
|
2023-05-11 22:55:43 +03:00
|
|
|
"""Get value of locale string
|
|
|
|
|
|
|
|
### Args:
|
2023-06-30 11:31:49 +03:00
|
|
|
* key (`str`): The last key of the locale's keys path.
|
|
|
|
* *args (`list`): Path to key like: `dict[args][key]`.
|
2023-08-06 13:50:34 +03:00
|
|
|
* locale (`Union[str, None]`): Locale to looked up in. Defaults to `"en"`.
|
2023-06-30 11:31:49 +03:00
|
|
|
* locales_root (`Union[str, Path]`, *optional*): Folder where locales are located. Defaults to `Path("locale")`.
|
2023-05-11 22:55:43 +03:00
|
|
|
|
|
|
|
### Returns:
|
|
|
|
* `Any`: Value of provided locale key. Is usually `str`, `dict` or `list`
|
|
|
|
"""
|
2023-08-06 13:42:37 +03:00
|
|
|
locale = libbot.sync.config_get("locale") if locale is None else locale
|
2023-05-11 22:55:43 +03:00
|
|
|
|
|
|
|
try:
|
2023-08-06 13:42:37 +03:00
|
|
|
this_dict = await libbot.json_read(Path(f"{locales_root}/{locale}.json"))
|
2023-05-11 22:55:43 +03:00
|
|
|
except FileNotFoundError:
|
|
|
|
try:
|
2023-08-06 13:42:37 +03:00
|
|
|
this_dict = await libbot.json_read(
|
|
|
|
Path(f'{locales_root}/{await libbot.config_get("locale")}.json')
|
2023-05-11 22:55:43 +03:00
|
|
|
)
|
|
|
|
except FileNotFoundError:
|
2023-05-26 14:02:09 +03:00
|
|
|
return f'⚠️ Locale in config is invalid: could not get "{key}" in {args} from locale "{locale}"'
|
2023-05-11 22:55:43 +03:00
|
|
|
|
|
|
|
this_key = this_dict
|
|
|
|
for dict_key in args:
|
|
|
|
this_key = this_key[dict_key]
|
|
|
|
|
|
|
|
try:
|
|
|
|
return this_key[key]
|
|
|
|
except KeyError:
|
2023-05-26 14:02:09 +03:00
|
|
|
return f'⚠️ Locale in config is invalid: could not get "{key}" in {args} from locale "{locale}"'
|
2023-05-11 22:55:43 +03:00
|
|
|
|
|
|
|
|
2023-06-30 11:31:49 +03:00
|
|
|
async def in_all_locales(
|
|
|
|
key: str, *args: str, locales_root: Union[str, Path] = Path("locale")
|
|
|
|
) -> list:
|
2023-05-11 22:55:43 +03:00
|
|
|
"""Get value of the provided key and path in all available locales
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* key (`str`): The last key of the locale's keys path.
|
|
|
|
* *args (`list`): Path to key like: `dict[args][key]`.
|
2023-06-30 11:31:49 +03:00
|
|
|
* locales_root (`Union[str, Path]`, *optional*): Folder where locales are located. Defaults to `Path("locale")`.
|
2023-05-11 22:55:43 +03:00
|
|
|
|
|
|
|
### Returns:
|
|
|
|
* `list`: List of values in all locales
|
|
|
|
"""
|
|
|
|
|
|
|
|
output = []
|
2023-06-30 11:31:49 +03:00
|
|
|
files_locales = listdir(locales_root)
|
2023-05-11 22:55:43 +03:00
|
|
|
|
2023-05-26 14:02:09 +03:00
|
|
|
valid_locales = [".".join(entry.split(".")[:-1]) for entry in files_locales]
|
2023-05-11 22:55:43 +03:00
|
|
|
for lc in valid_locales:
|
|
|
|
try:
|
2023-08-06 13:42:37 +03:00
|
|
|
this_dict = await libbot.json_read(Path(f"{locales_root}/{lc}.json"))
|
2023-05-11 22:55:43 +03:00
|
|
|
except FileNotFoundError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
this_key = this_dict
|
|
|
|
for dict_key in args:
|
|
|
|
this_key = this_key[dict_key]
|
|
|
|
|
|
|
|
try:
|
|
|
|
output.append(this_key[key])
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return output
|
2023-05-26 16:52:10 +03:00
|
|
|
|
|
|
|
|
2023-06-30 11:31:49 +03:00
|
|
|
async def in_every_locale(
|
|
|
|
key: str, *args: str, locales_root: Union[str, Path] = Path("locale")
|
|
|
|
) -> Dict[str, Any]:
|
2023-05-26 16:52:10 +03:00
|
|
|
"""Get value of the provided key and path in every available locale with locale tag
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* key (`str`): The last key of the locale's keys path.
|
|
|
|
* *args (`list`): Path to key like: `dict[args][key]`.
|
2023-06-30 11:31:49 +03:00
|
|
|
* locales_root (`Union[str, Path]`, *optional*): Folder where locales are located. Defaults to `Path("locale")`.
|
2023-05-26 16:52:10 +03:00
|
|
|
|
|
|
|
### Returns:
|
|
|
|
* `Dict[str, Any]`: Locale is a key and it's value from locale file is a value
|
|
|
|
"""
|
|
|
|
|
|
|
|
output = {}
|
2023-06-30 11:31:49 +03:00
|
|
|
files_locales = listdir(locales_root)
|
2023-05-26 16:52:10 +03:00
|
|
|
|
|
|
|
valid_locales = [".".join(entry.split(".")[:-1]) for entry in files_locales]
|
|
|
|
for lc in valid_locales:
|
|
|
|
try:
|
2023-08-06 13:42:37 +03:00
|
|
|
this_dict = await libbot.json_read(Path(f"{locales_root}/{lc}.json"))
|
2023-05-26 16:52:10 +03:00
|
|
|
except FileNotFoundError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
this_key = this_dict
|
|
|
|
for dict_key in args:
|
|
|
|
this_key = this_key[dict_key]
|
|
|
|
|
|
|
|
try:
|
|
|
|
output[lc] = this_key[key]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return output
|