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 ]:
@@ -119,7 +121,7 @@ def get_global(key: _GLOBAL_KEY) -> Mapping[str, Any]:
119
121
'mk' : 'mk_MK' , 'nl' : 'nl_NL' , 'nn' : 'nn_NO' , 'no' : 'nb_NO' , 'pl' : 'pl_PL' ,
120
122
'pt' : 'pt_PT' , 'ro' : 'ro_RO' , 'ru' : 'ru_RU' , 'sk' : 'sk_SK' , 'sl' : 'sl_SI' ,
121
123
'sv' : 'sv_SE' , 'th' : 'th_TH' , 'tr' : 'tr_TR' , 'uk' : 'uk_UA' ,
122
- }
124
+ } # fmt: skip
123
125
124
126
125
127
class UnknownLocaleError (Exception ):
@@ -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
@@ -346,7 +351,8 @@ def parse(
346
351
f"variables for the API you tried to use."
347
352
)
348
353
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 )
350
356
raise TypeError (msg )
351
357
352
358
if not isinstance (identifier , str ):
@@ -420,7 +426,9 @@ def _try_load_reducing(parts):
420
426
else :
421
427
language2 , _ , script2 , variant2 = parts2
422
428
modifier2 = None
423
- locale = _try_load_reducing ((language2 , territory , script2 , variant2 , modifier2 ))
429
+ locale = _try_load_reducing (
430
+ (language2 , territory , script2 , variant2 , modifier2 ),
431
+ )
424
432
if locale is not None :
425
433
return locale
426
434
@@ -431,19 +439,18 @@ def __eq__(self, other: object) -> bool:
431
439
if not hasattr (other , key ):
432
440
return False
433
441
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
439
447
)
440
448
441
449
def __ne__ (self , other : object ) -> bool :
442
450
return not self .__eq__ (other )
443
451
444
452
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 ))
447
454
448
455
def __repr__ (self ) -> str :
449
456
parameters = ['' ]
@@ -454,9 +461,9 @@ def __repr__(self) -> str:
454
461
return f"Locale({ self .language !r} { ', ' .join (parameters )} )"
455
462
456
463
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
+ )
460
467
461
468
@property
462
469
def _data (self ) -> localedata .LocaleDataDict :
@@ -499,7 +506,9 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
499
506
retval += f" ({ detail_string } )"
500
507
return retval
501
508
502
- display_name = property (get_display_name , doc = """\
509
+ display_name = property (
510
+ get_display_name ,
511
+ doc = """\
503
512
The localized display name of the locale.
504
513
505
514
>>> Locale('en').display_name
@@ -510,7 +519,8 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
510
519
u'svenska'
511
520
512
521
:type: `unicode`
513
- """ )
522
+ """ ,
523
+ )
514
524
515
525
def get_language_name (self , locale : Locale | str | None = None ) -> str | None :
516
526
"""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:
527
537
locale = Locale .parse (locale )
528
538
return locale .languages .get (self .language )
529
539
530
- language_name = property (get_language_name , doc = """\
540
+ language_name = property (
541
+ get_language_name ,
542
+ doc = """\
531
543
The localized language name of the locale.
532
544
533
545
>>> Locale('en', 'US').language_name
534
546
u'English'
535
- """ )
547
+ """ ,
548
+ )
536
549
537
550
def get_territory_name (self , locale : Locale | str | None = None ) -> str | None :
538
551
"""Return the territory name in the given locale."""
@@ -541,12 +554,15 @@ def get_territory_name(self, locale: Locale | str | None = None) -> str | None:
541
554
locale = Locale .parse (locale )
542
555
return locale .territories .get (self .territory or '' )
543
556
544
- territory_name = property (get_territory_name , doc = """\
557
+ territory_name = property (
558
+ get_territory_name ,
559
+ doc = """\
545
560
The localized territory name of the locale if available.
546
561
547
562
>>> Locale('de', 'DE').territory_name
548
563
u'Deutschland'
549
- """ )
564
+ """ ,
565
+ )
550
566
551
567
def get_script_name (self , locale : Locale | str | None = None ) -> str | None :
552
568
"""Return the script name in the given locale."""
@@ -555,12 +571,15 @@ def get_script_name(self, locale: Locale | str | None = None) -> str | None:
555
571
locale = Locale .parse (locale )
556
572
return locale .scripts .get (self .script or '' )
557
573
558
- script_name = property (get_script_name , doc = """\
574
+ script_name = property (
575
+ get_script_name ,
576
+ doc = """\
559
577
The localized script name of the locale if available.
560
578
561
579
>>> Locale('sr', 'ME', script='Latn').script_name
562
580
u'latinica'
563
- """ )
581
+ """ ,
582
+ )
564
583
565
584
@property
566
585
def english_name (self ) -> str | None :
@@ -784,8 +803,7 @@ def day_periods(self) -> localedata.LocaleDataDict:
784
803
785
804
@property
786
805
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`."""
789
807
return self ._data .get ('day_period_rules' , localedata .LocaleDataDict ({}))
790
808
791
809
@property
@@ -1149,7 +1167,12 @@ def default_locale(
1149
1167
return None
1150
1168
1151
1169
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 :
1153
1176
"""Find the best match between available and requested locale strings.
1154
1177
1155
1178
>>> 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
1215
1238
def parse_locale (
1216
1239
identifier : str ,
1217
1240
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
+ ):
1219
1245
"""Parse a locale identifier into a tuple of the form ``(language,
1220
1246
territory, script, variant, modifier)``.
1221
1247
@@ -1293,8 +1319,10 @@ def parse_locale(
1293
1319
territory = parts .pop (0 )
1294
1320
1295
1321
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 ()
1298
1326
):
1299
1327
variant = parts .pop ().upper ()
1300
1328
0 commit comments