def save(self, locale):
        """
        Load the source resource, modify it with changes made to this
        Resource instance, and save it over the locale-specific
        resource.
        """
        if not self.source_resource:
            raise SyncError(
                'Cannot save FTL resource {0}: No source resource given.'.
                format(self.path))

        with codecs.open(self.source_resource.path, 'r', 'utf-8') as resource:
            structure = parser.parse(resource.read())

        entities = structure.body

        # Use list() to iterate over a copy, leaving original free to modify
        for obj in list(entities):
            if isinstance(obj, localizable_entries):
                index = entities.index(obj)
                key = get_key(obj)
                entity = self.entities[key]

                if entity.strings:
                    message = parser.parse_entry(entity.strings[None])
                    message.comment = obj.comment
                    entities[index] = message
                else:
                    del entities[index]

        create_parent_directory(self.path)

        with codecs.open(self.path, 'w+', 'utf-8') as f:
            log.debug('Saving file: %s', self.path)
            f.write(serializer.serialize(structure))
Exemple #2
0
    def save(self, locale):
        """
        Load the source resource, modify it with changes made to this
        Resource instance, and save it over the locale-specific
        resource.
        """
        if not self.source_resource:
            raise SyncError(
                'Cannot save JSON resource {0}: No source resource given.'
                .format(self.path)
            )

        with codecs.open(self.source_resource.path, 'r', 'utf-8') as resource:
            json_file = json.load(resource, object_pairs_hook=OrderedDict)

            try:
                validate(json_file, SCHEMA)
            except ValidationError as e:
                raise ParseError(e)

        # Iterate over a copy, leaving original free to modify
        for key, value in json_file.copy().items():
            entity = self.entities[key]

            if entity.strings:
                json_file[key]['message'] = entity.strings[None]
            else:
                del json_file[key]

        create_parent_directory(self.path)

        with codecs.open(self.path, 'w+', 'utf-8') as f:
            log.debug('Saving file: %s', self.path)
            f.write(json.dumps(json_file, ensure_ascii=False, indent=2))
            f.write('\n')  # Add newline
Exemple #3
0
    def save(self, locale):
        """
        Load the source resource, modify it with changes made to this
        Resource instance, and save it over the locale-specific
        resource.
        """
        if not self.source_resource:
            raise SyncError('Cannot save l20n resource {0}: No source resource given.'
                            .format(self.path))

        with codecs.open(self.source_resource.path, 'r', 'utf-8') as resource:
            structure = FTLParser().parseResource(resource.read())

        def serialize_entity(obj, entities):
            entity_id = obj['id']['name']
            translations = self.entities[entity_id].strings

            if translations:
                source = translations[None]
                obj['value'] = ParseContext(source).getPattern().toJSON()
            else:
                index = entities.index(obj)
                del entities[index]

        entities = structure[0]['body']

        # Use list() to iterate over a copy, leaving original free to modify
        for obj in list(entities):
            if obj['type'] == 'Entity':
                serialize_entity(obj, entities)

            elif obj['type'] == 'Section':
                index = entities.index(obj)
                section = entities[index]['body']

                for obj in list(section):
                    if obj['type'] == 'Entity':
                        serialize_entity(obj, section)

                # Remove section if empty
                if len(section) == 0:
                    del entities[index]

        # Create parent directory if it doesn't exist.
        try:
            os.makedirs(os.path.dirname(self.path))
        except OSError:
            pass  # Already exists, phew!

        with codecs.open(self.path, 'w+', 'utf-8') as f:
            f.write(FTLSerializer().serialize(structure[0]))
            log.debug('Saved file: %s', self.path)
Exemple #4
0
    def save(self, locale):
        """
        Load the source resource, modify it with changes made to this
        Resource instance, and save it over the locale-specific
        resource.
        """
        if self.source_resource is None:
            raise SyncError('Cannot save silme resource {0}: No source resource given.'
                            .format(self.path))

        # Only uncomment MOZ_LANGPACK_CONTRIBUTORS if we have a
        # translation for it
        new_structure = self.parser.get_structure(read_file(
            self.source_resource.path,
            uncomment_moz_langpack=self.entities.get('MOZ_LANGPACK_CONTRIBUTORS', False)
        ))

        # Update translations in the copied resource.
        entities = [
            SilmeEntity(obj) for obj in new_structure if isinstance(obj, silme.core.entity.Entity)
        ]
        for silme_entity in entities:
            key = silme_entity.key

            translated_entity = self.entities.get(key)
            if translated_entity and None in translated_entity.strings:
                new_structure.modify_entity(key, translated_entity.strings[None])
            else:
                # Remove untranslated entity and following newline
                pos = new_structure.entity_pos(key)
                new_structure.remove_entity(key)

                try:
                    line = new_structure[pos]
                except IndexError:
                    # No newline at end of file
                    continue

                if type(line) == unicode and line.startswith('\n'):
                    line = line[len('\n'):]
                    new_structure[pos] = line
                    if len(line) is 0:
                        new_structure.remove_element(pos)

        # Create parent directory if it doesn't exist.
        try:
            os.makedirs(os.path.dirname(self.path))
        except OSError:
            pass  # Already exists, phew!

        with codecs.open(self.path, 'w', 'utf-8') as f:
            f.write(self.parser.dump_structure(new_structure))
Exemple #5
0
    def save(self, locale):
        """
        Load the source resource, modify it with changes made to this
        Resource instance, and save it over the locale-specific
        resource.
        """
        if not self.source_resource:
            raise SyncError(
                'Cannot save FTL resource {0}: No source resource given.'.
                format(self.path))

        with codecs.open(self.source_resource.path, 'r', 'utf-8') as resource:
            structure = parser.parse(resource.read())

        entities = structure.body

        # Use list() to iterate over a copy, leaving original free to modify
        for obj in list(entities):
            if type(obj) == ast.Message:
                index = entities.index(obj)
                entity = self.entities[obj.id.name]

                if entity.strings:
                    message = parser.parse_entry(entity.strings[None])
                    message.comment = obj.comment
                    entities[index] = message
                else:
                    del entities[index]

        # Create parent directory if it doesn't exist.
        try:
            os.makedirs(os.path.dirname(self.path))
        except OSError:
            pass  # Already exists, phew!

        with codecs.open(self.path, 'w+', 'utf-8') as f:
            f.write(serializer.serialize(structure))
            log.debug('Saved file: %s', self.path)
Exemple #6
0
    def save(self, locale):
        if not self.source_resource:
            raise SyncError(
                "Cannot save resource {0}: No source resource given.".format(
                    self.path))

        # A dictionary of new translations
        new_l10n = {
            key: entity.strings[None] if entity.strings else None
            for key, entity in self.entities.items()
        }

        # Create parent folders if necessary
        create_parent_directory(self.path)

        with open(self.path, "wb") as output_file:
            log.debug("Saving file: %s", self.path)
            output_file.write(
                serializer.serialize(
                    self.path,
                    self.source_resource.parsed_objects,
                    self.parsed_objects,
                    new_l10n,
                ))
Exemple #7
0
    def save(self, locale):
        """
        Load the source resource, modify it with changes made to this
        Resource instance, and save it over the locale-specific
        resource.
        """
        if self.source_resource is None:
            raise SyncError(
                'Cannot save silme resource {0}: No source resource given.'.
                format(self.path))

        # Only uncomment MOZ_LANGPACK_CONTRIBUTORS if we have a
        # translation for it
        new_structure = self.parser.get_structure(
            read_file(self.source_resource.path,
                      uncomment_moz_langpack=self.entities.get(
                          'MOZ_LANGPACK_CONTRIBUTORS', False)))

        # Update translations in the copied resource.
        entities = [
            SilmeEntity(obj) for obj in new_structure
            if isinstance(obj, silme.core.entity.Entity)
        ]
        for silme_entity in entities:
            key = silme_entity.key

            translated_entity = self.entities.get(key)
            if translated_entity and None in translated_entity.strings:
                translation = translated_entity.strings[None]

                if self.escape_quotes_on:
                    translation = escape_quotes(translation)

                new_structure.modify_entity(key, translation)
            else:
                # Remove untranslated entity and following newline
                pos = new_structure.entity_pos(key)
                new_structure.remove_entity(key)

                try:
                    line = new_structure[pos]
                except IndexError:
                    # No newline at end of file
                    continue

                if type(line) == text_type and line.startswith('\n'):
                    line = line[len('\n'):]
                    new_structure[pos] = line
                    if len(line) == 0:
                        new_structure.remove_element(pos)

        # Temporary fix for bug 1236281 until bug 721211 lands
        if (self.path.endswith('browser/chrome/browser/browser.properties')
                and locale.code == 'zh-CN'):
            new_entity = silme.core.entity.Entity(
                'browser.startup.homepage', 'https://start.firefoxchina.cn')
            new_structure.add_entity(new_entity)
            new_structure.add_string('\n')

        create_parent_directory(self.path)

        with codecs.open(self.path, 'w', 'utf-8') as f:
            f.write(self.parser.dump_structure(new_structure))
Exemple #8
0
    def save(self, locale):
        """
        Load the source resource, modify it with changes made to this
        Resource instance, and save it over the locale-specific
        resource.
        """
        if not self.source_resource:
            raise SyncError('Cannot save l20n resource {0}: No source resource given.'
                            .format(self.path))

        with codecs.open(self.source_resource.path, 'r', 'utf-8') as resource:
            new_structure = L20nParser().parse(resource.read())

        def attr_key(entity, attr):
            """Returns key for the attributes of entity."""
            return '.'.join([entity.id.name, attr.id.name])

        to_remove = []
        for res_entity in new_structure.body:
            if res_entity.type == 'Entity':
                entity_id = res_entity.id.name
                translations = self.entities[entity_id].strings
                comments = self.entities[entity_id].comments

                if not translations:
                    entity_idx = new_structure.body.index(res_entity)

                    for comment_entity in new_structure.body[entity_idx-len(comments):entity_idx]:
                        to_remove.append((new_structure.body, comment_entity))

                    # Checks if any attribute contains translation, if not
                    # then we can remove wole entity.
                    has_attributes = any([
                        bool(self.entities[attr_key(res_entity, a)].strings)
                        for a in res_entity.attrs
                    ])

                    if has_attributes:
                        res_entity.value = None

                    else:
                        to_remove.append((new_structure.body, res_entity))
                        continue

                if res_entity.value and res_entity.value.type == 'String':
                    res_entity.value.content[0] = translations[None]

                elif res_entity.value and res_entity.value.type == 'Hash':
                    res_entity.value.items = [
                        ast.HashItem(
                            ast.Identifier(locale.get_relative_cldr_plural(plural_relative_id)),
                            ast.String([value], value),
                            False
                        ) for plural_relative_id, value in translations.items()]


                for attr in res_entity.attrs:
                    key = attr_key(res_entity, attr)
                    attr_translations = self.entities[key].strings
                    if not attr_translations:
                        to_remove.append((res_entity.attrs, attr))
                        continue
                    attr.value.content[0] = attr_translations[None]

        for parent, entity in to_remove:
            parent.remove(entity)

        # Create parent directory if it doesn't exist.
        try:
            os.makedirs(os.path.dirname(self.path))
        except OSError:
            pass  # Already exists, phew!

        with codecs.open(self.path, 'w+', 'utf-8') as f:
            f.write(Serializer().serialize(new_structure))
            log.debug("Saved file: %s", self.path)