Repository: django-cms/django-cms

Issue:
cms.utils.i18n.get_fallback_languages may fail if LANGUAGES has more languages than CMS_LANGUAGES
Reported via IRC.

Use case: Show the admin interface in English (for users that configured their browser to English), but CMS pages should only be in German or French. 

In the use case above, settings might be something like:

``` python
LANGUAGES = [
    ('en', 'English'),
    ('de', 'Deutsch'),
    ('fr', 'French'),
]

CMS_LANGUAGES = {
    1: [
        {
            'code': 'de',
            'name': gettext('Deutsch'),
            'public': True,
        },
        {
            'code': 'fr',
            'name': gettext('French'),
            'fallbacks': ['de',],
            'public': False,
        },
    ],
    'default': {
        'fallbacks': ['de',],
        'redirect_on_fallback':True,
        'public': False,
        'hide_untranslated': False,
    }
}
```

`'en'` is in `LANGUAGES` but not in `CMS_LANGUAGES`.

Now if `cms.utils.i18n.get_fallback_languages` is called with `'en'` as argument (that happens for example if you try to add a page in the admin with the admin UI in English, as the add view tries to log the page change in the current active language, not language of the page. This triggers a call to `get_fallback_languages` with English as the argument) it raises a `LanguageError` as `'en'` is not available to the CMS.

cms.utils.i18n.get_fallback_languages may fail if LANGUAGES has more languages than CMS_LANGUAGES
Reported via IRC.

Use case: Show the admin interface in English (for users that configured their browser to English), but CMS pages should only be in German or French. 

In the use case above, settings might be something like:

``` python
LANGUAGES = [
    ('en', 'English'),
    ('de', 'Deutsch'),
    ('fr', 'French'),
]

CMS_LANGUAGES = {
    1: [
        {
            'code': 'de',
            'name': gettext('Deutsch'),
            'public': True,
        },
        {
            'code': 'fr',
            'name': gettext('French'),
            'fallbacks': ['de',],
            'public': False,
        },
    ],
    'default': {
        'fallbacks': ['de',],
        'redirect_on_fallback':True,
        'public': False,
        'hide_untranslated': False,
    }
}
```

`'en'` is in `LANGUAGES` but not in `CMS_LANGUAGES`.

Now if `cms.utils.i18n.get_fallback_languages` is called with `'en'` as argument (that happens for example if you try to add a page in the admin with the admin UI in English, as the add view tries to log the page change in the current active language, not language of the page. This triggers a call to `get_fallback_languages` with English as the argument) it raises a `LanguageError` as `'en'` is not available to the CMS.

cms.utils.i18n.get_fallback_languages may fail if LANGUAGES has more languages than CMS_LANGUAGES
Reported via IRC.

Use case: Show the admin interface in English (for users that configured their browser to English), but CMS pages should only be in German or French. 

In the use case above, settings might be something like:

``` python
LANGUAGES = [
    ('en', 'English'),
    ('de', 'Deutsch'),
    ('fr', 'French'),
]

CMS_LANGUAGES = {
    1: [
        {
            'code': 'de',
            'name': gettext('Deutsch'),
            'public': True,
        },
        {
            'code': 'fr',
            'name': gettext('French'),
            'fallbacks': ['de',],
            'public': False,
        },
    ],
    'default': {
        'fallbacks': ['de',],
        'redirect_on_fallback':True,
        'public': False,
        'hide_untranslated': False,
    }
}
```

`'en'` is in `LANGUAGES` but not in `CMS_LANGUAGES`.

Now if `cms.utils.i18n.get_fallback_languages` is called with `'en'` as argument (that happens for example if you try to add a page in the admin with the admin UI in English, as the add view tries to log the page change in the current active language, not language of the page. This triggers a call to `get_fallback_languages` with English as the argument) it raises a `LanguageError` as `'en'` is not available to the CMS.



Discussion:

Resolving patch:
diff --git a/cms/utils/i18n.py b/cms/utils/i18n.py
--- a/cms/utils/i18n.py
+++ b/cms/utils/i18n.py
@@ -162,7 +162,10 @@ def get_fallback_languages(language, site_id=None):
     """
     returns a list of fallback languages for the given language
     """
-    language = get_language_object(language, site_id)
+    try:
+        language = get_language_object(language, site_id)
+    except LanguageError:
+        language = get_languages(site_id)[0]
     return language.get('fallbacks', [])
 
 


Test patch:
diff --git a/cms/tests/i18n.py b/cms/tests/i18n.py
--- a/cms/tests/i18n.py
+++ b/cms/tests/i18n.py
@@ -1,5 +1,7 @@
 from cms.test_utils.testcases import SettingsOverrideTestCase
 from cms.utils import i18n
+from cms.utils.i18n import get_fallback_languages
+
 
 class TestLanguages(SettingsOverrideTestCase):
 
@@ -269,3 +271,36 @@ def test_get_languages_undefined_site(self):
         for lang in result:
             self.assertEqual(lang['public'], True)
             self.assertEqual(lang['hide_untranslated'], False)
+
+
+class TestLanguagesNotInCMSLanguages(SettingsOverrideTestCase):
+    settings_overrides = {
+        'LANGUAGE_CODE': 'en',
+        'LANGUAGES': [
+            ('en', 'English'),
+            ('de', 'German'),
+            ('fr', 'French')
+        ],
+        'CMS_LANGUAGES': {
+            1: [
+                {
+                    'code': 'de',
+                    'name': 'German',
+                    'public': True,
+                },
+                {
+                    'code': 'fr',
+                    'name': 'French',
+                    'public': True
+                }
+            ],
+            'default': {
+                'fallbacks': ['de', 'fr'],
+            }
+        },
+        'SITE_ID': 1,
+    }
+
+    def test_get_fallback_languages(self):
+        languages = get_fallback_languages('en', 1)
+        self.assertEqual(languages, ['de', 'fr'])