def value_from_datadict(self, data, files, name): from fluent.fields import TranslatableContent text, language_code = [ widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets) ] return TranslatableContent(text=text, language_code=language_code)
class TranslatedModel(models.Model): class Meta: app_label = "fluent" trans = TranslatableCharField(blank=True) trans_with_hint = TranslatableCharField(hint="Test", blank=True) trans_with_group = TranslatableCharField(group="Test", blank=True) trans_with_default = TranslatableCharField(blank=True, default=TranslatableContent(text="Adirondack"))
def test_finding_all_translations_for_a_group(self): TranslatedModel.objects.create() translations = MasterTranslation.find_by_group("Test") self.assertEqual(0, translations.count()) TranslatedModel.objects.create(trans_with_group=TranslatableContent(text="Hello World!")) translations = MasterTranslation.find_by_group("Test") self.assertEqual(1, translations.count())
def clean(self, value): from fluent.fields import TranslatableContent if isinstance(value, TranslatableContent): value.text = super(TranslatableCharField, self).clean(value.text) if not value.hint: value.hint = self.hint if not value.language_code: value.language_code = self.language_code return value elif isinstance(value, basestring): value = super(TranslatableCharField, self).clean(value) return TranslatableContent(text=value, hint=self.hint, language_code=self.language_code) else: return TranslatableContent(hint=self.hint, language_code=self.language_code)
def test_unicode_with_active_language(self): """ If there's a currently-active language, unicode should return the translated text. """ def mock_get_translation(text, hint, language_code): if language_code == "de": return {"singular": "translated", "o": "translated"} return {"singular": self.text} translation.activate("de") with sleuth.switch( "fluent.fields.trans.TRANSLATION_CACHE.get_translation", mock_get_translation ): obj = TranslatableContent(text=u'\xc5ukasz') result = unicode(obj) self.assertEqual(result, u'translated') self.assertIsInstance(result, unicode)
def gen_translatablecontent(): from fluent.fields import TranslatableContent return TranslatableContent(text=generators.gen_text())
def test_delete_does_nothing(self): """ Test that deleting a MasterTranslation does not delete the model which uses it. """ TranslatedModel.objects.create(trans=TranslatableContent("whatever")) self.assertEqual(TranslatedModel.objects.count(), 1) MasterTranslation.objects.all().delete() self.assertEqual(TranslatedModel.objects.count(), 1)
def test_unicode(self): obj = TranslatableContent(text=u'\xc5ukasz') result = unicode(obj) self.assertEqual(result, u'\xc5ukasz') self.assertIsInstance(result, unicode)
def test_str(self): obj = TranslatableContent(text=u'\xc5ukasz') result = str(obj) self.assertEqual(result, '\xc3\x85ukasz') self.assertIsInstance(result, str)
def test_repr_with_active_language(self): """ repr should give info about the default text, regardless of the active language. """ obj = TranslatableContent(text=u'\xc5ukasz') # Lukasz, but with a dirty L. translation.activate("de") result = repr(obj) self.assertEqual(result, "<TranslatableContent '\xc3\x85ukasz' lang: en-us>")
def test_repr(self): obj = TranslatableContent(text=u'\xc5ukasz') # Lukasz, but with a dirty L. result = repr(obj) self.assertEqual(result, "<TranslatableContent '\xc3\x85ukasz' lang: en-us>") self.assertIsInstance(result, str)
def gen_translatablecontent(): from fluent.fields import TranslatableContent return TranslatableContent(text=random_gen.gen_text())