class Locale(QtCore.QLocale):
"""Converts between numbers and their string representations in various languages."""
def __repr__(self):
return get_repr(self, self.bcp47Name())
def __reduce__(self):
return type(self), (self.bcp47Name(),)
def get_flag_unicode(self):
name = self.name().split("_")[1]
name = [c for c in name.lower() if c.isalnum()]
return "".join([chr(ord(c.upper()) + OFFSET) for c in name])
@classmethod
def get_system_locale(cls) -> Self:
return cls(cls.system())
@classmethod
def get_c_locale(cls) -> Self:
return cls(cls.c())
@classmethod
def get_system_language(cls) -> str:
return cls.get_system_locale().uiLanguages()[0]
def get_country(self):
return COUNTRY.inverse[self.country()]
def get_measurement_system(self) -> MeasurementSystemStr:
return MEASUREMENT_SYSTEM.inverse[self.measurementSystem()]
def get_formatted_data_size(
self, size: int, precision: int = 2, fmt: DataSizeFormatStr = "iec"
) -> str:
if size < 0:
return ""
return self.formattedDataSize(size, precision, DATA_SIZE_FORMAT[fmt])
def get_first_day_of_week(self) -> constants.DayOfWeekStr:
return constants.DAY_OF_WEEK.inverse[self.firstDayOfWeek()]
def get_text_direction(self) -> constants.LayoutDirectionStr:
return constants.LAYOUT_DIRECTION.inverse[self.textDirection()]
def get_weekdays(self) -> list[constants.DayOfWeekStr]:
return [constants.DAY_OF_WEEK.inverse[i] for i in self.weekdays()]
def get_day_name(self, day: int, format_type: FormatTypeStr = "long") -> str:
return self.dayName(day, FORMAT_TYPE[format_type])
def get_month_name(self, month: int, format_type: FormatTypeStr = "long") -> str:
return self.monthName(month, FORMAT_TYPE[format_type])
def get_standalone_day_name(
self, day: int, format_type: FormatTypeStr = "long"
) -> str:
return self.standaloneDayName(day, FORMAT_TYPE[format_type])
def get_time_format(self, format_type: FormatTypeStr = "long") -> str:
return self.timeFormat(FORMAT_TYPE[format_type])
def get_date_format(self, format_type: FormatTypeStr = "long") -> str:
return self.dateFormat(FORMAT_TYPE[format_type])
def get_datetime_format(self, format_type: FormatTypeStr = "long") -> str:
return self.dateTimeFormat(FORMAT_TYPE[format_type])
def to_datetime(
self, text: str, format_type: FormatTypeStr | str = "long"
) -> QtCore.QDateTime:
if format_type in {"long", "short", "narrow"}:
format_type = FORMAT_TYPE[format_type]
return self.toDateTime(text, format_type)
def to_date(
self, text: str, format_type: FormatTypeStr | str = "long"
) -> QtCore.QDate:
if format_type in {"long", "short", "narrow"}:
format_type = FORMAT_TYPE[format_type]
return self.toDate(text, format_type)
def to_time(
self, text: str, format_type: FormatTypeStr | str = "long"
) -> QtCore.QTime:
if format_type in {"long", "short", "narrow"}:
format_type = FORMAT_TYPE[format_type]
return self.to_time(text, format_type)
def get_standalone_month_name(
self, month: int, format_type: FormatTypeStr = "long"
) -> str:
return self.standaloneMonthName(month, FORMAT_TYPE[format_type])
def quote_string(self, string: str, alternate_style: bool = False) -> str:
flag = (
self.QuotationStyle.AlternateQuotation
if alternate_style
else self.QuotationStyle.StandardQuotation
)
return self.quoteString(string, flag)
@classmethod
def get_all_locales(cls) -> list[Self]:
return [
cls(i)
for i in cls.matchingLocales(
cls.Language.AnyLanguage, cls.Script.AnyScript, cls.Country.AnyCountry
)
]