From 2502a783cb5b54c1179d7c1f60dc4283f923b98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=8F=A1=E8=8A=B1=E6=B0=B4=E6=9C=88?= Date: Tue, 8 Jul 2025 00:06:28 -0300 Subject: [PATCH] Add BCP47 unicode tags for Locale enum Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com> --- discord/enums.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/discord/enums.py b/discord/enums.py index 71f755c12..acc780120 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from __future__ import annotations import types @@ -694,6 +695,42 @@ class MFALevel(Enum, comparable=True): require_2fa = 1 +_UNICODE_LANG_MAP: Dict[str, str] = { + 'bg': 'bg-BG', + 'zh-CN': 'zh-CN', + 'zh-TW': 'zh-TW', + 'hr': 'hr-HR', + 'cs': 'cs-CZ', + 'da': 'da-DK', + 'nl': 'nl-NL', + 'en-US': 'en-US', + 'en-GB': 'en-GB', + 'fi': 'fi-FI', + 'fr': 'fr-FR', + 'de': 'de-DE', + 'el': 'el-GR', + 'hi': 'hi-IN', + 'hu': 'hu-HU', + 'id': 'id-ID', + 'it': 'it-IT', + 'ja': 'ja-JP', + 'ko': 'ko-KR', + 'lt': 'lt-LT', + 'no': 'no-NO', + 'pl': 'pl-PL', + 'pt-BR': 'pt-BR', + 'ro': 'ro-RO', + 'ru': 'ru-RU', + 'es-ES': 'es-ES', + 'es-419': 'es-419', + 'sv-SE': 'sv-SE', + 'th': 'th-TH', + 'tr': 'tr-TR', + 'uk': 'uk-UA', + 'vi': 'vi-VN', +} + + class Locale(Enum): american_english = 'en-US' british_english = 'en-GB' @@ -731,6 +768,17 @@ class Locale(Enum): def __str__(self) -> str: return self.value + @property + def language_code(self) -> str: + """:class:`str`: Returns the locale's BCP 47 language code in the format of ``language-COUNTRY``. + + This is derived from a predefined mapping based on Discord's supported locales. + If no mapping exists for the current locale, this returns the raw locale value as a fallback. + + .. versionadded:: 2.6 + """ + return _UNICODE_LANG_MAP.get(self.value, self.value) + E = TypeVar('E', bound='Enum')