2 Utilities for creating multilingual fields in your apps.
6 from django.conf import settings
7 from django.db import models
8 from django.utils.translation import get_language, string_concat
11 def localize_field(name, lang=None):
14 if lang not in (x[0] for x in settings.LANGUAGES):
15 lang = settings.LANGUAGE_CODE
16 return "%s_%s" % (name, lang)
18 def field_getter(name):
21 val = getattr(self, localize_field(name), None)
23 val = getattr(self, localize_field(name, settings.LANGUAGE_CODE))
28 def add_translatable(model, fields, languages=None):
29 """Adds some translatable fields to a model, and a getter."""
31 languages = settings.LANGUAGES
32 for name, field in fields.items():
33 for lang_code, lang_name in languages:
34 new_field = copy(field)
35 if field.verbose_name:
36 new_field.verbose_name = string_concat(field.verbose_name, ' [%s]' % lang_code)
37 new_field.contribute_to_class(model, localize_field(name, lang_code))
38 setattr(model, name, field_getter(name))
42 def add_translatable_index(index_class, fields, languages=None):
43 """Adds some translatable fields to a search index."""
45 languages = settings.LANGUAGES
46 for name, field in fields.items():
47 for lang_code, lang_name in languages:
48 new_field = copy(field)
49 fname = localize_field(name, lang_code)
50 new_field.index_fieldname = new_field.index_fieldname \
51 and localize_field(new_field.index_fieldname, lang_code) \
53 new_field.model_attr = new_field.model_attr \
54 and localize_field(new_field.model_attr, lang_code) \
56 setattr(index_class, fname, new_field)
57 index_class.fields[fname] = new_field
60 def translated_fields(field_names, languages=settings.LANGUAGES):
61 """Generate a tuple of field names in translated versions."""
62 return tuple(localize_field(field_name, lang_code)
63 for field_name in field_names
64 for lang_code, lang_name in languages
68 """ Creates a query (Q) with lookups on translated fields. """
70 for k, v in kwargs.items():
71 trans_kwargs[localize_field(k)] = v
72 return models.Q(**trans_kwargs)