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 ]:
@@ -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
@@ -421,7 +426,9 @@ def _try_load_reducing(parts):
421426 else :
422427 language2 , _ , script2 , variant2 = parts2
423428 modifier2 = None
424- locale = _try_load_reducing ((language2 , territory , script2 , variant2 , modifier2 ))
429+ locale = _try_load_reducing (
430+ (language2 , territory , script2 , variant2 , modifier2 ),
431+ )
425432 if locale is not None :
426433 return locale
427434
@@ -432,19 +439,18 @@ def __eq__(self, other: object) -> bool:
432439 if not hasattr (other , key ):
433440 return False
434441 return (
435- self .language == getattr (other , 'language' ) and # noqa: B009
436- self .territory == getattr (other , 'territory' ) and # noqa: B009
437- self .script == getattr (other , 'script' ) and # noqa: B009
438- self .variant == getattr (other , 'variant' ) and # noqa: B009
439- 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
440447 )
441448
442449 def __ne__ (self , other : object ) -> bool :
443450 return not self .__eq__ (other )
444451
445452 def __hash__ (self ) -> int :
446- return hash ((self .language , self .territory , self .script ,
447- self .variant , self .modifier ))
453+ return hash ((self .language , self .territory , self .script , self .variant , self .modifier ))
448454
449455 def __repr__ (self ) -> str :
450456 parameters = ['' ]
@@ -455,9 +461,9 @@ def __repr__(self) -> str:
455461 return f"Locale({ self .language !r} { ', ' .join (parameters )} )"
456462
457463 def __str__ (self ) -> str :
458- return get_locale_identifier (( self . language , self . territory ,
459- self .script , self .variant ,
460- self . modifier ) )
464+ return get_locale_identifier (
465+ ( self . language , self . territory , self .script , self .variant , self . modifier ) ,
466+ )
461467
462468 @property
463469 def _data (self ) -> localedata .LocaleDataDict :
@@ -500,7 +506,9 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
500506 retval += f" ({ detail_string } )"
501507 return retval
502508
503- display_name = property (get_display_name , doc = """\
509+ display_name = property (
510+ get_display_name ,
511+ doc = """\
504512 The localized display name of the locale.
505513
506514 >>> Locale('en').display_name
@@ -511,7 +519,8 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
511519 u'svenska'
512520
513521 :type: `unicode`
514- """ )
522+ """ ,
523+ )
515524
516525 def get_language_name (self , locale : Locale | str | None = None ) -> str | None :
517526 """Return the language of this locale in the given locale.
@@ -528,12 +537,15 @@ def get_language_name(self, locale: Locale | str | None = None) -> str | None:
528537 locale = Locale .parse (locale )
529538 return locale .languages .get (self .language )
530539
531- language_name = property (get_language_name , doc = """\
540+ language_name = property (
541+ get_language_name ,
542+ doc = """\
532543 The localized language name of the locale.
533544
534545 >>> Locale('en', 'US').language_name
535546 u'English'
536- """ )
547+ """ ,
548+ )
537549
538550 def get_territory_name (self , locale : Locale | str | None = None ) -> str | None :
539551 """Return the territory name in the given locale."""
@@ -542,12 +554,15 @@ def get_territory_name(self, locale: Locale | str | None = None) -> str | None:
542554 locale = Locale .parse (locale )
543555 return locale .territories .get (self .territory or '' )
544556
545- territory_name = property (get_territory_name , doc = """\
557+ territory_name = property (
558+ get_territory_name ,
559+ doc = """\
546560 The localized territory name of the locale if available.
547561
548562 >>> Locale('de', 'DE').territory_name
549563 u'Deutschland'
550- """ )
564+ """ ,
565+ )
551566
552567 def get_script_name (self , locale : Locale | str | None = None ) -> str | None :
553568 """Return the script name in the given locale."""
@@ -556,12 +571,15 @@ def get_script_name(self, locale: Locale | str | None = None) -> str | None:
556571 locale = Locale .parse (locale )
557572 return locale .scripts .get (self .script or '' )
558573
559- script_name = property (get_script_name , doc = """\
574+ script_name = property (
575+ get_script_name ,
576+ doc = """\
560577 The localized script name of the locale if available.
561578
562579 >>> Locale('sr', 'ME', script='Latn').script_name
563580 u'latinica'
564- """ )
581+ """ ,
582+ )
565583
566584 @property
567585 def english_name (self ) -> str | None :
@@ -785,8 +803,7 @@ def day_periods(self) -> localedata.LocaleDataDict:
785803
786804 @property
787805 def day_period_rules (self ) -> localedata .LocaleDataDict :
788- """Day period rules for the locale. Used by `get_period_id`.
789- """
806+ """Day period rules for the locale. Used by `get_period_id`."""
790807 return self ._data .get ('day_period_rules' , localedata .LocaleDataDict ({}))
791808
792809 @property
@@ -1150,7 +1167,12 @@ def default_locale(
11501167 return None
11511168
11521169
1153- 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 :
11541176 """Find the best match between available and requested locale strings.
11551177
11561178 >>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
@@ -1216,7 +1238,10 @@ def negotiate_locale(preferred: Iterable[str], available: Iterable[str], sep: st
12161238def parse_locale (
12171239 identifier : str ,
12181240 sep : str = '_' ,
1219- ) -> 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+ ):
12201245 """Parse a locale identifier into a tuple of the form ``(language,
12211246 territory, script, variant, modifier)``.
12221247
@@ -1294,8 +1319,10 @@ def parse_locale(
12941319 territory = parts .pop (0 )
12951320
12961321 if parts and (
1297- len (parts [0 ]) == 4 and parts [0 ][0 ].isdigit () or
1298- 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 ()
12991326 ):
13001327 variant = parts .pop ().upper ()
13011328
0 commit comments