5
5
Localization utilities to find available language packs and packages with
6
6
localization data.
7
7
"""
8
+
8
9
from __future__ import annotations
9
10
10
11
import gettext
40
41
L10N_SCHEMA_NAME = "@jupyterlab/translation-extension:plugin"
41
42
PY37_OR_LOWER = sys .version_info [:2 ] <= (3 , 7 )
42
43
44
+ # Pseudo language locale for in-context translation
45
+ PSEUDO_LANGUAGE = "ach_UG"
46
+
43
47
_default_schema_context = "schema"
44
48
_default_settings_context = "settings"
45
49
_lab_i18n_config = "jupyter.lab.internationalization"
@@ -151,7 +155,9 @@ def is_valid_locale(locale_: str) -> bool:
151
155
- Brazilian German: "de_BR"
152
156
"""
153
157
# Add exception for Norwegian
154
- if locale_ == "no_NO" :
158
+ if locale_ in {
159
+ "no_NO" ,
160
+ }:
155
161
return True
156
162
157
163
valid = False
@@ -183,8 +189,11 @@ def get_display_name(locale_: str, display_locale: str = DEFAULT_LOCALE) -> str:
183
189
"""
184
190
locale_ = locale_ if is_valid_locale (locale_ ) else DEFAULT_LOCALE
185
191
display_locale = display_locale if is_valid_locale (display_locale ) else DEFAULT_LOCALE
186
- loc = babel .Locale .parse (locale_ )
187
- display_name = loc .get_display_name (display_locale )
192
+ try :
193
+ loc = babel .Locale .parse (locale_ )
194
+ display_name = loc .get_display_name (display_locale )
195
+ except babel .UnknownLocaleError :
196
+ display_name = display_locale
188
197
if display_name :
189
198
display_name = display_name [0 ].upper () + display_name [1 :]
190
199
return display_name # type:ignore[return-value]
@@ -312,21 +321,38 @@ def get_language_packs(display_locale: str = DEFAULT_LOCALE) -> tuple[dict, str]
312
321
else :
313
322
invalid_locales .append (locale_ )
314
323
315
- display_locale = display_locale if display_locale in valid_locales else DEFAULT_LOCALE
324
+ display_locale_ = display_locale if display_locale in valid_locales else DEFAULT_LOCALE
316
325
locales = {
317
326
DEFAULT_LOCALE : {
318
- "displayName" : get_display_name (DEFAULT_LOCALE , display_locale ),
327
+ "displayName" : (
328
+ get_display_name (DEFAULT_LOCALE , display_locale_ )
329
+ if display_locale != PSEUDO_LANGUAGE
330
+ else "Default"
331
+ ),
319
332
"nativeName" : get_display_name (DEFAULT_LOCALE , DEFAULT_LOCALE ),
320
333
}
321
334
}
322
335
for locale_ in valid_locales :
323
336
locales [locale_ ] = {
324
- "displayName" : get_display_name (locale_ , display_locale ),
337
+ "displayName" : get_display_name (locale_ , display_locale_ ),
325
338
"nativeName" : get_display_name (locale_ , locale_ ),
326
339
}
327
340
328
341
if invalid_locales :
329
- messages .append (f"The following locales are invalid: { invalid_locales } !" )
342
+ if PSEUDO_LANGUAGE in invalid_locales :
343
+ invalid_locales .remove (PSEUDO_LANGUAGE )
344
+ locales [PSEUDO_LANGUAGE ] = {
345
+ "displayName" : "Pseudo-language" ,
346
+ # Trick to ensure the proper language is selected in the language menu
347
+ "nativeName" : (
348
+ "to translate the UI"
349
+ if display_locale != PSEUDO_LANGUAGE
350
+ else "Pseudo-language"
351
+ ),
352
+ }
353
+ # Check again as the pseudo-language was maybe the only invalid locale
354
+ if invalid_locales :
355
+ messages .append (f"The following locales are invalid: { invalid_locales } !" )
330
356
331
357
return locales , "\n " .join (messages )
332
358
@@ -351,7 +377,11 @@ def get_language_pack(locale_: str) -> tuple:
351
377
found_packages_locales , message = get_installed_packages_locale (locale_ )
352
378
locale_data = {}
353
379
messages = message .split ("\n " )
354
- if not message and is_valid_locale (locale_ ) and locale_ in found_locales :
380
+ if (
381
+ not message
382
+ and (locale_ == PSEUDO_LANGUAGE or is_valid_locale (locale_ ))
383
+ and locale_ in found_locales
384
+ ):
355
385
path = found_locales [locale_ ]
356
386
for root , __ , files in os .walk (path , topdown = False ):
357
387
for name in files :
0 commit comments