1
1
"""
2
- babel.core
3
- ~~~~~~~~~~
2
+ babel.core
3
+ ~~~~~~~~~~
4
4
5
- Core locale representation and locale data access.
5
+ Core locale representation and locale data access.
6
6
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.
9
9
"""
10
10
11
11
from __future__ import annotations
56
56
57
57
58
58
def _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
+ )
65
67
66
68
67
69
def get_global (key : _GLOBAL_KEY ) -> Mapping [str , Any ]:
@@ -216,7 +218,11 @@ def __init__(
216
218
raise UnknownLocaleError (identifier )
217
219
218
220
@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 :
220
226
"""Return the system default locale for the specified category.
221
227
222
228
>>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']:
@@ -268,8 +274,7 @@ def negotiate(
268
274
:param aliases: a dictionary of aliases for locale identifiers
269
275
:param sep: separator for parsing; e.g. Windows tends to use '-' instead of '_'.
270
276
"""
271
- identifier = negotiate_locale (preferred , available , sep = sep ,
272
- aliases = aliases )
277
+ identifier = negotiate_locale (preferred , available , sep = sep , aliases = aliases )
273
278
if identifier :
274
279
return Locale .parse (identifier , sep = sep )
275
280
return None
@@ -421,7 +426,9 @@ def _try_load_reducing(parts):
421
426
else :
422
427
language2 , _ , script2 , variant2 = parts2
423
428
modifier2 = None
424
- locale = _try_load_reducing ((language2 , territory , script2 , variant2 , modifier2 ))
429
+ locale = _try_load_reducing (
430
+ (language2 , territory , script2 , variant2 , modifier2 ),
431
+ )
425
432
if locale is not None :
426
433
return locale
427
434
@@ -432,19 +439,18 @@ def __eq__(self, other: object) -> bool:
432
439
if not hasattr (other , key ):
433
440
return False
434
441
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
440
447
)
441
448
442
449
def __ne__ (self , other : object ) -> bool :
443
450
return not self .__eq__ (other )
444
451
445
452
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 ))
448
454
449
455
def __repr__ (self ) -> str :
450
456
parameters = ['' ]
@@ -455,9 +461,9 @@ def __repr__(self) -> str:
455
461
return f"Locale({ self .language !r} { ', ' .join (parameters )} )"
456
462
457
463
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
+ )
461
467
462
468
@property
463
469
def _data (self ) -> localedata .LocaleDataDict :
@@ -500,7 +506,9 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
500
506
retval += f" ({ detail_string } )"
501
507
return retval
502
508
503
- display_name = property (get_display_name , doc = """\
509
+ display_name = property (
510
+ get_display_name ,
511
+ doc = """\
504
512
The localized display name of the locale.
505
513
506
514
>>> Locale('en').display_name
@@ -511,7 +519,8 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
511
519
u'svenska'
512
520
513
521
:type: `unicode`
514
- """ )
522
+ """ ,
523
+ )
515
524
516
525
def get_language_name (self , locale : Locale | str | None = None ) -> str | None :
517
526
"""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:
528
537
locale = Locale .parse (locale )
529
538
return locale .languages .get (self .language )
530
539
531
- language_name = property (get_language_name , doc = """\
540
+ language_name = property (
541
+ get_language_name ,
542
+ doc = """\
532
543
The localized language name of the locale.
533
544
534
545
>>> Locale('en', 'US').language_name
535
546
u'English'
536
- """ )
547
+ """ ,
548
+ )
537
549
538
550
def get_territory_name (self , locale : Locale | str | None = None ) -> str | None :
539
551
"""Return the territory name in the given locale."""
@@ -542,12 +554,15 @@ def get_territory_name(self, locale: Locale | str | None = None) -> str | None:
542
554
locale = Locale .parse (locale )
543
555
return locale .territories .get (self .territory or '' )
544
556
545
- territory_name = property (get_territory_name , doc = """\
557
+ territory_name = property (
558
+ get_territory_name ,
559
+ doc = """\
546
560
The localized territory name of the locale if available.
547
561
548
562
>>> Locale('de', 'DE').territory_name
549
563
u'Deutschland'
550
- """ )
564
+ """ ,
565
+ )
551
566
552
567
def get_script_name (self , locale : Locale | str | None = None ) -> str | None :
553
568
"""Return the script name in the given locale."""
@@ -556,12 +571,15 @@ def get_script_name(self, locale: Locale | str | None = None) -> str | None:
556
571
locale = Locale .parse (locale )
557
572
return locale .scripts .get (self .script or '' )
558
573
559
- script_name = property (get_script_name , doc = """\
574
+ script_name = property (
575
+ get_script_name ,
576
+ doc = """\
560
577
The localized script name of the locale if available.
561
578
562
579
>>> Locale('sr', 'ME', script='Latn').script_name
563
580
u'latinica'
564
- """ )
581
+ """ ,
582
+ )
565
583
566
584
@property
567
585
def english_name (self ) -> str | None :
@@ -785,8 +803,7 @@ def day_periods(self) -> localedata.LocaleDataDict:
785
803
786
804
@property
787
805
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`."""
790
807
return self ._data .get ('day_period_rules' , localedata .LocaleDataDict ({}))
791
808
792
809
@property
@@ -1150,7 +1167,12 @@ def default_locale(
1150
1167
return None
1151
1168
1152
1169
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 :
1154
1176
"""Find the best match between available and requested locale strings.
1155
1177
1156
1178
>>> 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
1216
1238
def parse_locale (
1217
1239
identifier : str ,
1218
1240
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
+ ):
1220
1245
"""Parse a locale identifier into a tuple of the form ``(language,
1221
1246
territory, script, variant, modifier)``.
1222
1247
@@ -1294,8 +1319,10 @@ def parse_locale(
1294
1319
territory = parts .pop (0 )
1295
1320
1296
1321
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 ()
1299
1326
):
1300
1327
variant = parts .pop ().upper ()
1301
1328
0 commit comments