Пример #1
0
def _load_translations(instance, lang=None):
	"""
	Loads all translations as dynamic attributes:
		<attr>_<lang_code>
		<attr>_<lang_code>_is_fuzzy
	"""
	# Gets field translations
	translations = _get_fieldtranslations(instance=instance, lang=lang)
	for translation_i in translations:
		# Sets translated field lang for this language
		field_lang = trans_attr(translation_i.field, translation_i.lang)
		setattr(instance, field_lang, translation_i.translation)
		# Sets is_fuzzy value for this language
		field_isfuzzy_lang = trans_is_fuzzy_attr(translation_i.field, translation_i.lang)
		#print "{0} ({1}), attr_name={2} value={3}".format(translation.field, translation.lang, isfuzzy_lang,translation.is_fuzzy)
		setattr(instance, field_isfuzzy_lang, translation_i.is_fuzzy)
	return True
Пример #2
0
def _set_dict_translations(instance, dict_translations):
    """
	Establece los atributos de traducciones a partir de una dict que
	contiene todas las traducciones.
	"""

    # If class has no translatable fields get out
    if not hasattr(instance._meta, "translatable_fields"):
        return False

    # If we are in a site with one language there is no need of saving translations
    if site_is_monolingual():
        return False

    # Translatable fields
    translatable_fields = instance._meta.translatable_fields

    # For each translatable field and for each language (excluding default language), we have to see if there is
    # two dynamic fields:
    # - <attribute>_<language_code>: translated atribute in <language_code>.
    #   For example: name_fr, name_es, description_it, etc.
    # - <attribute>_is_fuzzy_<language_code>: is a provisional translation of <attribute> for language <language_code>.
    #   For example: name_is_fuzzy_fr, name_is_fuzzy_es, description_is_fuzzy_it, etc.
    for field in translatable_fields:
        for lang in settings.LANGUAGES:
            lang = lang[0]
            if lang != settings.LANGUAGE_CODE:

                # Translated field name
                trans_field = trans_attr(field, lang)
                # If translated field name is in the dict, we assign it to the object
                if dict_translations.has_key(trans_field):
                    setattr(instance, trans_field,
                            dict_translations[trans_field])

                # Is fuzzy attribute
                trans_isfuzzy = trans_is_fuzzy_attr(field, lang)
                # If "is fuzzy" name is in the dict, we assign it to the object
                if dict_translations.has_key(trans_isfuzzy):
                    is_fuzzy_value = (
                        dict_translations[trans_isfuzzy]
                        == "1") or (dict_translations[trans_isfuzzy] == 1)
                    setattr(instance, trans_isfuzzy, is_fuzzy_value)
Пример #3
0
def _set_dict_translations(instance, dict_translations):
	"""
	Establece los atributos de traducciones a partir de una dict que
	contiene todas las traducciones.
	"""

	# If class has no translatable fields get out
	if not hasattr(instance._meta, "translatable_fields"):
		return False

	# If we are in a site with one language there is no need of saving translations
	if site_is_monolingual():
		return False

	# Translatable fields
	translatable_fields = instance._meta.translatable_fields

	# For each translatable field and for each language (excluding default language), we have to see if there is
	# two dynamic fields:
	# - <attribute>_<language_code>: translated atribute in <language_code>.
	#   For example: name_fr, name_es, description_it, etc.
	# - <attribute>_is_fuzzy_<language_code>: is a provisional translation of <attribute> for language <language_code>.
	#   For example: name_is_fuzzy_fr, name_is_fuzzy_es, description_is_fuzzy_it, etc.
	for field in translatable_fields:
		for lang in settings.LANGUAGES:
			lang = lang[0]
			if lang != settings.LANGUAGE_CODE:
					
				# Translated field name
				trans_field = trans_attr(field,lang)
				# If translated field name is in the dict, we assign it to the object
				if dict_translations.has_key(trans_field):
					setattr(instance,trans_field,dict_translations[trans_field])
					
				# Is fuzzy attribute
				trans_isfuzzy = trans_is_fuzzy_attr(field,lang)
				# If "is fuzzy" name is in the dict, we assign it to the object
				if dict_translations.has_key(trans_isfuzzy):
					is_fuzzy_value = (dict_translations[trans_isfuzzy]=="1") or (dict_translations[trans_isfuzzy]==1)
					setattr(instance,trans_isfuzzy, is_fuzzy_value)
Пример #4
0
def _get_translated_field(instance, attr):
	# If we don't have translations, return the original attribute
	if site_is_monolingual():
		return getattr(instance, attr)
	
	# Current language
	lang = translation.get_language()
	#print u"\n{0}[id={1}]: {2}='{3}', atrr_in_lang='{2}_{4}'".format(instance.__class__.__name__, instance.id, attr, getattr(instance,attr), lang)
	
	# Load translations
	_load_translations(instance=instance, lang=lang)
	
	# Names of translated attributes of translated attribute and related is_fuzzy one.
	trans_field = trans_attr(attr, lang)
	is_fuzzy_attr = trans_is_fuzzy_attr(attr, lang)
	
	# If the translated attribute exists and is not fuzzy, return it
	if hasattr(instance,trans_field) and hasattr(instance, is_fuzzy_attr) and not getattr(instance, is_fuzzy_attr):
		return getattr(instance,trans_field)
	
	# Otherwise, return its original value
	return getattr(instance, attr)
Пример #5
0
def _get_translated_field(instance, attr):
    # If we don't have translations, return the original attribute
    if site_is_monolingual():
        return getattr(instance, attr)

    # Current language
    lang = translation.get_language()
    #print u"\n{0}[id={1}]: {2}='{3}', atrr_in_lang='{2}_{4}'".format(instance.__class__.__name__, instance.id, attr, getattr(instance,attr), lang)

    # Load translations
    _load_translations(instance=instance, lang=lang)

    # Names of translated attributes of translated attribute and related is_fuzzy one.
    trans_field = trans_attr(attr, lang)
    is_fuzzy_attr = trans_is_fuzzy_attr(attr, lang)

    # If the translated attribute exists and is not fuzzy, return it
    if hasattr(instance, trans_field) and hasattr(
            instance, is_fuzzy_attr) and not getattr(instance, is_fuzzy_attr):
        return getattr(instance, trans_field)

    # Otherwise, return its original value
    return getattr(instance, attr)
Пример #6
0
	def _add_translation_form_fields(self):
		# Model of the ModelForm
		cls = self._meta.model
		# If Model has no translatable fields
		if not hasattr(cls,"_meta") or not hasattr(cls._meta,"translatable_fields") or len(cls._meta.translatable_fields)==0:
			return False

		# If it is an edition, load the current translations of the object
		if self.instance:
			self.instance.load_translations()

		# For each translatable field a new field for each language is created
		translatable_fields = cls._meta.translatable_fields

		_ = lambda x : x

		for translatable_field in translatable_fields:
			# Add additional language fields only if that field is present
			# in the ModelForm
			if (hasattr(self._meta, "fields") and self._meta.fields and translatable_field in self._meta.fields) or\
				(hasattr(self._meta, "exclude") and self._meta.exclude and not translatable_field in self._meta.exclude):

				self.fields[translatable_field].widget.is_translatable = True
				self.fields[translatable_field].widget.translation_group = translatable_field
				self.fields[translatable_field].widget.lang = settings.LANGUAGE_CODE

				if not settings.IS_MONOLINGUAL:
					for language_pair in settings.LANGUAGES:
						lang = language_pair[0]
						language_name = language_pair[1]

						if lang != settings.LANGUAGE_CODE:
							# Adds a translatable field
							# It is empty by default, its language must not be current language and
							# it should not be required
							field_lang = trans_attr(translatable_field, lang)
							self.fields[field_lang] = copy.deepcopy(self.fields[translatable_field])
							self.fields[field_lang].label += u" ({0})".format(language_name)
							self.fields[field_lang].initial = ""
							self.fields[field_lang].widget.lang = lang
							self.fields[field_lang].required = False

							# Original field label
							field_label = self.fields[translatable_field].label

							# If we are editing a Model instance, sets its correct initial values
							if self.instance and hasattr(self.instance, field_lang):
								self.fields[field_lang].initial = getattr(self.instance, field_lang)

							# is_fuzzy fields
							isfuzzy_lang = trans_is_fuzzy_attr(translatable_field,lang)
							self.fields[isfuzzy_lang] = forms.ChoiceField(
								choices=(
									(u"0",_(u"No necesita revisión")),
									(u"1", _(u"Necesita revisión"))),
								label=_(u"'{0}' necesita revisión para idioma {1}").format(field_label, language_name), initial="1")
							self.fields[isfuzzy_lang].widget.attrs["class"] = "is_fuzzy"
							if self.instance and hasattr(self.instance, isfuzzy_lang):
								if getattr(self.instance, isfuzzy_lang):
									self.fields[isfuzzy_lang].initial = "1"
								else:
									self.fields[isfuzzy_lang].initial = "0"
		return True