KeyError: Unknown Language Code 'iw' in get_language_info() but check_for_language() Returns True

I encountered an inconsistency in Django’s translation system while working with multilingual support. Specifically, when calling get_language_info('iw'), Django raises a KeyError: unknown language code iw. However, when checking the same language code using check_for_language('iw'), it returns True. This behavior suggests a potential bug or inconsistency in language code handling.

Steps to Reproduce:

  1. Ensure Django is installed and configured for translations.

  2. Add multiple language codes to LANGUAGES in settings.py.

  3. Run the following Python code in the Django shell:

    from django.utils.translation import get_language_info, check_for_language
    
    print(check_for_language('iw'))  # Returns True
    print(get_language_info('iw'))   # Raises KeyError
    

Expected Behavior:
Both get_language_info('iw') and check_for_language('iw') should be consistent. Since check_for_language('iw') returns True, get_language_info('iw') should return relevant language metadata instead of raising a KeyError.

Actual Behavior:

  • check_for_language('iw') returns True, indicating that ‘iw’ is a recognized language code.
  • get_language_info('iw') raises a KeyError, suggesting the language code is not recognized.

Environment Details:

  • Django Version: Django 5.0
  • Python Version: Python 3.12.0
  • Operating System: windows

Additional Notes:

  • The ISO 639-1 standard previously used ‘iw’ for Hebrew, but it has since been replaced by ‘he’.
  • Django may be internally using ‘he’ instead of ‘iw’, causing this mismatch.
  • If Django officially supports ‘iw’, get_language_info() should handle it correctly.

Suggested Fix:

  • Ensure that get_language_info() recognizes ‘iw’ as an alias for ‘he’.
  • If ‘iw’ is deprecated, check_for_language() should return False for consistency.

Those functions are doing different things:

check_for_language() is checking if there is at least one <lang>.po file in Django or any other installed app.

get_language_info() tries to return a key in Django’s LANG_INFO structure (django/django/conf/locale/__init__.py at main · django/django · GitHub).

In your case, check_for_language('iw') is only returning True because one of your installed apps has an iw.po file. You should report a bug against that package asking for a file rename.

Ok, thanks for you are clarification.