Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Translation outside the broswer

Received: by 10.58.254.39 with SMTP id af7mr5874794ved.41.1349883903196;
        Wed, 10 Oct 2012 08:45:03 -0700 (PDT)
X-BeenThere: django-users@googlegroups.com
Received: by 10.52.67.52 with SMTP id k20ls461525vdt.1.gmail; Wed, 10 Oct 2012
 08:44:37 -0700 (PDT)
Received: by 10.52.93.132 with SMTP id cu4mr3836233vdb.14.1349883877290;
        Wed, 10 Oct 2012 08:44:37 -0700 (PDT)
Date: Wed, 10 Oct 2012 08:44:36 -0700 (PDT)
From: Bastian <bastien.roche...@gmail.com>
To: django-users@googlegroups.com
Cc: tevans...@googlemail.com
Message-Id: <439ca719-aad3-4862-bec0-6716a30b6ca0@googlegroups.com>
In-Reply-To: <CAFHbX1+O2XvPi0R=T5HYjbgBoDJyUc7F03r0etSRDRe1W5qA5Q@mail.gmail.com>
References: <3bc9d496-da7e-42f9-b7c0-35b0aee61b07@googlegroups.com>
 <CAFHbX1+O2XvPi0R=T5HYjbgBoDJyUc7F03r0etSRDRe1W5qA5Q@mail.gmail.com>
Subject: Re: Translation outside the broswer
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_2468_12402330.1349883876958"

------=_Part_2468_12402330.1349883876958
Content-Type: multipart/alternative; 
	boundary="----=_Part_2469_5829348.1349883876958"

------=_Part_2469_5829348.1349883876958
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Ok this is all very clear now, and the docs are quite helpful too 
(https://docs.djangoproject.com/en/dev/topics/i18n/translation/#using-translations-outside-views-and-templates).
But now I wonder what is the best way to implement that?
I mean in my case I use Django to automatically discover the user's 
preferred language according to the browser preferences and if it fails 
there is also a button to choose the language. I can link that button to a 
field in the user profile model to remember what language they set for 
later use with translation.activate. But in case Django displays the right 
language for the user automatically that field will remain empty and that 
would be pretty counter intuitive for the user to set their preferred 
language with a button when it is already displayed correctly. What would 
be the right thing to do in that case? Is there a way to retrieve the 
latest language used in the user session? Or something similar? Thanks!


On Friday, October 5, 2012 12:19:38 PM UTC+2, Tom Evans wrote:
>
> On Fri, Oct 5, 2012 at 10:12 AM, Bastian <bastien....@gmail.com<javascript:>> 
> wrote: 
> > Hi, 
> > 
> > I understand quite well how translations and i18n work inside a browser 
> for 
> > Django but I'm not sure about the correct way to do it outside a 
> browser. I 
> > mean when sending a mail or a tweet. What should I use to get the 
> language 
> > of the user that is going to receive the mail or in case of a tweet the 
> > language of the user that I will send it on behalf of. And then how do I 
> ask 
> > Django to translate that? 
> > I could not find it in the docs, if it exists please point me to it. 
> > 
> > Cheers 
> > 
>
> You will need to have a mechanism for storing what the user's chosen 
> language is. Once you have that, simply do this: 
>
> from django.utils import translation 
>
> cur_language = translation.get_language() 
> translation.activate(get_lang_for_user(user)) 
> # send email, tweet, etc 
> translation.activate(cur_language) 
>
> You would need to define the 'get_lang_for_user' function. 
>
> Cheers 
>
> Tom 
>

------=_Part_2469_5829348.1349883876958
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Ok this is all very clear now, and the docs are quite helpful too (https://=
docs.djangoproject.com/en/dev/topics/i18n/translation/#using-translations-o=
utside-views-and-templates).<br>But now I wonder what is the best way to im=
plement that?<br>I mean in my case I use Django to automatically discover t=
he user's preferred language according to the browser preferences and if it=
 fails there is also a button to choose the language. I can link that butto=
n to a field in the user profile model to remember what language they set f=
or later use with translation.activate. But in case Django displays the rig=
ht language for the user automatically that field will remain empty and tha=
t would be pretty counter intuitive for the user to set their preferred lan=
guage with a button when it is already displayed correctly. What would be t=
he right thing to do in that case? Is there a way to retrieve the latest la=
nguage used in the user session? Or something similar? Thanks!<br><br><br>O=
n Friday, October 5, 2012 12:19:38 PM UTC+2, Tom Evans wrote:<blockquote cl=
ass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px =
#ccc solid;padding-left: 1ex;">On Fri, Oct 5, 2012 at 10:12 AM, Bastian &lt=
;<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"W-s_8SK=
pNvoJ">bastien....@gmail.com</a>&gt; wrote:
<br>&gt; Hi,
<br>&gt;
<br>&gt; I understand quite well how translations and i18n work inside a br=
owser for
<br>&gt; Django but I'm not sure about the correct way to do it outside a b=
rowser. I
<br>&gt; mean when sending a mail or a tweet. What should I use to get the =
language
<br>&gt; of the user that is going to receive the mail or in case of a twee=
t the
<br>&gt; language of the user that I will send it on behalf of. And then ho=
w do I ask
<br>&gt; Django to translate that?
<br>&gt; I could not find it in the docs, if it exists please point me to i=
t.
<br>&gt;
<br>&gt; Cheers
<br>&gt;
<br>
<br>You will need to have a mechanism for storing what the user's chosen
<br>language is. Once you have that, simply do this:
<br>
<br>from django.utils import translation
<br>
<br>cur_language =3D translation.get_language()
<br>translation.activate(get_lang_<wbr>for_user(user))
<br># send email, tweet, etc
<br>translation.activate(cur_<wbr>language)
<br>
<br>You would need to define the 'get_lang_for_user' function.
<br>
<br>Cheers
<br>
<br>Tom
<br></blockquote>
------=_Part_2469_5829348.1349883876958--

------=_Part_2468_12402330.1349883876958--