我正在使用Django 国际化工具从我的应用程序中翻译一些字符串。代码如下所示:
from django.utils.translation import ugettext as _
def my_view(request):
output = _("Welcome to my site.")
return HttpResponse(output)
然后,我正在使用Django 测试客户端编写单元测试。这些测试向视图发出请求并比较返回的内容。
如何在运行单元测试时禁用翻译?我的目标是这样做:
class FoobarTestCase(unittest.TestCase):
def setUp(self):
# Do something here to disable the string translation. But what?
# I've already tried this, but it didn't work:
django.utils.translation.deactivate_all()
def testFoobar(self):
c = Client()
response = c.get("/foobar")
# I want to compare to the original string without translations.
self.assertEquals(response.content.strip(), "Welcome to my site.")