forked from python-babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.py
736 lines (614 loc) · 27.5 KB
/
support.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
"""
babel.support
~~~~~~~~~~~~~
Several classes and functions that help with integrating and using Babel
in applications.
.. note: the code in this module is not used by Babel itself
:copyright: (c) 2013-2024 by the Babel Team.
:license: BSD, see LICENSE for more details.
"""
from __future__ import annotations
import decimal
import gettext
import locale
import os
from collections.abc import Iterator
from typing import TYPE_CHECKING, Any, Callable, Iterable
from sep_coverage import instrument, CoverageEntity
from babel.core import Locale
from babel.dates import format_date, format_datetime, format_time, format_timedelta
from babel.numbers import (
format_compact_currency,
format_compact_decimal,
format_currency,
format_decimal,
format_percent,
format_scientific,
)
if TYPE_CHECKING:
from typing_extensions import Literal
from babel.dates import _PredefinedTimeFormat
class Format:
"""Wrapper class providing the various date and number formatting functions
bound to a specific locale and time-zone.
>>> from babel.util import UTC
>>> from datetime import date
>>> fmt = Format('en_US', UTC)
>>> fmt.date(date(2007, 4, 1))
u'Apr 1, 2007'
>>> fmt.decimal(1.2345)
u'1.234'
"""
def __init__(
self,
locale: Locale | str,
tzinfo: datetime.tzinfo | None = None,
*,
numbering_system: Literal["default"] | str = "latn",
) -> None:
"""Initialize the formatter.
:param locale: the locale identifier or `Locale` instance
:param tzinfo: the time-zone info (a `tzinfo` instance or `None`)
:param numbering_system: The numbering system used for formatting number symbols. Defaults to "latn".
The special value "default" will use the default numbering system of the locale.
"""
self.locale = Locale.parse(locale)
self.tzinfo = tzinfo
self.numbering_system = numbering_system
def date(
self,
date: datetime.date | None = None,
format: _PredefinedTimeFormat | str = 'medium',
) -> str:
"""Return a date formatted according to the given pattern.
>>> from datetime import date
>>> fmt = Format('en_US')
>>> fmt.date(date(2007, 4, 1))
u'Apr 1, 2007'
"""
return format_date(date, format, locale=self.locale)
def datetime(
self,
datetime: datetime.date | None = None,
format: _PredefinedTimeFormat | str = 'medium',
) -> str:
"""Return a date and time formatted according to the given pattern.
>>> from datetime import datetime
>>> from babel.dates import get_timezone
>>> fmt = Format('en_US', tzinfo=get_timezone('US/Eastern'))
>>> fmt.datetime(datetime(2007, 4, 1, 15, 30))
u'Apr 1, 2007, 11:30:00\u202fAM'
"""
return format_datetime(datetime, format, tzinfo=self.tzinfo, locale=self.locale)
def time(
self,
time: datetime.time | datetime.datetime | None = None,
format: _PredefinedTimeFormat | str = 'medium',
) -> str:
"""Return a time formatted according to the given pattern.
>>> from datetime import datetime
>>> from babel.dates import get_timezone
>>> fmt = Format('en_US', tzinfo=get_timezone('US/Eastern'))
>>> fmt.time(datetime(2007, 4, 1, 15, 30))
u'11:30:00\u202fAM'
"""
return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)
def timedelta(
self,
delta: datetime.timedelta | int,
granularity: Literal["year", "month", "week", "day", "hour", "minute", "second"] = "second",
threshold: float = 0.85,
format: Literal["narrow", "short", "medium", "long"] = "long",
add_direction: bool = False,
) -> str:
"""Return a time delta according to the rules of the given locale.
>>> from datetime import timedelta
>>> fmt = Format('en_US')
>>> fmt.timedelta(timedelta(weeks=11))
u'3 months'
"""
return format_timedelta(delta, granularity=granularity,
threshold=threshold,
format=format, add_direction=add_direction,
locale=self.locale)
def number(self, number: float | decimal.Decimal | str) -> str:
"""Return an integer number formatted for the locale.
>>> fmt = Format('en_US')
>>> fmt.number(1099)
u'1,099'
"""
return format_decimal(number, locale=self.locale, numbering_system=self.numbering_system)
def decimal(self, number: float | decimal.Decimal | str, format: str | None = None) -> str:
"""Return a decimal number formatted for the locale.
>>> fmt = Format('en_US')
>>> fmt.decimal(1.2345)
u'1.234'
"""
return format_decimal(number, format, locale=self.locale, numbering_system=self.numbering_system)
def compact_decimal(
self,
number: float | decimal.Decimal | str,
format_type: Literal['short', 'long'] = 'short',
fraction_digits: int = 0,
) -> str:
"""Return a number formatted in compact form for the locale.
>>> fmt = Format('en_US')
>>> fmt.compact_decimal(123456789)
u'123M'
>>> fmt.compact_decimal(1234567, format_type='long', fraction_digits=2)
'1.23 million'
"""
return format_compact_decimal(
number,
format_type=format_type,
fraction_digits=fraction_digits,
locale=self.locale,
numbering_system=self.numbering_system,
)
def currency(self, number: float | decimal.Decimal | str, currency: str) -> str:
"""Return a number in the given currency formatted for the locale.
"""
return format_currency(number, currency, locale=self.locale, numbering_system=self.numbering_system)
def compact_currency(
self,
number: float | decimal.Decimal | str,
currency: str,
format_type: Literal['short'] = 'short',
fraction_digits: int = 0,
) -> str:
"""Return a number in the given currency formatted for the locale
using the compact number format.
>>> Format('en_US').compact_currency(1234567, "USD", format_type='short', fraction_digits=2)
'$1.23M'
"""
return format_compact_currency(number, currency, format_type=format_type, fraction_digits=fraction_digits,
locale=self.locale, numbering_system=self.numbering_system)
def percent(self, number: float | decimal.Decimal | str, format: str | None = None) -> str:
"""Return a number formatted as percentage for the locale.
>>> fmt = Format('en_US')
>>> fmt.percent(0.34)
u'34%'
"""
return format_percent(number, format, locale=self.locale, numbering_system=self.numbering_system)
def scientific(self, number: float | decimal.Decimal | str) -> str:
"""Return a number formatted using scientific notation for the locale.
"""
return format_scientific(number, locale=self.locale, numbering_system=self.numbering_system)
class LazyProxy:
"""Class for proxy objects that delegate to a specified function to evaluate
the actual object.
>>> def greeting(name='world'):
... return 'Hello, %s!' % name
>>> lazy_greeting = LazyProxy(greeting, name='Joe')
>>> print(lazy_greeting)
Hello, Joe!
>>> u' ' + lazy_greeting
u' Hello, Joe!'
>>> u'(%s)' % lazy_greeting
u'(Hello, Joe!)'
This can be used, for example, to implement lazy translation functions that
delay the actual translation until the string is actually used. The
rationale for such behavior is that the locale of the user may not always
be available. In web applications, you only know the locale when processing
a request.
The proxy implementation attempts to be as complete as possible, so that
the lazy objects should mostly work as expected, for example for sorting:
>>> greetings = [
... LazyProxy(greeting, 'world'),
... LazyProxy(greeting, 'Joe'),
... LazyProxy(greeting, 'universe'),
... ]
>>> greetings.sort()
>>> for greeting in greetings:
... print(greeting)
Hello, Joe!
Hello, universe!
Hello, world!
"""
__slots__ = ['_func', '_args', '_kwargs', '_value', '_is_cache_enabled', '_attribute_error']
if TYPE_CHECKING:
_func: Callable[..., Any]
_args: tuple[Any, ...]
_kwargs: dict[str, Any]
_is_cache_enabled: bool
_value: Any
_attribute_error: AttributeError | None
def __init__(self, func: Callable[..., Any], *args: Any, enable_cache: bool = True, **kwargs: Any) -> None:
# Avoid triggering our own __setattr__ implementation
object.__setattr__(self, '_func', func)
object.__setattr__(self, '_args', args)
object.__setattr__(self, '_kwargs', kwargs)
object.__setattr__(self, '_is_cache_enabled', enable_cache)
object.__setattr__(self, '_value', None)
object.__setattr__(self, '_attribute_error', None)
@property
def value(self) -> Any:
if self._value is None:
try:
value = self._func(*self._args, **self._kwargs)
except AttributeError as error:
object.__setattr__(self, '_attribute_error', error)
raise
if not self._is_cache_enabled:
return value
object.__setattr__(self, '_value', value)
return self._value
def __contains__(self, key: object) -> bool:
return key in self.value
def __bool__(self) -> bool:
return bool(self.value)
def __dir__(self) -> list[str]:
return dir(self.value)
def __iter__(self) -> Iterator[Any]:
return iter(self.value)
def __len__(self) -> int:
return len(self.value)
def __str__(self) -> str:
return str(self.value)
def __add__(self, other: object) -> Any:
return self.value + other
def __radd__(self, other: object) -> Any:
return other + self.value
def __mod__(self, other: object) -> Any:
return self.value % other
def __rmod__(self, other: object) -> Any:
return other % self.value
def __mul__(self, other: object) -> Any:
return self.value * other
def __rmul__(self, other: object) -> Any:
return other * self.value
def __call__(self, *args: Any, **kwargs: Any) -> Any:
return self.value(*args, **kwargs)
def __lt__(self, other: object) -> bool:
return self.value < other
def __le__(self, other: object) -> bool:
return self.value <= other
def __eq__(self, other: object) -> bool:
return self.value == other
def __ne__(self, other: object) -> bool:
return self.value != other
def __gt__(self, other: object) -> bool:
return self.value > other
def __ge__(self, other: object) -> bool:
return self.value >= other
def __delattr__(self, name: str) -> None:
delattr(self.value, name)
def __getattr__(self, name: str) -> Any:
if self._attribute_error is not None:
raise self._attribute_error
return getattr(self.value, name)
def __setattr__(self, name: str, value: Any) -> None:
setattr(self.value, name, value)
def __delitem__(self, key: Any) -> None:
del self.value[key]
def __getitem__(self, key: Any) -> Any:
return self.value[key]
def __setitem__(self, key: Any, value: Any) -> None:
self.value[key] = value
def __copy__(self) -> LazyProxy:
return LazyProxy(
self._func,
enable_cache=self._is_cache_enabled,
*self._args, # noqa: B026
**self._kwargs,
)
def __deepcopy__(self, memo: Any) -> LazyProxy:
from copy import deepcopy
return LazyProxy(
deepcopy(self._func, memo),
enable_cache=deepcopy(self._is_cache_enabled, memo),
*deepcopy(self._args, memo), # noqa: B026
**deepcopy(self._kwargs, memo),
)
class NullTranslations(gettext.NullTranslations):
if TYPE_CHECKING:
_info: dict[str, str]
_fallback: NullTranslations | None
DEFAULT_DOMAIN = None
def __init__(self, fp: gettext._TranslationsReader | None = None) -> None:
"""Initialize a simple translations class which is not backed by a
real catalog. Behaves similar to gettext.NullTranslations but also
offers Babel's on *gettext methods (e.g. 'dgettext()').
:param fp: a file-like object (ignored in this class)
"""
# These attributes are set by gettext.NullTranslations when a catalog
# is parsed (fp != None). Ensure that they are always present because
# some *gettext methods (including '.gettext()') rely on the attributes.
self._catalog: dict[tuple[str, Any] | str, str] = {}
self.plural: Callable[[float | decimal.Decimal], int] = lambda n: int(n != 1)
super().__init__(fp=fp)
self.files = list(filter(None, [getattr(fp, 'name', None)]))
self.domain = self.DEFAULT_DOMAIN
self._domains: dict[str, NullTranslations] = {}
def dgettext(self, domain: str, message: str) -> str:
"""Like ``gettext()``, but look the message up in the specified
domain.
"""
return self._domains.get(domain, self).gettext(message)
def ldgettext(self, domain: str, message: str) -> str:
"""Like ``lgettext()``, but look the message up in the specified
domain.
"""
import warnings
warnings.warn(
'ldgettext() is deprecated, use dgettext() instead',
DeprecationWarning,
stacklevel=2,
)
return self._domains.get(domain, self).lgettext(message)
def udgettext(self, domain: str, message: str) -> str:
"""Like ``ugettext()``, but look the message up in the specified
domain.
"""
return self._domains.get(domain, self).ugettext(message)
# backward compatibility with 0.9
dugettext = udgettext
def dngettext(self, domain: str, singular: str, plural: str, num: int) -> str:
"""Like ``ngettext()``, but look the message up in the specified
domain.
"""
return self._domains.get(domain, self).ngettext(singular, plural, num)
def ldngettext(self, domain: str, singular: str, plural: str, num: int) -> str:
"""Like ``lngettext()``, but look the message up in the specified
domain.
"""
import warnings
warnings.warn(
'ldngettext() is deprecated, use dngettext() instead',
DeprecationWarning,
stacklevel=2,
)
return self._domains.get(domain, self).lngettext(singular, plural, num)
def udngettext(self, domain: str, singular: str, plural: str, num: int) -> str:
"""Like ``ungettext()`` but look the message up in the specified
domain.
"""
return self._domains.get(domain, self).ungettext(singular, plural, num)
# backward compatibility with 0.9
dungettext = udngettext
# Most of the downwards code, until it gets included in stdlib, from:
# https://bugs.python.org/file10036/gettext-pgettext.patch
#
# The encoding of a msgctxt and a msgid in a .mo file is
# msgctxt + "\x04" + msgid (gettext version >= 0.15)
CONTEXT_ENCODING = '%s\x04%s'
def pgettext(self, context: str, message: str) -> str | object:
"""Look up the `context` and `message` id in the catalog and return the
corresponding message string, as an 8-bit string encoded with the
catalog's charset encoding, if known. If there is no entry in the
catalog for the `message` id and `context` , and a fallback has been
set, the look up is forwarded to the fallback's ``pgettext()``
method. Otherwise, the `message` id is returned.
"""
ctxt_msg_id = self.CONTEXT_ENCODING % (context, message)
missing = object()
tmsg = self._catalog.get(ctxt_msg_id, missing)
if tmsg is missing:
if self._fallback:
return self._fallback.pgettext(context, message)
return message
return tmsg
def lpgettext(self, context: str, message: str) -> str | bytes | object:
"""Equivalent to ``pgettext()``, but the translation is returned in the
preferred system encoding, if no other encoding was explicitly set with
``bind_textdomain_codeset()``.
"""
import warnings
warnings.warn(
'lpgettext() is deprecated, use pgettext() instead',
DeprecationWarning,
stacklevel=2,
)
tmsg = self.pgettext(context, message)
encoding = getattr(self, "_output_charset", None) or locale.getpreferredencoding()
return tmsg.encode(encoding) if isinstance(tmsg, str) else tmsg
def npgettext(self, context: str, singular: str, plural: str, num: int) -> str:
"""Do a plural-forms lookup of a message id. `singular` is used as the
message id for purposes of lookup in the catalog, while `num` is used to
determine which plural form to use. The returned message string is an
8-bit string encoded with the catalog's charset encoding, if known.
If the message id for `context` is not found in the catalog, and a
fallback is specified, the request is forwarded to the fallback's
``npgettext()`` method. Otherwise, when ``num`` is 1 ``singular`` is
returned, and ``plural`` is returned in all other cases.
"""
ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
try:
tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
return tmsg
except KeyError:
if self._fallback:
return self._fallback.npgettext(context, singular, plural, num)
if num == 1:
return singular
else:
return plural
def lnpgettext(self, context: str, singular: str, plural: str, num: int) -> str | bytes:
"""Equivalent to ``npgettext()``, but the translation is returned in the
preferred system encoding, if no other encoding was explicitly set with
``bind_textdomain_codeset()``.
"""
import warnings
warnings.warn(
'lnpgettext() is deprecated, use npgettext() instead',
DeprecationWarning,
stacklevel=2,
)
ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
try:
tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
encoding = getattr(self, "_output_charset", None) or locale.getpreferredencoding()
return tmsg.encode(encoding)
except KeyError:
if self._fallback:
return self._fallback.lnpgettext(context, singular, plural, num)
if num == 1:
return singular
else:
return plural
def upgettext(self, context: str, message: str) -> str:
"""Look up the `context` and `message` id in the catalog and return the
corresponding message string, as a Unicode string. If there is no entry
in the catalog for the `message` id and `context`, and a fallback has
been set, the look up is forwarded to the fallback's ``upgettext()``
method. Otherwise, the `message` id is returned.
"""
ctxt_message_id = self.CONTEXT_ENCODING % (context, message)
missing = object()
tmsg = self._catalog.get(ctxt_message_id, missing)
if tmsg is missing:
if self._fallback:
return self._fallback.upgettext(context, message)
return str(message)
assert isinstance(tmsg, str)
return tmsg
def unpgettext(self, context: str, singular: str, plural: str, num: int) -> str:
"""Do a plural-forms lookup of a message id. `singular` is used as the
message id for purposes of lookup in the catalog, while `num` is used to
determine which plural form to use. The returned message string is a
Unicode string.
If the message id for `context` is not found in the catalog, and a
fallback is specified, the request is forwarded to the fallback's
``unpgettext()`` method. Otherwise, when `num` is 1 `singular` is
returned, and `plural` is returned in all other cases.
"""
ctxt_message_id = self.CONTEXT_ENCODING % (context, singular)
try:
tmsg = self._catalog[(ctxt_message_id, self.plural(num))]
except KeyError:
if self._fallback:
return self._fallback.unpgettext(context, singular, plural, num)
tmsg = str(singular) if num == 1 else str(plural)
return tmsg
def dpgettext(self, domain: str, context: str, message: str) -> str | object:
"""Like `pgettext()`, but look the message up in the specified
`domain`.
"""
return self._domains.get(domain, self).pgettext(context, message)
def udpgettext(self, domain: str, context: str, message: str) -> str:
"""Like `upgettext()`, but look the message up in the specified
`domain`.
"""
return self._domains.get(domain, self).upgettext(context, message)
# backward compatibility with 0.9
dupgettext = udpgettext
def ldpgettext(self, domain: str, context: str, message: str) -> str | bytes | object:
"""Equivalent to ``dpgettext()``, but the translation is returned in the
preferred system encoding, if no other encoding was explicitly set with
``bind_textdomain_codeset()``.
"""
return self._domains.get(domain, self).lpgettext(context, message)
def dnpgettext(self, domain: str, context: str, singular: str, plural: str, num: int) -> str:
"""Like ``npgettext``, but look the message up in the specified
`domain`.
"""
return self._domains.get(domain, self).npgettext(context, singular,
plural, num)
def udnpgettext(self, domain: str, context: str, singular: str, plural: str, num: int) -> str:
"""Like ``unpgettext``, but look the message up in the specified
`domain`.
"""
return self._domains.get(domain, self).unpgettext(context, singular,
plural, num)
# backward compatibility with 0.9
dunpgettext = udnpgettext
def ldnpgettext(self, domain: str, context: str, singular: str, plural: str, num: int) -> str | bytes:
"""Equivalent to ``dnpgettext()``, but the translation is returned in
the preferred system encoding, if no other encoding was explicitly set
with ``bind_textdomain_codeset()``.
"""
return self._domains.get(domain, self).lnpgettext(context, singular,
plural, num)
ugettext = gettext.NullTranslations.gettext
ungettext = gettext.NullTranslations.ngettext
class Translations(NullTranslations, gettext.GNUTranslations):
"""An extended translation catalog class."""
DEFAULT_DOMAIN = 'messages'
def __init__(self, fp: gettext._TranslationsReader | None = None, domain: str | None = None):
"""Initialize the translations catalog.
:param fp: the file-like object the translation should be read from
:param domain: the message domain (default: 'messages')
"""
super().__init__(fp=fp)
self.domain = domain or self.DEFAULT_DOMAIN
ugettext = gettext.GNUTranslations.gettext
ungettext = gettext.GNUTranslations.ngettext
@classmethod
def load(
cls,
dirname: str | os.PathLike[str] | None = None,
locales: Iterable[str | Locale] | str | Locale | None = None,
domain: str | None = None,
) -> NullTranslations:
"""Load translations from the given directory.
:param dirname: the directory containing the ``MO`` files
:param locales: the list of locales in order of preference (items in
this list can be either `Locale` objects or locale
strings)
:param domain: the message domain (default: 'messages')
"""
if not domain:
domain = cls.DEFAULT_DOMAIN
filename = gettext.find(domain, dirname, _locales_to_names(locales))
if not filename:
return NullTranslations()
with open(filename, 'rb') as fp:
return cls(fp=fp, domain=domain)
def __repr__(self) -> str:
version = self._info.get('project-id-version')
return f'<{type(self).__name__}: "{version}">'
def add(self, translations: Translations, merge: bool = True):
"""Add the given translations to the catalog.
If the domain of the translations is different than that of the
current catalog, they are added as a catalog that is only accessible
by the various ``d*gettext`` functions.
:param translations: the `Translations` instance with the messages to
add
:param merge: whether translations for message domains that have
already been added should be merged with the existing
translations
"""
domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN)
if merge and domain == self.domain:
return self.merge(translations)
existing = self._domains.get(domain)
if merge and isinstance(existing, Translations):
existing.merge(translations)
else:
translations.add_fallback(self)
self._domains[domain] = translations
return self
def merge(self, translations: Translations):
"""Merge the given translations into the catalog.
Message translations in the specified catalog override any messages
with the same identifier in the existing catalog.
:param translations: the `Translations` instance with the messages to
merge
"""
if isinstance(translations, gettext.GNUTranslations):
self._catalog.update(translations._catalog)
if isinstance(translations, Translations):
self.files.extend(translations.files)
return self
def _locales_to_names(
locales: Iterable[str | Locale] | str | Locale | None,
) -> list[str] | None:
"""Normalize a `locales` argument to a list of locale names.
:param locales: the list of locales in order of preference (items in
this list can be either `Locale` objects or locale
strings)
"""
instrument([CoverageEntity.SUPPORT, CoverageEntity.LOCALES_TO_NAMES], 0)
if locales is None:
instrument([CoverageEntity.SUPPORT, CoverageEntity.LOCALES_TO_NAMES], 1)
return None
instrument([CoverageEntity.SUPPORT, CoverageEntity.LOCALES_TO_NAMES], 2)
if isinstance(locales, Locale):
instrument([CoverageEntity.SUPPORT, CoverageEntity.LOCALES_TO_NAMES], 3)
return [str(locales)]
instrument([CoverageEntity.SUPPORT, CoverageEntity.LOCALES_TO_NAMES], 4)
if isinstance(locales, str):
instrument([CoverageEntity.SUPPORT, CoverageEntity.LOCALES_TO_NAMES], 5)
return [locales]
instrument([CoverageEntity.SUPPORT, CoverageEntity.LOCALES_TO_NAMES], 6)
return [str(locale) for locale in locales]