Skip to content

Commit 27cead5

Browse files
krassowskigoanpeca
andauthored
Fallback to context-less translation on Python 3.7 (#213)
and fix some typing issues. Co-authored-by: goanpeca <[email protected]> Co-authored-by: goanpeca <[email protected]>
1 parent 3c905c1 commit 27cead5

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

jupyterlab_server/translation_utils.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import babel
1717
import entrypoints
1818
from packaging.version import parse as parse_version
19+
from typing import Tuple
1920

2021
# Entry points
2122
JUPYTERLAB_LANGUAGEPACK_ENTRY = "jupyterlab.languagepack"
@@ -27,6 +28,7 @@
2728
LC_MESSAGES_DIR = "LC_MESSAGES"
2829
DEFAULT_DOMAIN = "jupyterlab"
2930
L10N_SCHEMA_NAME = "@jupyterlab/translation-extension:plugin"
31+
PY37_OR_LOWER = sys.version_info[:2] <= (3, 7)
3032

3133
_default_schema_context = "schema"
3234
_default_settings_context = "settings"
@@ -247,15 +249,17 @@ def merge_locale_data(language_pack_locale_data, package_locale_data):
247249
return result
248250

249251

250-
def get_installed_packages_locale(locale: str) -> dict:
252+
def get_installed_packages_locale(locale: str) -> Tuple[dict, str]:
251253
"""
252254
Get all jupyterlab extensions installed that contain locale data.
253255
254256
Returns
255257
-------
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, ...}
259263
260264
Examples
261265
--------
@@ -298,7 +302,7 @@ def get_installed_packages_locale(locale: str) -> dict:
298302

299303
# --- API
300304
# ----------------------------------------------------------------------------
301-
def get_language_packs(display_locale: str = DEFAULT_LOCALE) -> tuple:
305+
def get_language_packs(display_locale: str = DEFAULT_LOCALE) -> Tuple[dict, str]:
302306
"""
303307
Return the available language packs installed in the system.
304308
@@ -487,7 +491,14 @@ def pgettext(self, msgctxt: str, msgid: str) -> str:
487491
str
488492
The translated string.
489493
"""
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
491502

492503
def npgettext(self, msgctxt: str, msgid: str, msgid_plural: str, n: int) -> str:
493504
"""
@@ -509,7 +520,14 @@ def npgettext(self, msgctxt: str, msgid: str, msgid_plural: str, n: int) -> str:
509520
str
510521
The translated string.
511522
"""
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
513531

514532
# Shorthands
515533
def __(self, msgid: str) -> str:
@@ -566,7 +584,7 @@ def _p(self, msgctxt: str, msgid: str) -> str:
566584
"""
567585
return self.pgettext(msgctxt, msgid)
568586

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:
570588
"""
571589
Shorthand for npgettext.
572590

0 commit comments

Comments
 (0)