activate a users locale (bug 833563)

This commit is contained in:
Andy McKay 2013-05-06 12:01:53 -07:00
Родитель 8265474e47
Коммит b812c664c0
5 изменённых файлов: 49 добавлений и 20 удалений

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

@ -1,19 +1,21 @@
from base64 import decodestring
from datetime import datetime
import hashlib
import os
import random
import re
import string
import time
from base64 import decodestring
from contextlib import contextmanager
from datetime import datetime
from django import forms, dispatch
from django import dispatch, forms
from django.conf import settings
from django.contrib.auth.models import User as DjangoUser
from django.core import validators
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, transaction
from django.template import Context, loader
from django.utils import translation
from django.utils.encoding import smart_str, smart_unicode
from django.utils.functional import lazy
@ -25,7 +27,7 @@ import amo
import amo.models
from access.models import Group, GroupUser
from amo.urlresolvers import reverse
from translations.fields import save_signal, PurifiedField
from translations.fields import PurifiedField, save_signal
from translations.query import order_by_translation
log = commonware.log.getLogger('z.users')
@ -476,6 +478,19 @@ class UserProfile(amo.models.OnChangeMixin, amo.models.ModelBase):
"""
return bool(getattr(self.get_preapproval(), 'paypal_key', ''))
@contextmanager
def activate_lang(self):
"""
Activate the language for the user. If none is set will go to the site
default which is en-US.
"""
lang = self.lang if self.lang else settings.LANGUAGE_CODE
old = translation.get_language()
translation.activate(lang)
yield
translation.activate(old)
models.signals.pre_save.connect(save_signal, sender=UserProfile,
dispatch_uid='userprofile_translations')

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

@ -7,7 +7,7 @@ from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.utils import encoding
from django.utils import encoding, translation
from mock import patch
from nose.tools import eq_
@ -230,6 +230,15 @@ class TestUserProfile(amo.tests.TestCase):
eq_(UserProfile(username='<yolo>', id=1).get_url_path(),
'/en-US/firefox/user/1/')
@patch.object(settings, 'LANGUAGE_CODE', 'en-US')
def test_activate_locale(self):
eq_(translation.get_language(), 'en-us')
with UserProfile(username='yolo').activate_lang():
eq_(translation.get_language(), 'en-us')
with UserProfile(username='yolo', lang='fr').activate_lang():
eq_(translation.get_language(), 'fr')
class TestPasswords(amo.tests.TestCase):
utf = u'\u0627\u0644\u062a\u0637\u0628'

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

@ -1,13 +1,13 @@
{% trans %}
Thank you for purchasing from the Firefox Marketplace.
App name: {{ app_name }}
App developer: {% for author in authors.all() %}{{ author.display_name }}{% if not loop.last %}, {% endif %}{% endfor %}
App developer: {{ authors }}
Order date: {{ date|datetime }}
Order date: {{ date }}
Order number: {{ transaction_id }}
{% if support_url %}
For support please visit: {{ support_url }}
{% endif %}
Your purchase history: {{ purchases }}
Marketplace terms of service: {{ terms_of_service_url }}
{% endtrans %}

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

@ -1,6 +1,6 @@
from decimal import Decimal
import amo
import amo.tests
from addons.models import Addon
from market.models import AddonPremium, Price, PriceCurrency
from users.models import UserProfile

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

@ -1,6 +1,8 @@
import logging
from celeryutils import task
from jingo.helpers import datetime
from tower import ugettext as _
import amo
from amo.decorators import write
@ -63,17 +65,20 @@ def send_purchase_receipt(contrib_id, **kw):
Sends an email to the purchaser of the app.
"""
contrib = Contribution.objects.get(pk=contrib_id)
# TODO: localize when 833049 is done.
subject = u'Receipt for %s' % contrib.addon.name
data = {'app_name': contrib.addon.name,
'date': contrib.created.date(),
# TODO: localize this properly.
'authors': contrib.addon.authors,
with contrib.user.activate_lang():
# L10n: {0} is the app name.
subject = _('Receipt for {0}') % contrib.addon.name
data = {
'app_name': contrib.addon.name,
'authors': ', '.join([author.display_name
for author in contrib.addon.authors.all()]),
'date': datetime(contrib.created.date()),
'purchases': absolutify(reverse('account.purchases')),
'support_url': contrib.addon.support_url,
'terms_of_service_url': absolutify(reverse('site.terms')),
'transaction_id': contrib.uuid}
'transaction_id': contrib.uuid
}
log.info('Sending email about purchase: %s' % contrib_id)
send_mail_jinja(subject, 'purchase/receipt.txt', data,
recipient_list=[contrib.user.email])
log.info('Sending email about purchase: %s' % contrib_id)
send_mail_jinja(subject, 'purchase/receipt.txt', data,
recipient_list=[contrib.user.email])