11"""
2- babel.core
3- ~~~~~~~~~~
2+ babel.core
3+ ~~~~~~~~~~
44
5- Core locale representation and locale data access.
5+ Core locale representation and locale data access.
66
7- :copyright: (c) 2013-2025 by the Babel Team.
8- :license: BSD, see LICENSE for more details.
7+ :copyright: (c) 2013-2025 by the Babel Team.
8+ :license: BSD, see LICENSE for more details.
99"""
1010
1111from __future__ import annotations
5656
5757
5858def _raise_no_data_error ():
59- raise RuntimeError ('The babel data files are not available. '
60- 'This usually happens because you are using '
61- 'a source checkout from Babel and you did '
62- 'not build the data files. Just make sure '
63- 'to run "python setup.py import_cldr" before '
64- 'installing the library.' )
59+ raise RuntimeError (
60+ 'The babel data files are not available. '
61+ 'This usually happens because you are using '
62+ 'a source checkout from Babel and you did '
63+ 'not build the data files. Just make sure '
64+ 'to run "python setup.py import_cldr" before '
65+ 'installing the library.' ,
66+ )
6567
6668
6769def get_global (key : _GLOBAL_KEY ) -> Mapping [str , Any ]:
@@ -119,7 +121,7 @@ def get_global(key: _GLOBAL_KEY) -> Mapping[str, Any]:
119121 'mk' : 'mk_MK' , 'nl' : 'nl_NL' , 'nn' : 'nn_NO' , 'no' : 'nb_NO' , 'pl' : 'pl_PL' ,
120122 'pt' : 'pt_PT' , 'ro' : 'ro_RO' , 'ru' : 'ru_RU' , 'sk' : 'sk_SK' , 'sl' : 'sl_SI' ,
121123 'sv' : 'sv_SE' , 'th' : 'th_TH' , 'tr' : 'tr_TR' , 'uk' : 'uk_UA' ,
122- }
124+ } # fmt: skip
123125
124126
125127class UnknownLocaleError (Exception ):
@@ -216,7 +218,11 @@ def __init__(
216218 raise UnknownLocaleError (identifier )
217219
218220 @classmethod
219- def default (cls , category : str | None = None , aliases : Mapping [str , str ] = LOCALE_ALIASES ) -> Locale :
221+ def default (
222+ cls ,
223+ category : str | None = None ,
224+ aliases : Mapping [str , str ] = LOCALE_ALIASES ,
225+ ) -> Locale :
220226 """Return the system default locale for the specified category.
221227
222228 >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']:
@@ -268,8 +274,7 @@ def negotiate(
268274 :param aliases: a dictionary of aliases for locale identifiers
269275 :param sep: separator for parsing; e.g. Windows tends to use '-' instead of '_'.
270276 """
271- identifier = negotiate_locale (preferred , available , sep = sep ,
272- aliases = aliases )
277+ identifier = negotiate_locale (preferred , available , sep = sep , aliases = aliases )
273278 if identifier :
274279 return Locale .parse (identifier , sep = sep )
275280 return None
@@ -346,7 +351,8 @@ def parse(
346351 f"variables for the API you tried to use."
347352 )
348353 if isinstance (identifier , str ):
349- raise ValueError (msg ) # `parse_locale` would raise a ValueError, so let's do that here
354+ # `parse_locale` would raise a ValueError, so let's do that here
355+ raise ValueError (msg )
350356 raise TypeError (msg )
351357
352358 if not isinstance (identifier , str ):
@@ -420,7 +426,9 @@ def _try_load_reducing(parts):
420426 else :
421427 language2 , _ , script2 , variant2 = parts2
422428 modifier2 = None
423- locale = _try_load_reducing ((language2 , territory , script2 , variant2 , modifier2 ))
429+ locale = _try_load_reducing (
430+ (language2 , territory , script2 , variant2 , modifier2 ),
431+ )
424432 if locale is not None :
425433 return locale
426434
@@ -431,19 +439,18 @@ def __eq__(self, other: object) -> bool:
431439 if not hasattr (other , key ):
432440 return False
433441 return (
434- self .language == getattr (other , 'language' ) and # noqa: B009
435- self .territory == getattr (other , 'territory' ) and # noqa: B009
436- self .script == getattr (other , 'script' ) and # noqa: B009
437- self .variant == getattr (other , 'variant' ) and # noqa: B009
438- self .modifier == getattr (other , 'modifier' ) # noqa: B009
442+ self .language == getattr (other , 'language' ) # noqa: B009
443+ and self .territory == getattr (other , 'territory' ) # noqa: B009
444+ and self .script == getattr (other , 'script' ) # noqa: B009
445+ and self .variant == getattr (other , 'variant' ) # noqa: B009
446+ and self .modifier == getattr (other , 'modifier' ) # noqa: B009
439447 )
440448
441449 def __ne__ (self , other : object ) -> bool :
442450 return not self .__eq__ (other )
443451
444452 def __hash__ (self ) -> int :
445- return hash ((self .language , self .territory , self .script ,
446- self .variant , self .modifier ))
453+ return hash ((self .language , self .territory , self .script , self .variant , self .modifier ))
447454
448455 def __repr__ (self ) -> str :
449456 parameters = ['' ]
@@ -454,9 +461,9 @@ def __repr__(self) -> str:
454461 return f"Locale({ self .language !r} { ', ' .join (parameters )} )"
455462
456463 def __str__ (self ) -> str :
457- return get_locale_identifier (( self . language , self . territory ,
458- self .script , self .variant ,
459- self . modifier ) )
464+ return get_locale_identifier (
465+ ( self . language , self . territory , self .script , self .variant , self . modifier ) ,
466+ )
460467
461468 @property
462469 def _data (self ) -> localedata .LocaleDataDict :
@@ -499,7 +506,9 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
499506 retval += f" ({ detail_string } )"
500507 return retval
501508
502- display_name = property (get_display_name , doc = """\
509+ display_name = property (
510+ get_display_name ,
511+ doc = """\
503512 The localized display name of the locale.
504513
505514 >>> Locale('en').display_name
@@ -510,7 +519,8 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
510519 u'svenska'
511520
512521 :type: `unicode`
513- """ )
522+ """ ,
523+ )
514524
515525 def get_language_name (self , locale : Locale | str | None = None ) -> str | None :
516526 """Return the language of this locale in the given locale.
@@ -527,12 +537,15 @@ def get_language_name(self, locale: Locale | str | None = None) -> str | None:
527537 locale = Locale .parse (locale )
528538 return locale .languages .get (self .language )
529539
530- language_name = property (get_language_name , doc = """\
540+ language_name = property (
541+ get_language_name ,
542+ doc = """\
531543 The localized language name of the locale.
532544
533545 >>> Locale('en', 'US').language_name
534546 u'English'
535- """ )
547+ """ ,
548+ )
536549
537550 def get_territory_name (self , locale : Locale | str | None = None ) -> str | None :
538551 """Return the territory name in the given locale."""
@@ -541,12 +554,15 @@ def get_territory_name(self, locale: Locale | str | None = None) -> str | None:
541554 locale = Locale .parse (locale )
542555 return locale .territories .get (self .territory or '' )
543556
544- territory_name = property (get_territory_name , doc = """\
557+ territory_name = property (
558+ get_territory_name ,
559+ doc = """\
545560 The localized territory name of the locale if available.
546561
547562 >>> Locale('de', 'DE').territory_name
548563 u'Deutschland'
549- """ )
564+ """ ,
565+ )
550566
551567 def get_script_name (self , locale : Locale | str | None = None ) -> str | None :
552568 """Return the script name in the given locale."""
@@ -555,12 +571,15 @@ def get_script_name(self, locale: Locale | str | None = None) -> str | None:
555571 locale = Locale .parse (locale )
556572 return locale .scripts .get (self .script or '' )
557573
558- script_name = property (get_script_name , doc = """\
574+ script_name = property (
575+ get_script_name ,
576+ doc = """\
559577 The localized script name of the locale if available.
560578
561579 >>> Locale('sr', 'ME', script='Latn').script_name
562580 u'latinica'
563- """ )
581+ """ ,
582+ )
564583
565584 @property
566585 def english_name (self ) -> str | None :
@@ -784,8 +803,7 @@ def day_periods(self) -> localedata.LocaleDataDict:
784803
785804 @property
786805 def day_period_rules (self ) -> localedata .LocaleDataDict :
787- """Day period rules for the locale. Used by `get_period_id`.
788- """
806+ """Day period rules for the locale. Used by `get_period_id`."""
789807 return self ._data .get ('day_period_rules' , localedata .LocaleDataDict ({}))
790808
791809 @property
@@ -1149,7 +1167,12 @@ def default_locale(
11491167 return None
11501168
11511169
1152- def negotiate_locale (preferred : Iterable [str ], available : Iterable [str ], sep : str = '_' , aliases : Mapping [str , str ] = LOCALE_ALIASES ) -> str | None :
1170+ def negotiate_locale (
1171+ preferred : Iterable [str ],
1172+ available : Iterable [str ],
1173+ sep : str = '_' ,
1174+ aliases : Mapping [str , str ] = LOCALE_ALIASES ,
1175+ ) -> str | None :
11531176 """Find the best match between available and requested locale strings.
11541177
11551178 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
@@ -1215,7 +1238,10 @@ def negotiate_locale(preferred: Iterable[str], available: Iterable[str], sep: st
12151238def parse_locale (
12161239 identifier : str ,
12171240 sep : str = '_' ,
1218- ) -> tuple [str , str | None , str | None , str | None ] | tuple [str , str | None , str | None , str | None , str | None ]:
1241+ ) -> (
1242+ tuple [str , str | None , str | None , str | None ]
1243+ | tuple [str , str | None , str | None , str | None , str | None ]
1244+ ):
12191245 """Parse a locale identifier into a tuple of the form ``(language,
12201246 territory, script, variant, modifier)``.
12211247
@@ -1293,8 +1319,10 @@ def parse_locale(
12931319 territory = parts .pop (0 )
12941320
12951321 if parts and (
1296- len (parts [0 ]) == 4 and parts [0 ][0 ].isdigit () or
1297- len (parts [0 ]) >= 5 and parts [0 ][0 ].isalpha ()
1322+ len (parts [0 ]) == 4
1323+ and parts [0 ][0 ].isdigit ()
1324+ or len (parts [0 ]) >= 5
1325+ and parts [0 ][0 ].isalpha ()
12981326 ):
12991327 variant = parts .pop ().upper ()
13001328
0 commit comments