def run_checks(entity, locale_code, string): """ Run all compare-locales checks on provided translation and entity. :arg pontoon.base.models.Entity entity: Source entity instance :arg basestring locale_code: Locale of a translation :arg basestring string: translation string :return: Dictionary with the following structure: { 'clErrors': [ 'Error1', ], 'clWarnings': [ 'Warning1', ] } Both keys are optional. """ resource_ext = ".{}".format(entity.resource.format) extra_tests = None if "mobile/android/base" in entity.resource.path: extra_tests = {"android-dtd"} entity.string = escape_quotes(entity.string) string = escape_quotes(string) source_ent, translation_ent = cast_to_compare_locales( resource_ext, entity, string, ) checker = getChecker( File(entity.resource.path, entity.resource.path, locale=locale_code), extra_tests, ) if checker is None: # compare-locales has no checks for this format, it's OK. return {} # Currently, references are required only by DTD files but that may change in the future. if checker.needs_reference: references = KeyedTuple( CompareDTDEntity( e.key, e.string, e.comment, ) for e in entity.resource.entities.all()) checker.set_reference(references) errors = {} for severity, _, message, _ in checker.check(source_ent, translation_ent): messages = errors.setdefault("cl%ss" % severity.capitalize(), []) # Old-school duplicate prevention - set() is not JSON serializable if message not in messages: messages.append(message) return errors
def run_checks(entity, locale_code, string): """ Run all compare-locales checks on provided translation and entity. :arg pontoon.base.models.Entity entity: Source entity instance :arg basestring locale_code: Locale of a translation :arg basestring string: translation string :return: Dictionary with the following structure: { 'clErrors': [ 'Error1', ], 'clWarnings': [ 'Warning1', ] } Both keys are optional. """ resource_ext = '.{}'.format(entity.resource.format) extra_tests = None if 'mobile/android/base' in entity.resource.path: extra_tests = {'android-dtd'} entity.string = escape_quotes(entity.string) string = escape_quotes(string) source_ent, translation_ent = cast_to_compare_locales( resource_ext, entity, string, ) checker = getChecker( File(entity.resource.path, entity.resource.path, locale=locale_code), extra_tests ) if checker is None: # compare-locales has no checks for this format, it's OK. return {} # Currently, references are required only by DTD files but that may change in the future. if checker.needs_reference: references = KeyedTuple( CompareDTDEntity( e.key, e.string, e.comment, ) for e in entity.resource.entities.all() ) checker.set_reference(references) errors = {} for severity, _, message, _ in checker.check(source_ent, translation_ent): messages = errors.setdefault('cl%ss' % severity.capitalize(), []) # Old-school duplicate prevention - set() is not JSON serializable if message not in messages: messages.append(message) return errors
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))