def __init__(self, *args, **kwargs): '''Added i18m support to model fields and choices''' # detect current application name by model name app_name = re.sub("^([A-Z][^A-Z]+).*", "\\1", self.Meta.model.kind()).lower() # translate fields for field_name, field in self.base_fields.items(): # translate label if not field.label: field.label = field_name.capitalize() field.label = _(field.label, app_name) # translate choices if hasattr(field, "widget") and hasattr(field.widget, "choices"): field.widget.choices = [(key, _(name, app_name)) for key, name in field.widget.choices] super(ModelForm, self).__init__(*args, **kwargs)
def filter_translate(text): '''Return translated text''' return _(text)
def translate(self, context): '''Return translated text''' text = self.get_var(context, self.text) if hasattr(self, "app_name"): return _(text, self.app_name) return _(text)
def __init__(self, message, *args, **kwargs): '''Translate message to current language''' message = _(message) super(ValidationError, self).__init__(message=message, *args, **kwargs)