# Released under the MIT License. See LICENSE for details.#"""Functionality for wrangling locale info."""from__future__importannotationsfromenumimportEnumfromfunctoolsimportcached_propertyfromtypingimportTYPE_CHECKING,assert_neverifTYPE_CHECKING:pass
[docs]classLocale(Enum):"""A distinct grouping of language, cultural norms, etc. This list of locales is considered 'sacred' - we assume any values added here remain in use out in the wild indefinitely. If a locale is superseded by a newer or more specific one, the new one should be added and both new and old should map to the same LocaleResolved value. """# No strong reason to use iso codes or whatnot for these values, so# just using something short but easily recognizable in english.ENGLISH='eng'CHINESE_TRADITIONAL='chn_tr'CHINESE_SIMPLIFIED='chn_sim'PORTUGUESE_PORTUGAL='prtg_pr'PORTUGUESE_BRAZIL='prtg_brz'ARABIC='arabc'BELARUSSIAN='blrs'CROATIAN='croat'CZECH='czch'DANISH='dnsh'DUTCH='dtch'PIRATE_SPEAK='pirate'ESPERANTO='esprnto'FILIPINO='filp'FRENCH='frnch'GERMAN='grmn'GIBBERISH='gibber'GREEK='greek'HINDI='hndi'HUNGARIAN='hngr'INDONESIAN='indnsn'ITALIAN='italn'KOREAN='kor'MALAY='mlay'PERSIAN='pers'POLISH='pol'ROMANIAN='rom'RUSSIAN='rusn'SERBIAN='srbn'SPANISH_LATIN_AMERICA='spn_lat'SPANISH_SPAIN='spn_spn'SLOVAK='slvk'SWEDISH='swed'TAMIL='taml'THAI='thai'TURKISH='turk'UKRAINIAN='ukrn'VENETIAN='venetn'VIETNAMESE='viet'# Note: we cache these functions so we only have to traverse long# lists of if-statements once per value.
[docs]@cached_propertydefresolved(self)->LocaleResolved:"""Return the associated resolved locale."""# pylint: disable=too-many-return-statements# pylint: disable=too-many-branchescls=type(self)R=LocaleResolvedifselfiscls.ENGLISH:returnR.ENGLISHifselfiscls.CHINESE_TRADITIONAL:returnR.CHINESE_TRADITIONALifselfiscls.CHINESE_SIMPLIFIED:returnR.CHINESE_SIMPLIFIEDifselfiscls.PORTUGUESE_PORTUGAL:returnR.PORTUGUESE_PORTUGALifselfiscls.PORTUGUESE_BRAZIL:returnR.PORTUGUESE_BRAZILifselfiscls.ARABIC:returnR.ARABICifselfiscls.BELARUSSIAN:returnR.BELARUSSIANifselfiscls.CROATIAN:returnR.CROATIANifselfiscls.CZECH:returnR.CZECHifselfiscls.DANISH:returnR.DANISHifselfiscls.DUTCH:returnR.DUTCHifselfiscls.PIRATE_SPEAK:returnR.PIRATE_SPEAKifselfiscls.ESPERANTO:returnR.ESPERANTOifselfiscls.FILIPINO:returnR.FILIPINOifselfiscls.FRENCH:returnR.FRENCHifselfiscls.GERMAN:returnR.GERMANifselfiscls.GIBBERISH:returnR.GIBBERISHifselfiscls.GREEK:returnR.GREEKifselfiscls.HINDI:returnR.HINDIifselfiscls.HUNGARIAN:returnR.HUNGARIANifselfiscls.INDONESIAN:returnR.INDONESIANifselfiscls.ITALIAN:returnR.ITALIANifselfiscls.KOREAN:returnR.KOREANifselfiscls.MALAY:returnR.MALAYifselfiscls.PERSIAN:returnR.PERSIANifselfiscls.POLISH:returnR.POLISHifselfiscls.ROMANIAN:returnR.ROMANIANifselfiscls.RUSSIAN:returnR.RUSSIANifselfiscls.SERBIAN:returnR.SERBIANifselfiscls.SPANISH_LATIN_AMERICA:returnR.SPANISH_LATIN_AMERICAifselfiscls.SPANISH_SPAIN:returnR.SPANISH_SPAINifselfiscls.SLOVAK:returnR.SLOVAKifselfiscls.SWEDISH:returnR.SWEDISHifselfiscls.TAMIL:returnR.TAMILifselfiscls.THAI:returnR.THAIifselfiscls.TURKISH:returnR.TURKISHifselfiscls.UKRAINIAN:returnR.UKRAINIANifselfiscls.VENETIAN:returnR.VENETIANifselfiscls.VIETNAMESE:returnR.VIETNAMESE# Make sure we're covering all cases.assert_never(self)
[docs]classLocaleResolved(Enum):"""A resolved :class:``Locale``. Logic should always use these. These values should never be stored or transmitted and should always come from resolving a :class:``Locale``. This gives us the freedom to revise this list as needed to keep our actual list of implemented locales as trim as possible. """ENGLISH='eng'CHINESE_TRADITIONAL='chn_tr'CHINESE_SIMPLIFIED='chn_sim'PORTUGUESE_PORTUGAL='prtg_pr'PORTUGUESE_BRAZIL='prtg_brz'ARABIC='arabc'BELARUSSIAN='blrs'CROATIAN='croat'CZECH='czch'DANISH='dnsh'DUTCH='dtch'PIRATE_SPEAK='pirate'ESPERANTO='esprnto'FILIPINO='filp'FRENCH='frnch'GERMAN='grmn'GIBBERISH='gibber'GREEK='greek'HINDI='hndi'HUNGARIAN='hngr'INDONESIAN='indnsn'ITALIAN='italn'KOREAN='kor'MALAY='mlay'PERSIAN='pers'POLISH='pol'ROMANIAN='rom'RUSSIAN='rusn'SERBIAN='srbn'SPANISH_LATIN_AMERICA='spn_lat'SPANISH_SPAIN='spn_spn'SLOVAK='slvk'SWEDISH='swed'TAMIL='taml'THAI='thai'TURKISH='turk'UKRAINIAN='ukrn'VENETIAN='venetn'VIETNAMESE='viet'# Note: we cache these functions so we only have to traverse long# lists of if-statements once per value.# TODO: This call is specific to the client so should probably be# moved to the app's locale subsystem or whatnot.
[docs]@cached_propertydefrequires_full_unicode_display(self)->bool:"""Do we need to be able to draw full unicode to show this?"""# pylint: disable=too-many-boolean-expressionscls=type(self)# DO need full unicode.if(selfiscls.CHINESE_TRADITIONALorselfiscls.CHINESE_SIMPLIFIEDorselfiscls.ARABICorselfiscls.HINDIorselfiscls.KOREANorselfiscls.PERSIANorselfiscls.TAMILorselfiscls.THAIorselfiscls.VIETNAMESE):returnTrue# Do NOT need full unicode.if(selfiscls.ENGLISHorselfiscls.PORTUGUESE_PORTUGALorselfiscls.PORTUGUESE_BRAZILorselfiscls.BELARUSSIANorselfiscls.CROATIANorselfiscls.CZECHorselfiscls.DANISHorselfiscls.DUTCHorselfiscls.PIRATE_SPEAKorselfiscls.ESPERANTOorselfiscls.FILIPINOorselfiscls.FRENCHorselfiscls.GERMANorselfiscls.GIBBERISHorselfiscls.GREEKorselfiscls.HUNGARIANorselfiscls.INDONESIANorselfiscls.ITALIANorselfiscls.MALAYorselfiscls.POLISHorselfiscls.ROMANIANorselfiscls.RUSSIANorselfiscls.SERBIANorselfiscls.SPANISH_LATIN_AMERICAorselfiscls.SPANISH_SPAINorselfiscls.SLOVAKorselfiscls.SWEDISHorselfiscls.TURKISHorselfiscls.UKRAINIANorselfiscls.VENETIAN):returnFalse# Make sure we're covering all cases.assert_never(self)
# Docs-generation hack; import some stuff that we likely only forward-declared# in our actual source code so that docs tools can find it.fromtypingimport(Coroutine,Any,Literal,Callable,Generator,Awaitable,Sequence,Self)importasynciofromconcurrent.futuresimportFuturefrompathlibimportPathfromenumimportEnum