Detecting user language with Django - django

I'm looking for a way to detect user language. I set my default language as 'en_US', and I translated my site for 'pt_BR'. How can I switch the language and show 'pt_BR' version for Brazilians and 'en_US' for the rest of the world?
I read these documentation links:
https://docs.djangoproject.com/en/dev/topics/http/sessions/
https://docs.djangoproject.com/en/1.7/topics/i18n/translation/
I believe that maybe I'll have to take this information from user cookies or browser preferences, but how can I do this?
Furthermore, how can I test different languages? I have to change my browser language? OS language? Use a proxy?

Every user's HTTP request contains in header parameter Accept-Language.
Example would be:
user_langs = request.META.get('HTTP_ACCEPT_LANGUAGE', ['en-US', ])

Try to add navigator.language to your post data and resolve it in your view.
http://www.w3schools.com/jsref/prop_nav_language.asp

Related

Django internationalization: prefix_default_language & language redirect

I'm adding internationalization to my Django project. The idea of how it should work is the following:
When a user enters "/" i.e. the "homepage" we try to get his/her
language preferences from our session. If it's not our default
language (EN), we redirect the user to the local version, like /ES/,
/RU/, /CN/, etc
If we have no such session data, we check Accept-Language and
redirect the user to the local version if we support such
If we don't support user's Accept-Language OR if it's EN - we don't
redirect user! we just show the "default language"
I.e. all "not default languages" should have their prefixes, but default one won't. Why the default language should have a prefix, right? )))
The issue is, I can't figure out how to set this logic. If I set prefix_default_language=False to disable the /EN/ prefix for default language, it also disables the "not default" redirect. I found a package that solves the problem: https://github.com/st4lk/django-solid-i18n-urls#behaviour settings.SOLID_I18N_USE_REDIRECTS = True but it doesn't work in Django 2+
I can't believe I'm the only one hating the default language prefixes )) Although I could not find any solution with both redirect and no prefix for the default language. Hope someone will advice something here 😅

How to set language for django-pagination?

As you can see, django-pagination has polish (pl) translations - https://github.com/ericflo/django-pagination/tree/master/pagination/locale but I dont know, how to set polish language for django-pagination? (default english)
This should happen automatically.
Check your django settings if USE_I18N is set to True and if your LANGUAGE_CODE is set to pl.
For further information take a look at the django localization page. You can find a more detailed documentation of how the translation in django works here.
There's also a list of language codes, I guess pl should be correct.
You can either change the language setting of your browser, which will send the appropriate headers with each request and trigger the translation to be used, or you can provide a language setting selection so the user can choose their language.
You can roll your own code to provide this interface or use django-user-accounts.
You also might want to check that you have the appropriate middleware installed as described in this documenation.

django internalization in urls? how to make urls like this: "en/articles" and "pt/artigos"...?

Hy!
I would need to make urls based on language.
Example:
If I had the language english and subcategory named "articles" then the url might be like this:
/en/articles/...
If the language were portuguese and subcategory already translated is "artigos" then the url will be:
/pt/artigos/...
How can I do it?
I must use the urls.py or some monkeypatches?
thanks
This features is already existing in the yet-to-be released version 1.4. You can read about it in the release note.
If your really need this feature, and no existing app feets your needs, you can still try to apply the corresponding patch yourself.
Django LocaleURL is a piece of middleware that does exactly this. The documentation can be found in the source, or online.
Edit: I read over the fact that you want to translate the url itself... I'm not aware of any piece of code that provides this. Perhaps you could extend the LocalURL middleware to take care of the translations for you. Say you have a regex match like (?P<articles>\w+), you could in the middleware determine which view you want to use. Something like the following mapping perhaps?
if article_slug in ['articles', 'artigos', 'article']:
article(request) # Call the article view
I've been using transurlvania with great success, it does exactly what you need and more, however i see that in the next Django release django-i18nurls will be included in django core so perhaps it would be better to learn that

How to pass language information from page to page?

If I have a multilingual site, what is the best way to pass information about language?
Right now the language is saved in cookies. That's convenient except that might be not good for search optimization, if search bots don't use cookies.
The other option would be specifying language in address, like exampel.com/?lang=de, but then you probably need to add ?lang=xx to every link on the page.
Is there a right way?
Better way is to maintain this info in session,
The other option would be specifying
language in address, like
exampel.com/?lang=de, but then you
probably need to add ?lang=xx to every
link on the page.
Is there a right way?
I would have created filter than parse each request and fetches the lang param and process accordingly.
Moreover I would recommend you to use following url pattern, and get the lang from filter
yourapp.com/en/welcome/
If you want all the content crawlable then you'd have to pass it in the URL. Either as a parameter http://mydomain.com/en/english-content or maybe have separate sites/subdomains http://english.mydomain.com/english-content
I would use Wikepedia's approach: Different URLs for different languages.
http://en.wikipedia.org for english
http://es.wikipedia.org for Spanish

URL Rewrite in DotNetNuke remove chunk of address (and read cookie?)

I am working on a DotNetNuke application using the iFinity URL Master module. (that may be irrelevant, as a solution may be platform independent)
What I have is a site with addresses based on language.
so
www.thesite.com/en/products/towels/redtowel
is the english version and
www.thesite.com/de/products/towels/redtowel
is the german version.
What I need to do is allow a user (who has already visited the site and set a cookie with their language) to be able to go to www.thesite.com/products/towels/redtowel and get to www.thesite.com/en/products/towels/redtowel if their cookie is set to english, and /de/products/towels/redtowel if it is set to german.
How would I do this?
if it was me and i didnt want to spend a lot of time programming I would look at something like this
http://www.snowcovered.com/snowcovered2/Default.aspx?tabid=242&PackageID=10059
then it could do a redirect based on the cookie - otherwise with iFinity I think you can do that sort of but not exactly. (I may be wrong on that - not a fan of iFinity url rewriter)