Skip to content

Fix locale.Error: unsupported locale setting in minimal environments#18

Merged
wasertech merged 2 commits intomainfrom
copilot/fix-17
Aug 28, 2025
Merged

Fix locale.Error: unsupported locale setting in minimal environments#18
wasertech merged 2 commits intomainfrom
copilot/fix-17

Conversation

Copy link
Contributor

Copilot AI commented Aug 28, 2025

This PR fixes a crash that occurs when running the translator in minimal environments (like Makefiles, containers, or systems with incomplete locale configurations) where locale.setlocale(locale.LC_ALL, '') fails with locale.Error: unsupported locale setting.

Problem

The issue manifested as:

Traceback (most recent call last):
  File "/home/waser/Projets/DFI/backend/.venv/bin/translate", line 5, in <module>
    from translator.main import main
  File "/home/waser/Projets/DFI/backend/.venv/lib/python3.13/site-packages/translator/main.py", line 21, in <module>
    locale.setlocale(locale.LC_ALL, '')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/locale.py", line 615, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

This prevented the translator from running in environments where locale settings are not properly configured or supported.

Solution

Wrapped the locale.setlocale(locale.LC_ALL, '') call in a try-except block to gracefully handle locale.Error exceptions. The locale setting is used for convenience/formatting but is not critical to the core translation functionality, as evidenced by the get_sys_lang_format() function already having its own fallback logic.

Changes

  • Added error handling around locale setting in translator/main.py
  • Maintains backward compatibility - no behavior change in working environments
  • Prevents crashes in minimal environments while preserving all functionality
  • Added clear comments explaining the error handling rationale

Testing

Verified the fix handles various problematic environments:

  • ✅ Normal environments: No change in behavior
  • ✅ Empty locale variables: Continues gracefully
  • ✅ Invalid locale settings: No longer crashes
  • ✅ Maintains all existing functionality

Fixes #17.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: wasertech <79070834+wasertech@users.noreply.github.com>
Copilot AI changed the title [WIP] locale.Error: unsupported locale setting Fix locale.Error: unsupported locale setting in minimal environments Aug 28, 2025
Copilot AI requested a review from wasertech August 28, 2025 17:29
Copy link
Owner

@wasertech wasertech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wasertech wasertech marked this pull request as ready for review August 28, 2025 17:57
@wasertech wasertech merged commit 47759b9 into main Aug 28, 2025
@wasertech wasertech deleted the copilot/fix-17 branch August 28, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

locale.Error: unsupported locale setting

2 participants