Return the language ID to use for translations The language ID is a code like: en, en_US, nds, C There may be an underscore or no underscore. The first part may contain two or three letters. There will not be a dot like `en_US.UTF-8`.
()
| 301 | |
| 302 | |
| 303 | def get_active_language_code(): |
| 304 | """Return the language ID to use for translations |
| 305 | |
| 306 | The language ID is a code like: en, en_US, nds, C |
| 307 | |
| 308 | There may be an underscore or no underscore. The first part may |
| 309 | contain two or three letters. |
| 310 | |
| 311 | There will not be a dot like `en_US.UTF-8`. |
| 312 | """ |
| 313 | try: |
| 314 | from bleachbit.Options import options |
| 315 | except ImportError: |
| 316 | logger.error("Failed to get language options") |
| 317 | else: |
| 318 | if not options.get('auto_detect_lang') and options.has_option('forced_language') and options.get('forced_language'): |
| 319 | return options.get('forced_language') |
| 320 | import locale |
| 321 | # locale.getdefaultlocale() will be removed in Python 3.15, so |
| 322 | # use getlocale() instead. |
| 323 | # However, on Windows, getlocale() may return values like |
| 324 | # 'English_United States' instead of RFC1766 codes. |
| 325 | if os.name == 'nt': |
| 326 | import ctypes |
| 327 | kernel32 = ctypes.windll.kernel32 |
| 328 | lcid = kernel32.GetUserDefaultLCID() |
| 329 | # Convert Windows LCID (e.g., 1033) to RFC1766 (e.g., en-US). |
| 330 | user_locale = locale.windows_locale.get(lcid, '') |
| 331 | else: |
| 332 | user_locale = locale.getlocale()[0] |
| 333 | |
| 334 | if not user_locale: |
| 335 | user_locale = 'C' |
| 336 | logger.warning("no default locale found. Assuming '%s'", user_locale) |
| 337 | |
| 338 | if '.' in user_locale: |
| 339 | # This should never happen. |
| 340 | logger.warning('locale contains a dot: %s', user_locale) |
| 341 | user_locale = user_locale.split('.')[0] |
| 342 | |
| 343 | assert isinstance(user_locale, str) |
| 344 | assert len( |
| 345 | user_locale) >= 2 or user_locale == 'C', f"user_locale: {user_locale}" |
| 346 | |
| 347 | return user_locale |
| 348 | |
| 349 | |
| 350 | def setup_translation(): |