Package: modeltranslation
Versión en español enREADME.es.mdIntroductionThis application allows you useDjangomodel translations easily.
Everything is based on FieldTranslation, a class that stores the
translation of each field of your application models.All the process is transparent and the entries are created when save is
called.You will not have to modify FieldTranslation, but you can read the code.All code in in the github repository:https://github.com/intelligenia/modeltranslationInstructionsInstallationThis application depends ondjango-cuserandTinyMCE, so you will
need to install it and put it in the list of INSTALLED_APPS before
modeltranslation.You are encouraged to use pip to install django-cuser.The easiest way to install modeltranslation is by installing it frompipy:pipinstallmodeltranslationOnce you’ve done this, you can install modeltranslation in settings.py:INSTALLED_APPS = ( “tinymce”, “cuser”, “modeltranslation” )Add IS_MONOLINGUAL to settings.pyYou’ll have to include a new setting in settings.py
IS_MONOLINGUAL=False. IS_MONOLINGUAL acts as a switch for
modeltranslation:# modeltranslation only works when IS_MONOLINGUAL is FalseIS_MONOLINGUAL=FalseAdd TRANSLATABLE_MODEL_MODULES to settings.pyAdd file setting TRANSLATABLE_MODEL_MODULES to settings.py.
TRANSLATABLE_MODEL_MODULES contains a list of module paths that will
be translated. For example:TRANSLATABLE_MODEL_MODULES=["app1.models","app2.models","fees.models","menus.models",...]Import addtranslationsImportaddtranslationsif each of your models.py files:frommodeltranslation.translationimportaddtranslationsAfter that, you’ll have to calladdtranslationsat the end of this
file:addtranslations(__name__)This call adds an observer that saves translations whensavemodel
method is executed.Add translatable_fields to your modelsModify your models including a meta field “translatable_fields”. This
field is a list with the fields you want to translate.For example:fromdjango.dbimportmodelsclassEvent(models.Model):name=models.CharField(blank=False,max_length=150,verbose_name=u"Name",help_text=u"Name of the event.")description=models.TextField(blank=False,verbose_name=u"Description",help_text=u"Long description of the event.")short_description=models.CharField(blank=False,max_length=150,verbose_name=u"Short description",help_text=u"Short description of the event.")#...## Event MetaclassMeta:verbose_name="event"verbose_name_plural="events"translatable_fields=("name","description","short_description")And that’s all!Now you have everything configurated and can use modeltranslation
translations.How to use translations in ModelFormsMake your ModelForm object inherits of TranslatableModelForm. This will
include automatically the extra fields for each language you have in
your website.frommodeltranslation.formsimportTranslatableModelFormclassEventForm(TranslatableModelForm):passIf you need to modify any of the fields ininitmethod (for example
by changing the widget of one field), you’ll have to call
_add_translation_form_fields after your changes.For example:# EventForm __init__def__init__(self,event,*args,**kwargs):super(EventForm,self).__init__(*args,**kwargs)# Adds TinyMCE editor but we also want this editor in other# languages' description fieldsself.fields["description"].widget=TinyMCE()# Manually adition of translation fieldsself._add_translation_form_fields()Dynamic translations in Django templatesInclude modeltranslation filters in each template you wan to use this
special filters.{%loadmodeltranslation_tags%}Use filter “_” with the object and one of its fields. This filter
will return the translation of that field in the current language.For example:{{event|_:"name"}}{# Translates event name #}{{event.area|_:"name"}}{# Translates area name #}Dynamic translations in codeThis application injects a new method to each translatable model:get_trans_attr. This method returns the translation of the
attribute in the current language (if it exists), otherwise returns the
default value for this attribute:For example:# Original event nameoriginal_event_nameevent.name# Translated event nametranslated_event_nameevent.get_trans_attr("name")Contact and suggestionsCreate a new issue in this repository.Email me atdiegoREMOVE_THIS@intelligeniaREMOVE_THIS.com