move Locale logic into a function

This commit is contained in:
Wil Clouser 2012-05-23 14:13:28 -07:00
Родитель a693cf1bfc
Коммит 453a61693d
3 изменённых файлов: 22 добавлений и 16 удалений

Просмотреть файл

@ -10,7 +10,6 @@ from django.utils import translation
from django.utils.encoding import smart_unicode
from django.template import defaultfilters
from babel import Locale
from babel.support import Format
import caching.base as caching
import jinja2
@ -216,10 +215,7 @@ class Paginator(object):
def _get_format():
lang = translation.get_language()
if lang == 'dbg':
lang = 'en'
locale = Locale(translation.to_locale(lang))
return Format(locale)
return Format(utils.get_locale_from_lang(lang))
@register.filter

Просмотреть файл

@ -29,10 +29,11 @@ from django.core.mail import EmailMessage
from django.forms.fields import Field
from django.http import HttpRequest
from django.template import Context, loader
from django.utils.translation import trans_real
from django.utils import translation
from django.utils.functional import Promise
from django.utils.encoding import smart_str, smart_unicode
from babel import Locale
import bleach
from cef import log_cef as _log_cef
from easy_thumbnails import processors
@ -511,13 +512,22 @@ def to_language(locale):
"""Like django's to_language, but en_US comes out as en-US."""
# A locale looks like en_US or fr.
if '_' in locale:
return to_language(trans_real.to_language(locale))
return to_language(translation.trans_real.to_language(locale))
# Django returns en-us but we want to see en-US.
elif '-' in locale:
lang, region = locale.split('-')
return '%s-%s' % (lang, region.upper())
else:
return trans_real.to_language(locale)
return translation.trans_real.to_language(locale)
def get_locale_from_lang(lang):
"""Pass in a language (u'en-US') get back a Locale object courtesy of
Babel. Use this to figure out currencies, bidi, names, etc."""
# Special fake language can just act like English for formatting and such
if lang == 'dbg':
lang = 'en'
return Locale(translation.to_locale(lang))
class HttpResponseSendFile(http.HttpResponse):
@ -529,7 +539,7 @@ class HttpResponseSendFile(http.HttpResponse):
super(HttpResponseSendFile, self).__init__('', status=status,
content_type=content_type)
if settings.XSENDFILE:
self[settings.XSENDFILE_HEADER] = path
self[settings.XSENDFILE_HEADER] = path
def __iter__(self):
if settings.XSENDFILE:
@ -769,10 +779,10 @@ def log_cef(name, severity, env, *args, **kwargs):
@contextlib.contextmanager
def no_translation():
lang = trans_real.get_language()
trans_real.deactivate()
lang = translation.trans_real.get_language()
translation.trans_real.deactivate()
yield
trans_real.activate(lang)
translation.trans_real.activate(lang)
def escape_all(v):

Просмотреть файл

@ -9,11 +9,11 @@ from translations.fields import TranslatedField
import amo
from amo.decorators import write
import amo.models
from amo.utils import memoize_key
from amo.utils import get_locale_from_lang, memoize_key
from stats.models import Contribution
from users.models import UserProfile
from babel import Locale, numbers
from babel import numbers
import commonware.log
from jinja2.filters import do_dictsort
import json_field
@ -63,7 +63,7 @@ class Price(amo.models.ModelBase):
Price.transformer([])
lang = translation.get_language()
locale = Locale(translation.to_locale(lang))
locale = get_locale_from_lang(lang)
currency = amo.LOCALE_CURRENCY.get(locale.language)
if currency:
price_currency = Price._currencies.get((currency, self.id), None)
@ -107,7 +107,7 @@ class PriceCurrency(amo.models.ModelBase):
def get_price_locale(self):
"""Return the price as a nicely localised string for the locale."""
lang = translation.get_language()
locale = Locale(translation.to_locale(lang))
locale = get_locale_from_lang(lang)
return numbers.format_currency(self.price, self.currency,
locale=locale)