Hi Ulrich et al,
Thanks again for your help.
import locale
locale.setlocale(locale.LC_ALL, 'en_GB.utf-8')
def common(request=None):
return {
'TODAY' : date.today(),
'LOCALE' : locale.getlocale(),
'LOCALE_DF' : locale.nl_langinfo(locale.D_FMT),
}
<pre>{{ LOCALE }}</pre>
<pre>{{ LOCALE_DF }}</pre>
<pre>{{ TODAY|date:"SHORT_DATE_FORMAT" }}</pre>
<pre>('en_GB', 'UTF-8')</pre>
<pre>%d/%m/%y</pre>
Any ideas appreciated please.
Thanks
Jay
Jay
On 17 September 2014 07:38, James Hargreaves <james.hargreaves@gmail.com> wrote:
Thanks Ulrich.No I think I've misread the documentation with regards it affecting the whole environment rather than the program.I'll try your solution myself but it looks like it will work, thanks!Jay
On Tuesday, September 16, 2014, uvetter <rettevhcirlu@googlemail.com> wrote:Dear Jay,--I just tried to following in my wsgi.py file:import osimport sysimport siteimport localesite.addsitedir('/mypath/lib/python3.3/site-packages')os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings")os.environ['HTTPS'] = "on"sys.path.append('/mypath/test')sys.path.append('/mypath/test/test')sys.path.append('/mypath/test/app1')sys.path.append('/mypath/test/app2')activate_env=os.path.expanduser("/mypath/bin/activate_this.py")exec(open(activate_env).read())locale.setlocale(locale.LC_ALL, 'en_US.utf-8')from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()This sets the locale correctly for all calls to my Django application.I just realized that in your first post you mentioned that "this affects the entire environment". I just read through the locale module documentation, which states that "The C standard defines the locale as a program-wide property ....". This means that there will be no system-wide changes when you call locale.setenv(). Or did I misunderstand you?Best wishes,Ulrich
Am Dienstag, 16. September 2014 22:39:08 UTC+2 schrieb James Hargreaves:Thanks for your reply Ulrich.If I set the locale in WSGI would that persist for all connections? If so that sounds like the best option.ThanksJay
On Tuesday, September 16, 2014, <rettev...@googlemail.com> wrote:Hi,--I ran into a similar problem yesterday while programming a web shop with Django (Version 1.6, Python version 3.3) where language switching should immediately have an effect on the way prices, etc. are displayed (doing so by a <form action="/i18n/setlang/" method="POST" > in the template and redirecting back to the page where it was called). The locale is indeed not set this way and needs to be set explicitly. What I finally came up with as a first working solution as part of a function:def convert_my_price(_price) # _price is a decimal.Decimallanguage = django.utils.translation.get_language()if language == 'en':locale.setlocale(locale.LC_ALL, 'en_US.utf-8') # 'en_GB.utf-8' didn't work for meelse:locale.setlocale(locale.LC_ALL, 'de_DE.utf-8') # this is the fallback_loc = locale.localeconv().. # process the Decimal.
return my_price_as_a_stringAfter doing so I have access to the locale details stored in the dictionairy _loc, like e.g. _loc['thousands_sep'], _loc['currency_symbol'] in this function, but also everywhere else via locale.localeconv()This function however is currently called from within class based views, but also implemented as a template filter. Of course one should not set the locale every time when calling this function, as you mentioned locale.getlocale() will should whether it was correctly set or not.Where you call locale.setlocale depends on your application. When you only have to set it once maybe the best would be to call it in even in e.g. wsgi.pyBest regards,Ulrich
--
a: 1 Oak Cottage, Town Lane, Mobberley, WA16 7HJ
t: 01565 873 019 | 07899 872 306
e: ja...@hargreaves.me.uk
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/9kuQBMknp9g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ca7c49a3-c3bc-4aa7-9234-6d3896c3e171%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
e: james@hargreaves.me.uk
--
a: 1 Oak Cottage, Town Lane, Mobberley, WA16 7HJ
t: 01565 873 019 | 07899 872 306
--
a: 1 Oak Cottage, Town Lane, Mobberley, WA16 7HJ
t: 01565 873 019 | 07899 872 306
e: james@hargreaves.me.uk
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKceJetdfDX5Qyw2nnGpzGaF-phSvy-dFN7jiyPbiymbnGYQaQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment