1

I'm trying to start of my new website in Django with a lot of settings which down the road will be tougher to implement. One of these things is website internationalization, but also url pattern internationalization.

What I am trying to achieve is having urls like this:

www.example.com/news                 [en] [news]
www.example.com/en/news              [en] [news]
www.example.com/no/nyheter           [no] [news]
www.example.com/en/top-articles      [en] [top articles]

and on every page I visit, I want the website navigation to have a dropdown menu, which contains website languages, and which language is currently selected. Similar to this:

<ul class="dropdown">
    <li><a href="/en/news" hreflang="en" class="active">English</a></li>
    <li><a href="/no/nyheter" hreflang="no" rel="alternate">Norsk</a></li>
</ul>

Now as for changing to another language, is there any way to see if the current page exists in the language the users can pick from?

If the current page does not exist in the language of choice, I want the user to be returned to the frontpage of the selected language. So the dropdown would look like this:

<ul class="dropdown">
    <li><a href="/en/top-articles" hreflang="en" class="active">English</a></li>
    <li><a href="/no" hreflang="no">Norsk</a></li>
</ul>
Gjert
  • 1,069
  • 1
  • 18
  • 48
  • Are you creating translation files for each resource or having the content for each resource persisted for each language in the database? – Oluwafemi Sule Jul 29 '17 at 17:17
  • @OluwafemiSule I'm thinking about having translation files, and for content, such as articles, I will create multiple in the database, each for every language. – Gjert Jul 29 '17 at 17:20

1 Answers1

1

One way I see you doing this is by setting django_language in the user session based on user selection.

Django will handle translation of any text that is specified using gettext_lazy. Your URLs will be generated using Django url.reverse function together with django_language and other arguments.

Determining if the current page exists in the language the users can pick from will boil down to your implementation.

For example, content models will have a language field make it easy to query for records whose language match django_language. If there is no match, a redirect can be made to language front page assuming that will always exist.

References:

http://django-book.readthedocs.io/en/latest/chapter19.html

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • Is there any way to back-track this through templates in Django? So lets say `get_url "en" as url` and if exists it will return the URL? If not, it returns false, and I can validate this in template to output language frontpage instead? Is this possible? I just read through the entired docs on internationalization, but I couldn't find a specific track-back option... as for validating certain urls, such as articles, I am aware that I have to create my own database lookup. – Gjert Jul 29 '17 at 18:21
  • You should consider writing a custom template tag if you'll like to backtrack urls inside of the template _to the url for the front page_. This is going to be really weird. What I recommend is backtracking in the view function or method. This way you can always redirect the user to the appropriate language front page. – Oluwafemi Sule Jul 29 '17 at 18:52
  • Good point, maybe I should do that instead. Do you know if gettext allows variables instead of strings? I have a shared header template and I need to pass a head_title variable for each page using that template. Looks cleaner with variables instead of strings when translating through the view of each page. – Gjert Jul 29 '17 at 18:57
  • 1
    Yes gettext allows for using variables. – Oluwafemi Sule Jul 29 '17 at 18:59
  • 1
    Great, I will try out the way you described :) – Gjert Jul 29 '17 at 19:02