def _do_change_language(request, lang_code, current_url): """change the language and returns redirect URL""" if lang_code and check_for_language(lang_code): # path is the locale-independent url path = strip_locale_path(current_url)[1] article_class = get_article_class() try: # get the translated slug of the current article # If switching from French to English and path is /fr/accueil/ # The next should be : /en/home/ # Get the article next_article = article_class.objects.get(slug=path.strip('/')) except article_class.DoesNotExist: next_article = None if hasattr(request, 'session'): request.session['django_language'] = lang_code activate(lang_code) if next_article: next_url = next_article.get_absolute_url() else: next_url = make_locale_path(path, lang_code) return next_url
def get_article_slug(*args, **kwargs): """slugify""" slug = reverse(*args, **kwargs) lang, slug = strip_locale_path(slug) return slug.strip('/')
def test_get_locale_article_no_prefix_no_trailing(self): """it should returns empty locale and path""" result = strip_locale_path('/home') self.assertEqual(len(result), 2) self.assertEqual(result[0], '') self.assertEqual(result[1], '/home')
def test_get_locale_article_no_trailing(self): """it should return lang and locale-independent-path""" result = strip_locale_path('/en/home') self.assertEqual(len(result), 2) self.assertEqual(result[0], 'en') self.assertEqual(result[1], '/home')