예제 #1
0
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]
        }
예제 #2
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
예제 #3
0
파일: __init__.py 프로젝트: tymofij/adofex
 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
예제 #4
0
파일: __init__.py 프로젝트: tymofij/adofex
 def mimetype(cls, r):
     """
     Return the mimetype in a GET request instead of the i18n_type.
     """
     return registry.mimetypes_for(r.i18n_method)[0]
예제 #5
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
예제 #6
0
    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

예제 #7
0
 def mimetype(cls, r):
     """
     Return the mimetype in a GET request instead of the i18n_type.
     """
     return registry.mimetypes_for(r.i18n_method)[0]