diff --git a/apps/users/models.py b/apps/users/models.py index bee6e8f12f..81867bbf13 100644 --- a/apps/users/models.py +++ b/apps/users/models.py @@ -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') diff --git a/apps/users/tests/test_models.py b/apps/users/tests/test_models.py index 3ae7c334dc..268c6773e5 100644 --- a/apps/users/tests/test_models.py +++ b/apps/users/tests/test_models.py @@ -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='', 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' diff --git a/mkt/purchase/templates/purchase/receipt.txt b/mkt/purchase/templates/purchase/receipt.txt index 08969f7306..9b01dc0970 100644 --- a/mkt/purchase/templates/purchase/receipt.txt +++ b/mkt/purchase/templates/purchase/receipt.txt @@ -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 %} diff --git a/mkt/purchase/tests/utils.py b/mkt/purchase/tests/utils.py index 176d723091..a6ab924be0 100644 --- a/mkt/purchase/tests/utils.py +++ b/mkt/purchase/tests/utils.py @@ -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 diff --git a/mkt/purchase/webpay_tasks.py b/mkt/purchase/webpay_tasks.py index ab58e2b558..d70b7ccd5b 100644 --- a/mkt/purchase/webpay_tasks.py +++ b/mkt/purchase/webpay_tasks.py @@ -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])