class StringTranslation(Translation): """ Handle requests for translation as strings. """ def get(self, start=None, end=None, pseudo_type=None, mode=None): """ Return the requested translation in a json string. If self.language is None, return all translations. Args: start: Start for pagination. end: End for pagination. pseudo_type: The pseudo_type requested. mode: The mode for the compilation. Returns: A dict with the translation(s). Raises: BadRequestError: There was a problem with the request. """ try: fb = FormatsBackend(self.resource, self.language) template = fb.compile_translation(pseudo_type, mode=mode) except Exception, e: logger.error(unicode(e), exc_info=True) raise BadRequestError("Error compiling the translation file: %s" % e) if self.resource.i18n_method == 'PROPERTIES': template = template.decode('ISO-8859-1') return { 'content': template, 'mimetype': registry.mimetypes_for(self.resource.i18n_method)[0] }
def to_http_for_get(cls, translation, result): response = HttpResponse(result, mimetype=registry.mimetypes_for( translation.resource.i18n_method)[0]) response['Content-Disposition'] = ( 'attachment; filename*="UTF-8\'\'%s_%s%s"' % (urllib.quote(translation.resource.name.encode('UTF-8')), translation.language.code, registry.file_extension_for(translation.resource, translation.language))) return response
def to_http_for_get(cls, translation, result): response = HttpResponse( result, mimetype=registry.mimetypes_for( translation.resource.i18n_method )[0] ) response['Content-Disposition'] = ( 'attachment; filename*="UTF-8\'\'%s_%s%s"' % ( urllib.quote(translation.resource.name.encode('UTF-8')), translation.language.code, registry.file_extension_for( translation.resource, translation.language ) ) ) return response
def mimetype(cls, r): """ Return the mimetype in a GET request instead of the i18n_type. """ return registry.mimetypes_for(r.i18n_method)[0]
slug = resource_slug) language = get_object_or_404(Language, code=lang_code) try: fb = FormatsBackend(resource, language) template = fb.compile_translation(**kwargs) except Exception, e: messages.error(request, "Error compiling translation file.") logger.error("Error compiling '%s' file for '%s': %s" % (language, resource, str(e))) return HttpResponseRedirect(reverse('resource_detail', args=[resource.project.slug, resource.slug]),) response = HttpResponse( template, mimetype=registry.mimetypes_for(resource.i18n_method)[0] ) _filename = "%(proj)s_%(res)s_%(lang)s%(type)s" % { 'proj': smart_unicode(resource.project.slug), 'res': smart_unicode(resource.slug), 'lang': language.code, 'type': registry.file_extension_for(resource, language) } # Prefix filename with mode, case it exists if kwargs.has_key('mode'): _filename = "%s_" % kwargs.get('mode').label + _filename response['Content-Disposition'] = ('attachment; filename=%s' % _filename) return response
language = get_object_or_404(Language, code=lang_code) try: fb = FormatsBackend(resource, language) template = fb.compile_translation(**kwargs) except Exception, e: messages.error(request, "Error compiling translation file.") logger.error("Error compiling '%s' file for '%s': %s" % (language, resource, str(e))) return HttpResponseRedirect( reverse('resource_detail', args=[resource.project.slug, resource.slug]), ) response = HttpResponse(template, mimetype=registry.mimetypes_for( resource.i18n_method)[0]) _filename = "%(proj)s_%(res)s_%(lang)s%(type)s" % { 'proj': smart_unicode(resource.project.slug), 'res': smart_unicode(resource.slug), 'lang': language.code, 'type': registry.file_extension_for(resource, language) } # Prefix filename with mode, case it exists if kwargs.has_key('mode'): _filename = "%s_" % kwargs.get('mode').label + _filename response['Content-Disposition'] = ('attachment; filename=%s' % _filename) return response