16
16
import babel
17
17
import entrypoints
18
18
from packaging .version import parse as parse_version
19
+ from typing import Tuple
19
20
20
21
# Entry points
21
22
JUPYTERLAB_LANGUAGEPACK_ENTRY = "jupyterlab.languagepack"
27
28
LC_MESSAGES_DIR = "LC_MESSAGES"
28
29
DEFAULT_DOMAIN = "jupyterlab"
29
30
L10N_SCHEMA_NAME = "@jupyterlab/translation-extension:plugin"
31
+ PY37_OR_LOWER = sys .version_info [:2 ] <= (3 , 7 )
30
32
31
33
_default_schema_context = "schema"
32
34
_default_settings_context = "settings"
@@ -247,15 +249,17 @@ def merge_locale_data(language_pack_locale_data, package_locale_data):
247
249
return result
248
250
249
251
250
- def get_installed_packages_locale (locale : str ) -> dict :
252
+ def get_installed_packages_locale (locale : str ) -> Tuple [ dict , str ] :
251
253
"""
252
254
Get all jupyterlab extensions installed that contain locale data.
253
255
254
256
Returns
255
257
-------
256
- dict
257
- Ordered list of available language packs.
258
- >>> {"package-name": locale_data, ...}
258
+ tuple
259
+ A tuple in the form `(locale_data_dict, message)`,
260
+ where the `locale_data_dict` is an ordered list
261
+ of available language packs:
262
+ >>> {"package-name": locale_data, ...}
259
263
260
264
Examples
261
265
--------
@@ -298,7 +302,7 @@ def get_installed_packages_locale(locale: str) -> dict:
298
302
299
303
# --- API
300
304
# ----------------------------------------------------------------------------
301
- def get_language_packs (display_locale : str = DEFAULT_LOCALE ) -> tuple :
305
+ def get_language_packs (display_locale : str = DEFAULT_LOCALE ) -> Tuple [ dict , str ] :
302
306
"""
303
307
Return the available language packs installed in the system.
304
308
@@ -487,7 +491,14 @@ def pgettext(self, msgctxt: str, msgid: str) -> str:
487
491
str
488
492
The translated string.
489
493
"""
490
- return gettext .dpgettext (self ._domain , msgctxt , msgid )
494
+ # Python 3.7 or lower does not offer translations based on context.
495
+ # On these versions `pgettext` falls back to `gettext`
496
+ if PY37_OR_LOWER :
497
+ translation = gettext .dgettext (self ._domain , msgid )
498
+ else :
499
+ translation = gettext .dpgettext (self ._domain , msgctxt , msgid )
500
+
501
+ return translation
491
502
492
503
def npgettext (self , msgctxt : str , msgid : str , msgid_plural : str , n : int ) -> str :
493
504
"""
@@ -509,7 +520,14 @@ def npgettext(self, msgctxt: str, msgid: str, msgid_plural: str, n: int) -> str:
509
520
str
510
521
The translated string.
511
522
"""
512
- return gettext .dnpgettext (self ._domain , msgctxt , msgid , msgid_plural , n )
523
+ # Python 3.7 or lower does not offer translations based on context.
524
+ # On these versions `npgettext` falls back to `ngettext`
525
+ if PY37_OR_LOWER :
526
+ translation = gettext .dngettext (self ._domain , msgid , msgid_plural , n )
527
+ else :
528
+ translation = gettext .dnpgettext (self ._domain , msgctxt , msgid , msgid_plural , n )
529
+
530
+ return translation
513
531
514
532
# Shorthands
515
533
def __ (self , msgid : str ) -> str :
@@ -566,7 +584,7 @@ def _p(self, msgctxt: str, msgid: str) -> str:
566
584
"""
567
585
return self .pgettext (msgctxt , msgid )
568
586
569
- def _np (self , msgctxt : str , msgid : str , msgid_plural : str , n : str ) -> str :
587
+ def _np (self , msgctxt : str , msgid : str , msgid_plural : str , n : int ) -> str :
570
588
"""
571
589
Shorthand for npgettext.
572
590
0 commit comments