def set_field_translation(request): c_type = ContentType.objects.get(pk=request.GET['content_type']) original = c_type.get_object_for_this_type(pk=request.GET['object_id']) original_value = getattr(original, request.GET['field_name']) # Creates objects for translation for available languages for lang, display in settings.LANGUAGES: TranslatedField.objects.get_or_create( language=format_language(lang), content_type=c_type, object_id=request.GET['object_id'], field_name=request.GET['field_name'], ) # Gets queryset with translations translations = TranslatedField.objects.filter( content_type=c_type, object_id=request.GET['object_id'], field_name=request.GET['field_name'], ) # Makes the formset class FormSet = modelformset_factory( TranslatedField, form=FormTranslatedField, max_num=len(settings.LANGUAGES), extra=len(settings.LANGUAGES), ) if request.method == 'POST': formset = FormSet(request.POST, queryset=translations) if formset.is_valid(): formset.save() return HttpResponse( '<script type="text/javascript">window.close()</script>') else: formset = FormSet(queryset=translations) return render_to_response( 'i18n_functions/set_field_translation.html', locals(), context_instance=RequestContext(request), )
def set_field_translation(request): c_type = ContentType.objects.get(pk=request.GET['content_type']) original = c_type.get_object_for_this_type(pk=request.GET['object_id']) original_value = getattr(original, request.GET['field_name']) # Creates objects for translation for available languages for lang, display in settings.LANGUAGES: TranslatedField.objects.get_or_create( language=format_language(lang), content_type=c_type, object_id=request.GET['object_id'], field_name=request.GET['field_name'], ) # Gets queryset with translations translations = TranslatedField.objects.filter( content_type=c_type, object_id=request.GET['object_id'], field_name=request.GET['field_name'], ) # Makes the formset class FormSet = modelformset_factory( TranslatedField, form=FormTranslatedField, max_num=len(settings.LANGUAGES), extra=len(settings.LANGUAGES), ) if request.method == 'POST': formset = FormSet(request.POST, queryset=translations) if formset.is_valid(): formset.save() return HttpResponse('<script type="text/javascript">window.close()</script>') else: formset = FormSet(queryset=translations) return render_to_response( 'i18n_functions/set_field_translation.html', locals(), context_instance=RequestContext(request), )
def ugettext_field(obj, field_name): """ This function returns the translation for field value in the given object. It uses model class TranslatedField to find translation value or just returns the current field value if it doesn't exist or just it's empty. """ # Gets from cache if it exists cached_value = TranslatedField.objects.get_from_cache(obj, field_name) if cached_value: return cached_value c_type = ContentType.objects.get_for_model(obj) try: trans = TranslatedField.objects.get( language=format_language(get_language()), content_type=c_type, object_id=obj.pk, field_name=field_name, ) result = trans.value except TranslatedField.DoesNotExist: result = '' # If doesn't exists or is empty, get field value instead if not unicode(result).strip(): result = getattr(obj, field_name) # Replaces static URLs for that including current language result = result.replace('/static/','/static/%s/' % get_language()) # Stores in cache TranslatedField.objects.save_to_cache(obj, field_name, result) return result