def test_translation_sequence_increases(self): """Make sure translation sequence increases monotonically.""" newtrans1 = Translation.new('abc', 'en-us') newtrans1.save() newtrans2 = Translation.new('def', 'de') newtrans2.save() assert newtrans2.pk > newtrans1.pk, ( 'Translation sequence needs to keep increasing.')
def format_output(self, widgets): s = super(TransMulti, self).format_output(widgets) init = self.widget().render(self.name + '_', Translation(locale='init'), {'class': 'trans-init'}) return '<div id="trans-%s" class="trans" data-name="%s">%s%s</div>' % ( self.name, self.name, s, init)
def test_empty_translations_seq(self): """Make sure we can handle an empty translation sequence table.""" TranslationSequence.objects.all().delete() newtrans = Translation.new('abc', 'en-us') newtrans.save() assert newtrans.id > 0, ( 'Empty translation table should still generate an ID.')
def test_translation_bool(): t = lambda s: Translation(localized_string=s) assert bool(t('text')) is True assert bool(t(' ')) is False assert bool(t('')) is False assert bool(t(None)) is False
def test_single_translation_sequence(self): """Make sure we only ever have one translation sequence.""" TranslationSequence.objects.all().delete() eq_(TranslationSequence.objects.count(), 0) for i in range(5): newtrans = Translation.new(str(i), 'en-us') newtrans.save() eq_(TranslationSequence.objects.count(), 1)
def render(self, name, value, attrs=None): self.name = name value = self.decompress(value) if value: self.widgets = [self.widget() for ignored in value] else: # Give an empty widget in the current locale. self.widgets = [self.widget()] value = [Translation(locale=translation_utils.get_language())] return super(TransMulti, self).render(name, value, attrs)
def decompress(self, value): if not value: return [] elif isinstance(value, (long, int)): # We got a foreign key to the translation table. qs = Translation.objects.filter(id=value) return list(qs.filter(localized_string__isnull=False)) elif isinstance(value, dict): # We're getting a datadict, there was a validation error. return [ Translation(locale=k, localized_string=v) for k, v in value.items() ]
def get_trans(items): if not items: return connection = connections[multidb.get_slave()] cursor = connection.cursor() model = items[0].__class__ sql, params = build_query(model, connection) item_dict = dict((item.pk, item) for item in items) ids = ','.join(map(str, item_dict.keys())) cursor.execute(sql.format(ids='(%s)' % ids), tuple(params)) step = len(trans_fields) for row in cursor.fetchall(): # We put the item's pk as the first selected field. item = item_dict[row[0]] for index, field in enumerate(model._meta.translated_fields): start = 1 + step * index t = Translation(*row[start:start + step]) if t.id is not None and t.localized_string is not None: setattr(item, field.name, t)
def test_whitespace(self): t = Translation(localized_string=' khaaaaaan! ', id=999) t.save() eq_('khaaaaaan!', t.localized_string)
def test_translation_unicode(): t = lambda s: Translation(localized_string=s) eq_(unicode(t('hello')), 'hello') eq_(unicode(t(None)), '')
def test_sorting(self): """Test translation comparisons in Python code.""" b = Translation.new('bbbb', 'de') a = Translation.new('aaaa', 'de') c = Translation.new('cccc', 'de') eq_(sorted([c, a, b]), [a, b, c])
def test_comparison_with_lazy(): x = Translation(localized_string='xxx') lazy_u = lazy(lambda x: x, unicode) x == lazy_u('xxx') lazy_u('xxx') == x