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))
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, 'w') 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, ) )
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
def save(self, locale): if not self.source_resource: raise SyncError( f"Cannot save resource {self.path}: No source resource given." ) # 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, ) )
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) 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(self.path) with codecs.open(self.path, 'w+', 'utf-8') as f: log.debug('Saving file: %s', self.path) f.write(serializer.serialize(structure))
def save_json_file(self, json_file): 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, separators=(",", ": "))) f.write("\n") # Add newline
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))
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) is 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))