Example #1
0
def cast_to_compare_locales(resource_ext, entity, string):
    """
    Cast a Pontoon's translation object into Entities supported by `compare-locales`.

    :arg basestring resource_ext: extension of a resource.
    :arg pontoon.base.models.Entity entity: Source entity
    :arg basestring string: a translation
    :return: source entity and translation entity that will be compatible with
        a compare-locales checker. Type of those entities depends on the resource_ext.
    """
    if resource_ext == '.properties':
        return (
            ComparePropertiesEntity(
                entity.key,
                entity.string,
                CommentEntity(entity.comment)
            ),
            ComparePropertiesEntity(
                entity.key,
                string,
                CommentEntity(entity.comment),
            )
        )

    elif resource_ext == '.dtd':
        return (
            CompareDTDEntity(
                entity.key,
                entity.string,
                CommentEntity(entity.comment),
            ),
            CompareDTDEntity(
                entity.key,
                string,
                CommentEntity(entity.comment),
            )
        )

    elif resource_ext == '.ftl':
        parser = FluentParser()

        parser.readUnicode(entity.string)
        refEntity, = list(parser)

        parser.readUnicode(string)
        trEntity = list(parser)[0] if list(parser) else None

        if not trEntity or isinstance(trEntity, Junk):
            raise UnsupportedStringError(resource_ext)

        return (
            refEntity,
            trEntity,
        )

    raise UnsupportedResourceTypeError(resource_ext)
Example #2
0
def cast_to_compare_locales(resource_ext, entity, string):
    """
    Cast a Pontoon's translation object into Entities supported by `compare-locales`.

    :arg basestring resource_ext: extension of a resource.
    :arg pontoon.base.models.Entity entity: Source entity
    :arg basestring string: a translation
    :return: source entity and translation entity that will be compatible with
        a compare-locales checker. Type of those entities depends on the resource_ext.
    """
    if resource_ext == ".properties":
        return (
            ComparePropertiesEntity(entity.key, entity.string,
                                    CommentEntity(entity.comment)),
            ComparePropertiesEntity(
                entity.key,
                string,
                CommentEntity(entity.comment),
            ),
        )

    elif resource_ext == ".dtd":
        return (
            CompareDTDEntity(
                entity.key,
                entity.string,
                CommentEntity(entity.comment),
            ),
            CompareDTDEntity(
                entity.key,
                string,
                CommentEntity(entity.comment),
            ),
        )

    elif resource_ext == ".ftl":
        parser = FluentParser()

        parser.readUnicode(entity.string)
        (refEntity, ) = list(parser)

        parser.readUnicode(string)
        trEntity = list(parser)[0] if list(parser) else None

        if not trEntity or isinstance(trEntity, Junk):
            raise UnsupportedStringError(resource_ext)

        return (
            refEntity,
            trEntity,
        )

    elif resource_ext == ".xml":
        parser = AndroidParser()

        content = u"""<?xml version="1.0" encoding="utf-8"?>
            <resources>
                <string name="{key}"><![CDATA[{original}]]></string>
                <string name="{key}"><![CDATA[{translation}]]></string>
            </resources>
        """.format(
            key=entity.key,
            original=entity.string,
            translation=string,
        )

        parser.readUnicode(content)
        parsed_objects = list(parser.parse())

        refEntity = parsed_objects[0]
        trEntity = parsed_objects[1]

        if isinstance(trEntity, Junk):
            raise UnsupportedStringError(resource_ext)

        return (
            refEntity,
            trEntity,
        )

    raise UnsupportedResourceTypeError(resource_ext)
Example #3
0
def cast_to_compare_locales(resource_ext, entity, string):
    """
    Cast a Pontoon's translation object into Entities supported by `compare-locales`.

    :arg basestring resource_ext: extension of a resource.
    :arg pontoon.base.models.Entity entity: Source entity
    :arg basestring string: a translation
    :return: source entity and translation entity that will be compatible with
        a compare-locales checker. Type of those entities depends on the resource_ext.
    """
    if resource_ext == '.properties':
        return (
            ComparePropertiesEntity(
                entity.key,
                entity.string,
                CommentEntity(entity.comment)
            ),
            ComparePropertiesEntity(
                entity.key,
                string,
                CommentEntity(entity.comment),
            )
        )

    elif resource_ext == '.dtd':
        return (
            CompareDTDEntity(
                entity.key,
                entity.string,
                CommentEntity(entity.comment),
            ),
            CompareDTDEntity(
                entity.key,
                string,
                CommentEntity(entity.comment),
            )
        )

    elif resource_ext == '.ftl':
        parser = FluentParser()

        parser.readUnicode(entity.string)
        refEntity, = list(parser)

        parser.readUnicode(string)
        trEntity = list(parser)[0] if list(parser) else None

        if not trEntity or isinstance(trEntity, Junk):
            raise UnsupportedStringError(resource_ext)

        return (
            refEntity,
            trEntity,
        )

    elif resource_ext == '.xml':
        parser = AndroidParser()

        content = u"""<?xml version="1.0" encoding="utf-8"?>
            <resources>
                <string name="{key}"><![CDATA[{original}]]></string>
                <string name="{key}"><![CDATA[{translation}]]></string>
            </resources>
        """.format(
            key=entity.key,
            original=entity.string,
            translation=string,
        )

        parser.readUnicode(content)
        parsed_objects = list(parser.parse())

        refEntity = parsed_objects[0]
        trEntity = parsed_objects[1]

        if isinstance(trEntity, Junk):
            raise UnsupportedStringError(resource_ext)

        return (
            refEntity,
            trEntity,
        )

    raise UnsupportedResourceTypeError(resource_ext)