Exemple #1
0
def _get_stringset(post_data, resources, language, review=False, *args, **kwargs):
    """Return the source strings for the specified resources and language
    based on the filters active in the request.

    Filters are: translated|untranslated, specific user and specific
    resources, which must be a subset of the resources argument. Also, the
    user can select to search for a term, sort the columns and show more
    languages other than the selected one.
    """
    # Find a way to determine the source language of multiple resources #FIXME
    source_language = get_source_language(resources)
    try:
        source_strings = _get_source_strings_for_request(
            post_data, resources, source_language, language
        )
    except LotteBadRequestError, e:
        logger.warning("Error in lotte filters: %s" % e.message, exc_info=True)
        return HttpResponseBadRequest()
def _get_stringset(post_data,
                   resources,
                   language,
                   review=False,
                   session='',
                   *args,
                   **kwargs):
    """Return the source strings for the specified resources and language
    based on the filters active in the request.

    Filters are: translated|untranslated, specific user and specific
    resources, which must be a subset of the resources argument. Also, the
    user can select to search for a term, sort the columns and show more
    languages other than the selected one.
    """
    # Find a way to determine the source language of multiple resources #FIXME
    source_language = get_source_language(resources)
    try:
        source_strings = _get_source_strings_for_request(
            post_data, resources, source_language, language, session)
    except LotteBadRequestError, e:
        logger.warning("Error in lotte filters: %s" % e.message, exc_info=True)
        return HttpResponseBadRequest()
Exemple #3
0
def translate(request, project_slug, lang_code, resource_slug=None,
                     *args, **kwargs):
    """
    Main lotte view.
    """

    # Permissions handling
    # Project should always be available
    project = get_object_or_404(Project, slug=project_slug)
    team = Team.objects.get_or_none(project, lang_code)
    check = ProjectPermission(request.user)
    if not check.submit_translations(team or project) and not\
        check.maintain(project):
        return permission_denied(request)

    resources = []
    if resource_slug:
        resource_list = [get_object_or_404(Resource, slug=resource_slug,
            project=project)]
    else:
        resource_list = Resource.objects.filter(project=project)

        # Return a page explaining that the project has multiple source langs and
        # cannot be translated as a whole.
        if resource_list.values('source_language').distinct().count() > 1:
            messages.info(request,_(
                          "There are multiple source languages for this project. "
                          "You will only be able to translate resources for one "
                          "source language at a time."))
            return HttpResponseRedirect(reverse('project_detail',
                                        args=[project_slug]),)

    # Filter resources that are not accepting translations
    for resource in resource_list:
        if resource.accept_translations:
            resources.append(resource)

    # If no resource accepting translations, raise a 403
    if not resources:
        return permission_denied(request)

    target_language = Language.objects.by_code_or_alias_or_404(lang_code)

    # If it is an attempt to edit the source language, redirect the user to
    # resource_detail and show him a message explaining the reason.
    if target_language == get_source_language(resources):
        messages.error(request,_(
                       "Cannot edit the source language because this would "
                       "result in translation mismatches! If you want to "
                       "update the source strings consider using the transifex "
                       "command-line client."))
        if resource_slug:
            return HttpResponseRedirect(reverse('resource_detail',
                                                args=[project_slug,
                                                      resource_slug]),)
        else:
            return HttpResponseRedirect(reverse('project_detail',
                                                args=[project_slug]),)

    total_strings = SourceEntity.objects.filter(
        resource__in = resources).count()

    translated_strings = Translation.objects.filter(
        resource__in = resources,
        language = target_language,
        source_entity__pluralized=False,
        rule = 5).count()

    # Include counting of pluralized entities
    for pluralized_entity in SourceEntity.objects.filter(resource__in = resources,
                                                         pluralized=True):
        plurals_translated = Translation.objects.filter(
            language=target_language,
            source_entity=pluralized_entity).count()
        if plurals_translated == len(target_language.get_pluralrules()):
            translated_strings += 1

    if len(resources) > 1:
        translation_resource = None
    else:
        translation_resource = resources[0]

    contributors = User.objects.filter(pk__in=Translation.objects.filter(
        resource__in = resources,
        language = target_language,
        rule = 5).values_list("user", flat=True))

    lotte_init.send(None, request=request, resources=resources,
        language=target_language)

    if target_language in [team.language for team in project.available_teams]:
        team_language = True
    else:
        team_language = False

    GtModel = get_model('gtranslate', 'Gtranslate')
    try:
        auto_translate = GtModel.objects.get(project=project)
    except GtModel.DoesNotExist:
        auto_translate = None

    return render_to_response("translate.html",
        { 'project' : project,
          'resource' : translation_resource,
          'target_language' : target_language,
          'translated_strings': translated_strings,
          'untranslated_strings': total_strings - translated_strings,
          'contributors': contributors,
          'resources': resources,
          'resource_slug': resource_slug,
          'languages': Language.objects.all(),
          'auto_translate': auto_translate,
          'spellcheck_supported_langs' : SPELLCHECK_SUPPORTED_LANGS,
          'team_language': team_language
        },
        context_instance = RequestContext(request))
def translate(request,
              project_slug,
              lang_code,
              resource_slug=None,
              *args,
              **kwargs):
    """
    Main lotte view.
    """

    # Permissions handling
    # Project should always be available
    project = get_object_or_404(Project, slug=project_slug)
    team = Team.objects.get_or_none(project, lang_code)
    check = ProjectPermission(request.user)
    if not check.submit_translations(team or project) and not\
        check.maintain(project):
        return permission_denied(request)

    resources = []
    if resource_slug:
        resource_list = [
            get_object_or_404(Resource, slug=resource_slug, project=project)
        ]
    else:
        resource_list = Resource.objects.filter(project=project)

        # Return a page explaining that the project has multiple source langs and
        # cannot be translated as a whole.
        if resource_list.values('source_language').distinct().count() > 1:
            messages.info(
                request,
                _("There are multiple source languages for this project. "
                  "You will only be able to translate resources for one "
                  "source language at a time."))
            return HttpResponseRedirect(
                reverse('project_detail', args=[project_slug]), )

    # Filter resources that are not accepting translations
    for resource in resource_list:
        if resource.accept_translations:
            resources.append(resource)

    # If no resource accepting translations, raise a 403
    if not resources:
        return permission_denied(request)

    target_language = Language.objects.by_code_or_alias_or_404(lang_code)

    # If it is an attempt to edit the source language, redirect the user to
    # resource_detail and show him a message explaining the reason.
    if target_language == get_source_language(resources):
        messages.error(
            request,
            _("Cannot edit the source language because this would "
              "result in translation mismatches! If you want to "
              "update the source strings consider using the transifex "
              "command-line client."))
        if resource_slug:
            return HttpResponseRedirect(
                reverse('resource_detail', args=[project_slug,
                                                 resource_slug]), )
        else:
            return HttpResponseRedirect(
                reverse('project_detail', args=[project_slug]), )

    total_strings = SourceEntity.objects.filter(resource__in=resources).count()

    translated_strings = Translation.objects.filter(
        resource__in=resources,
        language=target_language,
        source_entity__pluralized=False,
        rule=5).count()

    reviewed_strings = Translation.objects.filter(
        resource__in=resources,
        language=target_language,
        source_entity__pluralized=False,
        rule=5,
        reviewed=True).count()

    # Include counting of pluralized entities
    for pluralized_entity in SourceEntity.objects.filter(
            resource__in=resources, pluralized=True):
        plurals_translated = Translation.objects.filter(
            language=target_language, source_entity=pluralized_entity).count()
        if plurals_translated == len(target_language.get_pluralrules()):
            translated_strings += 1

    if len(resources) > 1:
        translation_resource = None
    else:
        translation_resource = resources[0]

    contributors = User.objects.filter(pk__in=Translation.objects.filter(
        resource__in=resources, language=target_language, rule=5).values_list(
            "user", flat=True))

    lotte_init.send(None,
                    request=request,
                    resources=resources,
                    language=target_language)

    if target_language in [team.language for team in project.available_teams]:
        team_language = True
    else:
        team_language = False

    GtModel = get_model('gtranslate', 'Gtranslate')
    try:
        auto_translate = GtModel.objects.get(project=project)
    except GtModel.DoesNotExist:
        auto_translate = None
    """
    if cache.get('lotte_%s' % request.session.session_key, None):
        cache.delete('lotte_%s' % request.session.session_key)
    """

    #Set rtl to True if target_language is an RTL language
    rtl = False
    if target_language.code in settings.RTL_LANGUAGE_CODES:
        rtl = True

    return render_to_response("translate.html", {
        'project': project,
        'resource': translation_resource,
        'target_language': target_language,
        'translated_strings': translated_strings,
        'reviewed_strings': reviewed_strings,
        'untranslated_strings': total_strings - translated_strings,
        'contributors': contributors,
        'resources': resources,
        'resource_slug': resource_slug,
        'languages': Language.objects.all(),
        'auto_translate': auto_translate,
        'spellcheck_supported_langs': SPELLCHECK_SUPPORTED_LANGS,
        'team_language': team_language,
        'RTL': rtl,
    },
                              context_instance=RequestContext(request))