Exemplo n.º 1
0
def get_language_from_request(request, current_page=None):
    """
    Return the most obvious language according the request
    """
    language = request.REQUEST.get('language', None)
    site_id = current_page.site_id if current_page else None
    if language:
        language = get_language_code(language)
        if not language in get_language_list(site_id):
            language = None
    if language is None:
        language = get_language_code(getattr(request, 'LANGUAGE_CODE', None))
    if language:
        if not language in get_language_list(site_id):
            language = None

    if language is None and current_page:
        # in last resort, get the first language available in the page
        languages = current_page.get_languages()

        if len(languages) > 0:
            language = languages[0]

    if language is None:
        # language must be defined in CMS_LANGUAGES, so check first if there
        # is any language with LANGUAGE_CODE, otherwise try to split it and find
        # best match
        language = get_default_language(site_id=site_id)

    return language
Exemplo n.º 2
0
def get_language_from_request(request, current_page=None):
    """
    Return the most obvious language according the request
    """
    language = request.REQUEST.get('language', None)
    site_id = current_page.site_id if current_page else None
    if language:
        language = get_language_code(language)
        if not language in get_language_list(site_id):
            language = None
    if language is None:
        language = get_language_code(getattr(request, 'LANGUAGE_CODE', None))
    if language:
        if not language in get_language_list(site_id):
            language = None

    if language is None and current_page:
        # in last resort, get the first language available in the page
        languages = current_page.get_languages()

        if len(languages) > 0:
            language = languages[0]

    if language is None:
        # language must be defined in CMS_LANGUAGES, so check first if there
        # is any language with LANGUAGE_CODE, otherwise try to split it and find
        # best match
        language = get_default_language(site_id=site_id)

    return language
Exemplo n.º 3
0
 def test_get_languages_default_site(self):
     result = i18n.get_languages()
     self.assertEqual(2, len(result))
     lang = result[0]
     self.assertEqual(lang['code'], 'en')
     self.assertEqual(i18n.get_language_code(lang['code']), 'en')
     self.assertEqual(lang['public'], True)
     lang = result[1]
     self.assertEqual(lang['code'], 'fr')
     self.assertEqual(i18n.get_language_code(lang['code']), 'fr')
     self.assertEqual(lang['public'], False)
Exemplo n.º 4
0
 def test_get_languages_defined_site(self):
     result = i18n.get_languages(1)
     self.assertEqual(2, len(result))
     lang = result[0]
     self.assertEqual(lang['code'], 'en')
     self.assertEqual(i18n.get_language_code(lang['code']), 'en')
     self.assertEqual(lang['public'], True)
     lang = result[1]
     self.assertEqual(lang['code'], 'fr')
     self.assertEqual(i18n.get_language_code(lang['code']), 'fr')
     self.assertEqual(lang['public'], False)
Exemplo n.º 5
0
 def test_get_languages_defined_site(self):
     result = i18n.get_languages(1)
     self.assertEqual(2, len(result))
     lang = result[0]
     self.assertEqual(lang["code"], "en")
     self.assertEqual(i18n.get_language_code(lang["code"]), "en")
     self.assertEqual(lang["public"], True)
     lang = result[1]
     self.assertEqual(lang["code"], "fr")
     self.assertEqual(i18n.get_language_code(lang["code"]), "fr")
     self.assertEqual(lang["public"], False)
Exemplo n.º 6
0
 def test_get_languages_undefined_site(self):
     result = i18n.get_languages(66)
     self.assertEqual(4, len(result))
     self.assertEqual(result[0]['code'], 'fr')
     self.assertEqual(i18n.get_language_code(result[0]['code']), 'fr')
     self.assertEqual(result[1]['code'], 'en')
     self.assertEqual(i18n.get_language_code(result[1]['code']), 'en')
     self.assertEqual(result[2]['code'], 'de')
     self.assertEqual(i18n.get_language_code(result[2]['code']), 'de')
     self.assertEqual(result[3]['code'], 'es')
     self.assertEqual(i18n.get_language_code(result[3]['code']), 'es')
     for lang in result:
         self.assertEqual(lang['public'], True)
         self.assertEqual(lang['hide_untranslated'], False)
Exemplo n.º 7
0
 def test_get_languages_undefined_site(self):
     result = i18n.get_languages(66)
     self.assertEqual(4, len(result))
     self.assertEqual(result[0]['code'], 'fr')
     self.assertEqual(i18n.get_language_code(result[0]['code']), 'fr')
     self.assertEqual(result[1]['code'], 'en')
     self.assertEqual(i18n.get_language_code(result[1]['code']), 'en')
     self.assertEqual(result[2]['code'], 'de')
     self.assertEqual(i18n.get_language_code(result[2]['code']), 'de')
     self.assertEqual(result[3]['code'], 'es')
     self.assertEqual(i18n.get_language_code(result[3]['code']), 'es')
     for lang in result:
         self.assertEqual(lang['public'], True)
         self.assertEqual(lang['hide_untranslated'], False)
Exemplo n.º 8
0
 def test_get_languages_undefined_site(self):
     result = i18n.get_languages(66)
     self.assertEqual(4, len(result))
     self.assertEqual(result[0]["code"], "fr")
     self.assertEqual(i18n.get_language_code(result[0]["code"]), "fr")
     self.assertEqual(result[1]["code"], "en")
     self.assertEqual(i18n.get_language_code(result[1]["code"]), "en")
     self.assertEqual(result[2]["code"], "de")
     self.assertEqual(i18n.get_language_code(result[2]["code"]), "de")
     self.assertEqual(result[3]["code"], "es")
     self.assertEqual(i18n.get_language_code(result[3]["code"]), "es")
     for lang in result:
         self.assertEqual(lang["public"], True)
         self.assertEqual(lang["hide_untranslated"], True)
Exemplo n.º 9
0
def alias_from_language(language):
    """
    Returns alias if alias is a valid language.
    """
    language = get_language_code(language)

    if language == settings.ALDRYN_SEARCH_DEFAULT_LANGUAGE:
        return DEFAULT_ALIAS
    return language
Exemplo n.º 10
0
def alias_from_language(language):
    """
    Returns alias if alias is a valid language.
    """
    language = get_language_code(language)

    if language == settings.ALDRYN_SEARCH_DEFAULT_LANGUAGE:
        return DEFAULT_ALIAS
    return language
Exemplo n.º 11
0
    def _get_operation_language(self, request):
        # Unfortunately the ?language GET query
        # has a special meaning on the CMS.
        # It allows users to see another language while maintaining
        # the same url. This complicates language detection.
        site = get_current_site(request)
        parsed_url = urlparse(request.GET['cms_path'])
        queries = dict(parse_qsl(parsed_url.query))
        language = queries.get('language')

        if not language:
            language = translation.get_language_from_path(parsed_url.path)
        return get_language_code(language, site_id=site.pk)
Exemplo n.º 12
0
 def test_language_code(self):
     self.assertEqual(i18n.get_language_code('en'), 'en')
     self.assertEqual(i18n.get_current_language(), 'en')
Exemplo n.º 13
0
 def __unicode__(self):
     return self.extended_object.get_title(
         get_language_code(get_language()))
Exemplo n.º 14
0
 def test_language_code(self):
     self.assertEqual(i18n.get_language_code("en"), "en")
     self.assertEqual(i18n.get_current_language(), "en")
Exemplo n.º 15
0
 def test_language_code(self):
     self.assertEqual(i18n.get_language_code(self.settings_overrides['LANGUAGE_CODE']), 'en')
     self.assertEqual(i18n.get_current_language(), 'en')
Exemplo n.º 16
0
 def test_language_code(self):
     self.assertEqual(i18n.get_language_code('en'), 'en')
     self.assertEqual(i18n.get_current_language(), 'en')
Exemplo n.º 17
0
 def test_language_code(self):
     self.assertEqual(
         i18n.get_language_code(self.settings_overrides['LANGUAGE_CODE']),
         'en')
     self.assertEqual(i18n.get_current_language(), 'en')