def test_get_fallback(self): """Return the translation for the locale fallback.""" user = UserProfile.objects.create( lang='fr', bio={'en-US': 'my bio', 'fr': 'ma bio'}) trans_eq(user.bio, 'my bio', 'en-US') # Uses current locale. with self.activate(locale='de'): user = UserProfile.objects.get(pk=user.pk) # Reload. trans_eq(user.bio, 'ma bio', 'fr') # Uses the default fallback.
def test_fetch_translation_de_locale(self): """Check that locale fallbacks work.""" try: translation.activate('de') o = TranslatedModel.objects.get(id=1) trans_eq(o.name, 'German!! (unst unst)', 'de') trans_eq(o.description, 'some description', 'en-US') finally: translation.deactivate()
def test_fetch_translation_de_locale(self): """Check that locale fallbacks work.""" try: translation.activate("de") o = TranslatedModel.objects.get(id=1) trans_eq(o.name, "German!! (unst unst)", "de") trans_eq(o.description, "some description", "en-US") finally: translation.deactivate()
def test_update_translation(self): o = TranslatedModel.objects.get(id=1) translation_id = o.name.autoid o.name = 'new name' o.save() o = TranslatedModel.objects.get(id=1) trans_eq(o.name, 'new name', 'en-US') # Make sure it was an update, not an insert. eq_(o.name.autoid, translation_id)
def test_post(self): assert self.client.login(username='******', password='******') url = reverse('localizers.categories', kwargs=dict(locale_code='es-ES')) data = { 'form-TOTAL_FORMS': 2, 'form-INITIAL_FORMS': 2, 'form-0-id': self.cat1.id, 'form-0-name': u'Nada', 'form-1-id': self.cat2.id, 'form-1-name': u'Amigo', } res = self.client.post(url, data, follow=True) self.assertRedirects(res, url, status_code=302) doc = pq(res.content.decode('utf-8')) eq_(doc('#id_form-0-name').val(), u'Nada') eq_(doc('#id_form-1-name').val(), u'Amigo') cat = Category.objects.get(pk=self.cat1.id) trans_eq(cat.name, u'Nada', 'es-ES')
def test_translations(self): translation.activate('en-US') # There's en-US and de translations. We should get en-US. r1 = Review.objects.get(id=1) test_utils.trans_eq(r1.title, 'r1 title en', 'en-US') # There's only a de translation, so we get that. r2 = Review.objects.get(id=2) test_utils.trans_eq(r2.title, 'r2 title de', 'de') translation.activate('de') # en and de exist, we get de. r1 = Review.objects.get(id=1) test_utils.trans_eq(r1.title, 'r1 title de', 'de') # There's only a de translation, so we get that. r2 = Review.objects.get(id=2) test_utils.trans_eq(r2.title, 'r2 title de', 'de')
def test_translations(self): translation.activate('en-US') # There's en-US and de translations. We should get en-US. r1 = Rating.objects.get(id=1) trans_eq(r1.body, 'r1 body en', 'en-US') # There's only a de translation, so we get that. r2 = Rating.objects.get(id=2) trans_eq(r2.body, 'r2 body de', 'de') translation.activate('de') # en and de exist, we get de. r1 = Rating.objects.get(id=1) trans_eq(r1.body, 'r1 body de', 'de') # There's only a de translation, so we get that. r2 = Rating.objects.get(id=2) trans_eq(r2.body, 'r2 body de', 'de')
def test_create_with_dict(self): # Set translations with a dict. strings = {'en-US': 'right language', 'de': 'wrong language'} o = TranslatedModel.objects.create(name=strings) # Make sure we get the English text since we're in en-US. trans_eq(o.name, 'right language', 'en-US') # Check that de was set. translation.activate('de') o = TranslatedModel.objects.get(id=o.id) trans_eq(o.name, 'wrong language', 'de') # We're in de scope, so we should see the de text. de = TranslatedModel.objects.create(name=strings) trans_eq(o.name, 'wrong language', 'de') # Make sure en-US was still set. translation.deactivate() o = TranslatedModel.objects.get(id=de.id) trans_eq(o.name, 'right language', 'en-US')
def test_update_with_dict(self): # There's existing en-US and de strings. strings = {'de': None, 'fr': 'oui'} get_model = lambda: TranslatedModel.objects.get(id=1) # Don't try checking that the model's name value is en-US. It will be # one of the other locales, but we don't know which one. You just set # the name to a dict, deal with it. get_model().name = strings # en-US was not touched. trans_eq(get_model().name, 'some name', 'en-US') # de was updated to NULL, so it falls back to en-US. translation.activate('de') trans_eq(get_model().name, 'some name', 'en-US') # fr was added. translation.activate('fr') trans_eq(get_model().name, 'oui', 'fr')
def test_update_with_dict(self): # There's existing en-US and de strings. strings = {'de': None, 'fr': 'oui'} get_model = lambda: TranslatedModel.objects.get(id=1) # Don't try checking that the model's name value is en-US. It will be # one of the other locales, but we don't know which one. You just set # the name to a dict, deal with it. m = get_model() m.name = strings m.save() # en-US was not touched. trans_eq(get_model().name, 'some name', 'en-US') # de was updated to NULL, so it falls back to en-US. translation.activate('de') trans_eq(get_model().name, 'some name', 'en-US') # fr was added. translation.activate('fr') trans_eq(get_model().name, 'oui', 'fr')
def test_fetch_translations(self): """Basic check of fetching translations in the current locale.""" o = TranslatedModel.objects.get(id=1) trans_eq(o.name, 'some name', 'en-US') trans_eq(o.description, 'some description', 'en-US')
def test_create_translation(self): o = TranslatedModel.objects.create(name='english name') get_model = lambda: TranslatedModel.objects.get(id=o.id) trans_eq(o.name, 'english name', 'en-US') eq_(o.description, None) # Make sure the translation id is stored on the model, not the autoid. eq_(o.name.id, o.name_id) # Check that a different locale creates a new row with the same id. translation.activate('de') german = get_model() trans_eq(o.name, 'english name', 'en-US') german.name = u'Gemütlichkeit name' german.description = u'clöüserw description' german.save() trans_eq(german.name, u'Gemütlichkeit name', 'de') trans_eq(german.description, u'clöüserw description', 'de') # ids should be the same, autoids are different. eq_(o.name.id, german.name.id) assert o.name.autoid != german.name.autoid # Check that de finds the right translation. fresh_german = get_model() trans_eq(fresh_german.name, u'Gemütlichkeit name', 'de') trans_eq(fresh_german.description, u'clöüserw description', 'de') # Check that en-US has the right translations. translation.deactivate() english = get_model() trans_eq(english.name, 'english name', 'en-US') english.debug = True eq_(english.description, None) english.description = 'english description' english.save() fresh_english = get_model() trans_eq(fresh_english.description, 'english description', 'en-US') eq_(fresh_english.description.id, fresh_german.description.id)