Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move Locale logic into a function

  • Loading branch information...
commit 453a61693dafaa8bc1f5cc52216c656c6bbfd244 1 parent a693cf1
@clouserw clouserw authored
Showing with 22 additions and 16 deletions.
  1. +1 −5 apps/amo/helpers.py
  2. +17 −7 apps/amo/utils.py
  3. +4 −4 apps/market/models.py
View
6 apps/amo/helpers.py
@@ -10,7 +10,6 @@
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 @@ def render(self):
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
View
24 apps/amo/utils.py
@@ -29,10 +29,11 @@
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 @@ def __init__(self, request, path, content=None, status=None,
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):
View
8 apps/market/models.py
@@ -9,11 +9,11 @@
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 @@ def _price(self):
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 Meta:
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)
Please sign in to comment.
Something went wrong with that request. Please try again.