Skip to content
This repository
Browse code

move Locale logic into a function

  • Loading branch information...
commit 453a61693dafaa8bc1f5cc52216c656c6bbfd244 1 parent a693cf1
Wil Clouser authored May 23, 2012
6  apps/amo/helpers.py
@@ -10,7 +10,6 @@
10 10
 from django.utils.encoding import smart_unicode
11 11
 from django.template import defaultfilters
12 12
 
13  
-from babel import Locale
14 13
 from babel.support import Format
15 14
 import caching.base as caching
16 15
 import jinja2
@@ -216,10 +215,7 @@ def render(self):
216 215
 
217 216
 def _get_format():
218 217
     lang = translation.get_language()
219  
-    if lang == 'dbg':
220  
-        lang = 'en'
221  
-    locale = Locale(translation.to_locale(lang))
222  
-    return Format(locale)
  218
+    return Format(utils.get_locale_from_lang(lang))
223 219
 
224 220
 
225 221
 @register.filter
24  apps/amo/utils.py
@@ -29,10 +29,11 @@
29 29
 from django.forms.fields import Field
30 30
 from django.http import HttpRequest
31 31
 from django.template import Context, loader
32  
-from django.utils.translation import trans_real
  32
+from django.utils import translation
33 33
 from django.utils.functional import Promise
34 34
 from django.utils.encoding import smart_str, smart_unicode
35 35
 
  36
+from babel import Locale
36 37
 import bleach
37 38
 from cef import log_cef as _log_cef
38 39
 from easy_thumbnails import processors
@@ -511,13 +512,22 @@ def to_language(locale):
511 512
     """Like django's to_language, but en_US comes out as en-US."""
512 513
     # A locale looks like en_US or fr.
513 514
     if '_' in locale:
514  
-        return to_language(trans_real.to_language(locale))
  515
+        return to_language(translation.trans_real.to_language(locale))
515 516
     # Django returns en-us but we want to see en-US.
516 517
     elif '-' in locale:
517 518
         lang, region = locale.split('-')
518 519
         return '%s-%s' % (lang, region.upper())
519 520
     else:
520  
-        return trans_real.to_language(locale)
  521
+        return translation.trans_real.to_language(locale)
  522
+
  523
+
  524
+def get_locale_from_lang(lang):
  525
+    """Pass in a language (u'en-US') get back a Locale object courtesy of
  526
+    Babel.  Use this to figure out currencies, bidi, names, etc."""
  527
+    # Special fake language can just act like English for formatting and such
  528
+    if lang == 'dbg':
  529
+        lang = 'en'
  530
+    return Locale(translation.to_locale(lang))
521 531
 
522 532
 
523 533
 class HttpResponseSendFile(http.HttpResponse):
@@ -529,7 +539,7 @@ def __init__(self, request, path, content=None, status=None,
529 539
         super(HttpResponseSendFile, self).__init__('', status=status,
530 540
                                                    content_type=content_type)
531 541
         if settings.XSENDFILE:
532  
-            self[settings.XSENDFILE_HEADER] = path 
  542
+            self[settings.XSENDFILE_HEADER] = path
533 543
 
534 544
     def __iter__(self):
535 545
         if settings.XSENDFILE:
@@ -769,10 +779,10 @@ def log_cef(name, severity, env, *args, **kwargs):
769 779
 
770 780
 @contextlib.contextmanager
771 781
 def no_translation():
772  
-    lang = trans_real.get_language()
773  
-    trans_real.deactivate()
  782
+    lang = translation.trans_real.get_language()
  783
+    translation.trans_real.deactivate()
774 784
     yield
775  
-    trans_real.activate(lang)
  785
+    translation.trans_real.activate(lang)
776 786
 
777 787
 
778 788
 def escape_all(v):
8  apps/market/models.py
@@ -9,11 +9,11 @@
9 9
 import amo
10 10
 from amo.decorators import write
11 11
 import amo.models
12  
-from amo.utils import memoize_key
  12
+from amo.utils import get_locale_from_lang, memoize_key
13 13
 from stats.models import Contribution
14 14
 from users.models import UserProfile
15 15
 
16  
-from babel import Locale, numbers
  16
+from babel import numbers
17 17
 import commonware.log
18 18
 from jinja2.filters import do_dictsort
19 19
 import json_field
@@ -63,7 +63,7 @@ def _price(self):
63 63
             Price.transformer([])
64 64
 
65 65
         lang = translation.get_language()
66  
-        locale = Locale(translation.to_locale(lang))
  66
+        locale = get_locale_from_lang(lang)
67 67
         currency = amo.LOCALE_CURRENCY.get(locale.language)
68 68
         if currency:
69 69
             price_currency = Price._currencies.get((currency, self.id), None)
@@ -107,7 +107,7 @@ class Meta:
107 107
     def get_price_locale(self):
108 108
         """Return the price as a nicely localised string for the locale."""
109 109
         lang = translation.get_language()
110  
-        locale = Locale(translation.to_locale(lang))
  110
+        locale = get_locale_from_lang(lang)
111 111
         return numbers.format_currency(self.price, self.currency,
112 112
                                        locale=locale)
113 113
 

0 notes on commit 453a616

Please sign in to comment.
Something went wrong with that request. Please try again.