Re: Generate different querysets depending of the website language

2016-03-30 Thread Mathieu Poussin
Hello.

I have been doing some tests, here are my results :

The Django default language (fallback) is in Spanish in all case

No IgnoreHTTPLanguageMiddleware
Browser having English has priority

URL specified language in Spanish /es/
-> Translated blocks in Spanish
-> Language related DB queries in English

URL specified language in English /en/
-> Translated blocks in English
-> Language related DB queries in English



No IgnoreHTTPLanguageMiddleware
Browser having Spanish has priority

URL specified language in Spanish /es/
-> Translated blocks in Spanish
-> Language related DB queries in Spanish

URL specified language in English /en/
-> Translated blocks in English
-> Language related DB queries in Spanish



IgnoreHTTPLanguageMiddleware enabled
Browser having Spanish has priority

URL specified language in Spanish /es/
-> Translated blocks in Spanish
-> Language related DB queries in Spanish

URL specified language in English /en/
-> Translated blocks in English
-> Language related DB queries in Spanish



IgnoreHTTPLanguageMiddleware enabled
Browser having English has priority

URL specified language in Spanish /es/
-> Translated blocks in Spanish
-> Language related DB queries in Spanish

URL specified language in English /en/
-> Translated blocks in English
-> Language related DB queries in Spanish



Basically, it looks like the block translations always use the language
specified in the URL, where my queries always use the language specified in
the HTTP request header... Is there something wrong with get_language()
that could cause this ? Is there a better way ?
So I suppose when I "silence" the HTTP request header language it fallbacks
to the default "Spanish" language.

Hmm, after some search, adding check_path=True to get_language_from_request
fix this :)

Thank you.

On 27 March 2016 at 15:46, Daniel Chimeno  wrote:

> Hello again,
> In that case, I would double-check the middleware process and settings in
> your project.
> One reason could be your are placing LocaleMiddleware after your
> ForceDefaultLanguage,
> so
> https://github.com/django/django/blob/master/django/middleware/locale.py#L26
> is setting the default language.
>
> Hope it helps.
>
> El sábado, 26 de marzo de 2016, 20:43:09 (UTC+1), Mathieu Poussin escribió:
>>
>> Hello,
>>
>> I have an issue, I am creating a website that will be available in many
>> languages, sharing the same database.
>> Most models have a "language" attribute that is the 2 letters from the
>> language code (en, es, fr, etc.).
>>
>> I am trying to find a way to show the correct content per language.
>>
>> I tried many things, creating a custom manager :
>>
>> from django.utils.translation import get_language, get_language_info
>> from django.db import models
>>
>> class PerLanguageManager(models.Manager):
>> def get_queryset(self):
>> if get_language():
>> return super(PerLanguageManager, self).get_queryset().filter(
>> language=get_language_info(get_language())['code'])
>> else:
>> return super(PerLanguageManager, self).get_queryset()
>>
>>
>> Or overriding get_queryset using another method : (The language is always
>> present in the url as /en/ or /es/)
>>
>> class RecipeIndexView(generic.ListView):
>> paginate_by = 10
>>
>> def get_queryset(self):
>> return
>> Recipe.objects.filter(language=get_language_from_request(self.request,
>> check_path=False))
>>
>> But nothing work, I always get the default configured language (even if
>> with the debug toolbar tell me the site is in another language, and all the
>> translations are correctly done in the language specified in the URL, I
>> always get the default language from the queries...)
>>
>> I'm using a specific middleware to ignore the language specified in the
>> browser to only use the language specified in the URL :
>> class ForceDefaultLanguageMiddleware(object):
>> """
>> Ignore Accept-Language HTTP headers
>>
>> This will force the I18N machinery to always choose
>> settings.LANGUAGE_CODE
>> as the default initial language, unless another one is set via
>> sessions or cookies
>>
>> Should be installed *before* any middleware that checks
>> request.META['HTTP_ACCEPT_LANGUAGE'],
>> namely django.middleware.locale.LocaleMiddleware
>> """
>> def process_request(self, request):
>> if 'HTTP_ACCEPT_LANGUAGE' in request.META:
>> del request.META['HTTP_ACCEPT_LANGUAGE']
>>
>>
>> Any idea of how to make this work ? What is the good way to do this ?
>>
>> Thank you.
>> Mathieu
>>
>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/unqwN8dQpo8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroup

Re: Generate different querysets depending of the website language

2016-03-27 Thread Daniel Chimeno
Hello again,
In that case, I would double-check the middleware process and settings in 
your project.
One reason could be your are placing LocaleMiddleware after your 
ForceDefaultLanguage,
so https://github.com/django/django/blob/master/django/middleware/locale.py#L26
is setting the default language.

Hope it helps.
 
El sábado, 26 de marzo de 2016, 20:43:09 (UTC+1), Mathieu Poussin escribió:
>
> Hello,
>
> I have an issue, I am creating a website that will be available in many 
> languages, sharing the same database.
> Most models have a "language" attribute that is the 2 letters from the 
> language code (en, es, fr, etc.).
>
> I am trying to find a way to show the correct content per language.
>
> I tried many things, creating a custom manager :
>
> from django.utils.translation import get_language, get_language_info
> from django.db import models
>
> class PerLanguageManager(models.Manager):
> def get_queryset(self):
> if get_language():
> return super(PerLanguageManager, self).get_queryset().filter(
> language=get_language_info(get_language())['code'])
> else:
> return super(PerLanguageManager, self).get_queryset()
>
>
> Or overriding get_queryset using another method : (The language is always 
> present in the url as /en/ or /es/) 
>
> class RecipeIndexView(generic.ListView):
> paginate_by = 10
>
> def get_queryset(self):
> return 
> Recipe.objects.filter(language=get_language_from_request(self.request, 
> check_path=False))
>
> But nothing work, I always get the default configured language (even if 
> with the debug toolbar tell me the site is in another language, and all the 
> translations are correctly done in the language specified in the URL, I 
> always get the default language from the queries...)
>
> I'm using a specific middleware to ignore the language specified in the 
> browser to only use the language specified in the URL :
> class ForceDefaultLanguageMiddleware(object):
> """
> Ignore Accept-Language HTTP headers
>
> This will force the I18N machinery to always choose 
> settings.LANGUAGE_CODE
> as the default initial language, unless another one is set via 
> sessions or cookies
>
> Should be installed *before* any middleware that checks 
> request.META['HTTP_ACCEPT_LANGUAGE'],
> namely django.middleware.locale.LocaleMiddleware
> """
> def process_request(self, request):
> if 'HTTP_ACCEPT_LANGUAGE' in request.META:
> del request.META['HTTP_ACCEPT_LANGUAGE']
>
>
> Any idea of how to make this work ? What is the good way to do this ?
>
> Thank you.
> Mathieu
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/66d4859d-d61b-46ca-b3a0-3232da1c4786%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Generate different querysets depending of the website language

2016-03-27 Thread Mathieu Poussin
Thanks for the tips about the translation module, in my case, I think it 
would be better to keep managing it the way I do as each models have 
complex relation with other models that are only bound to a specific 
language of the model (Like tags, comments, steps (This is about recipes), 
etc.
I also suppose that would break stuff like Reversion or Haystack (I prefer 
to keep my models as simple and standard as possible).

The weird thing is that, when I switch to a language, everything work fine, 
except dynamic queryset, exemple :
I go to "/en/r/" (The main recipe list), I expect to only see the english 
recipes, but I get the Spanish ones in all cases (This is the default 
language), but I can see that everything else is translated correctly to 
english, and the HTTP header response tell me the page is in English, so I 
think the problem is really in my queryset, why is it always selecting the 
default language just for the queryset when everything else is translated 
correctly ?

Thanks.
Mathieu

On Sunday, 27 March 2016 04:49:20 UTC+2, Daniel Chimeno wrote:
>
> Hello,
>
> If you are able to change the schema I would suggest you to use 
> modeltranslation [https://github.com/deschler/django-modeltranslation],
> also the source code of that app can help you in order to take an specific 
> approach to your project.
>
> About the URL's, if you use i18urls, it has priority over the http headers 
> [
> https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#module-django.conf.urls.i18n]
>  
> (Not confirmed, but was true last time I checked it)
>
>
> Hope it helps.
>
> El sábado, 26 de marzo de 2016, 20:43:09 (UTC+1), Mathieu Poussin escribió:
>>
>> Hello,
>>
>> I have an issue, I am creating a website that will be available in many 
>> languages, sharing the same database.
>> Most models have a "language" attribute that is the 2 letters from the 
>> language code (en, es, fr, etc.).
>>
>> I am trying to find a way to show the correct content per language.
>>
>> I tried many things, creating a custom manager :
>>
>> from django.utils.translation import get_language, get_language_info
>> from django.db import models
>>
>> class PerLanguageManager(models.Manager):
>> def get_queryset(self):
>> if get_language():
>> return super(PerLanguageManager, self).get_queryset().filter(
>> language=get_language_info(get_language())['code'])
>> else:
>> return super(PerLanguageManager, self).get_queryset()
>>
>>
>> Or overriding get_queryset using another method : (The language is always 
>> present in the url as /en/ or /es/) 
>>
>> class RecipeIndexView(generic.ListView):
>> paginate_by = 10
>>
>> def get_queryset(self):
>> return 
>> Recipe.objects.filter(language=get_language_from_request(self.request, 
>> check_path=False))
>>
>> But nothing work, I always get the default configured language (even if 
>> with the debug toolbar tell me the site is in another language, and all the 
>> translations are correctly done in the language specified in the URL, I 
>> always get the default language from the queries...)
>>
>> I'm using a specific middleware to ignore the language specified in the 
>> browser to only use the language specified in the URL :
>> class ForceDefaultLanguageMiddleware(object):
>> """
>> Ignore Accept-Language HTTP headers
>>
>> This will force the I18N machinery to always choose 
>> settings.LANGUAGE_CODE
>> as the default initial language, unless another one is set via 
>> sessions or cookies
>>
>> Should be installed *before* any middleware that checks 
>> request.META['HTTP_ACCEPT_LANGUAGE'],
>> namely django.middleware.locale.LocaleMiddleware
>> """
>> def process_request(self, request):
>> if 'HTTP_ACCEPT_LANGUAGE' in request.META:
>> del request.META['HTTP_ACCEPT_LANGUAGE']
>>
>>
>> Any idea of how to make this work ? What is the good way to do this ?
>>
>> Thank you.
>> Mathieu
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/dbb9dff6-c89b-40ca-9277-f6f24938eaa3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Generate different querysets depending of the website language

2016-03-26 Thread Daniel Chimeno
Hello,

If you are able to change the schema I would suggest you to use 
modeltranslation [https://github.com/deschler/django-modeltranslation],
also the source code of that app can help you in order to take an specific 
approach to your project.

About the URL's, if you use i18urls, it has priority over the http headers 
[https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#module-django.conf.urls.i18n]
 
(Not confirmed, but was true last time I checked it)


Hope it helps.

El sábado, 26 de marzo de 2016, 20:43:09 (UTC+1), Mathieu Poussin escribió:
>
> Hello,
>
> I have an issue, I am creating a website that will be available in many 
> languages, sharing the same database.
> Most models have a "language" attribute that is the 2 letters from the 
> language code (en, es, fr, etc.).
>
> I am trying to find a way to show the correct content per language.
>
> I tried many things, creating a custom manager :
>
> from django.utils.translation import get_language, get_language_info
> from django.db import models
>
> class PerLanguageManager(models.Manager):
> def get_queryset(self):
> if get_language():
> return super(PerLanguageManager, self).get_queryset().filter(
> language=get_language_info(get_language())['code'])
> else:
> return super(PerLanguageManager, self).get_queryset()
>
>
> Or overriding get_queryset using another method : (The language is always 
> present in the url as /en/ or /es/) 
>
> class RecipeIndexView(generic.ListView):
> paginate_by = 10
>
> def get_queryset(self):
> return 
> Recipe.objects.filter(language=get_language_from_request(self.request, 
> check_path=False))
>
> But nothing work, I always get the default configured language (even if 
> with the debug toolbar tell me the site is in another language, and all the 
> translations are correctly done in the language specified in the URL, I 
> always get the default language from the queries...)
>
> I'm using a specific middleware to ignore the language specified in the 
> browser to only use the language specified in the URL :
> class ForceDefaultLanguageMiddleware(object):
> """
> Ignore Accept-Language HTTP headers
>
> This will force the I18N machinery to always choose 
> settings.LANGUAGE_CODE
> as the default initial language, unless another one is set via 
> sessions or cookies
>
> Should be installed *before* any middleware that checks 
> request.META['HTTP_ACCEPT_LANGUAGE'],
> namely django.middleware.locale.LocaleMiddleware
> """
> def process_request(self, request):
> if 'HTTP_ACCEPT_LANGUAGE' in request.META:
> del request.META['HTTP_ACCEPT_LANGUAGE']
>
>
> Any idea of how to make this work ? What is the good way to do this ?
>
> Thank you.
> Mathieu
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d6c35903-a95b-4117-bd75-30966ed56fd8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Generate different querysets depending of the website language

2016-03-26 Thread Mathieu Poussin
Hello,

I have an issue, I am creating a website that will be available in many 
languages, sharing the same database.
Most models have a "language" attribute that is the 2 letters from the 
language code (en, es, fr, etc.).

I am trying to find a way to show the correct content per language.

I tried many things, creating a custom manager :

from django.utils.translation import get_language, get_language_info
from django.db import models

class PerLanguageManager(models.Manager):
def get_queryset(self):
if get_language():
return super(PerLanguageManager, self).get_queryset().filter(
language=get_language_info(get_language())['code'])
else:
return super(PerLanguageManager, self).get_queryset()


Or overriding get_queryset using another method : (The language is always 
present in the url as /en/ or /es/) 

class RecipeIndexView(generic.ListView):
paginate_by = 10

def get_queryset(self):
return 
Recipe.objects.filter(language=get_language_from_request(self.request, 
check_path=False))

But nothing work, I always get the default configured language (even if 
with the debug toolbar tell me the site is in another language, and all the 
translations are correctly done in the language specified in the URL, I 
always get the default language from the queries...)

I'm using a specific middleware to ignore the language specified in the 
browser to only use the language specified in the URL :
class ForceDefaultLanguageMiddleware(object):
"""
Ignore Accept-Language HTTP headers

This will force the I18N machinery to always choose 
settings.LANGUAGE_CODE
as the default initial language, unless another one is set via sessions 
or cookies

Should be installed *before* any middleware that checks 
request.META['HTTP_ACCEPT_LANGUAGE'],
namely django.middleware.locale.LocaleMiddleware
"""
def process_request(self, request):
if 'HTTP_ACCEPT_LANGUAGE' in request.META:
del request.META['HTTP_ACCEPT_LANGUAGE']


Any idea of how to make this work ? What is the good way to do this ?

Thank you.
Mathieu

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5988f3dc-1a46-4638-bee8-ab367e9eed33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.