def __init__(self, vcs_project, path, locales=None): """ Load the resource file for each enabled locale and store its translations in VCSEntity instances. """ from pontoon.base.models import Locale from pontoon.sync import formats # Avoid circular import. self.vcs_project = vcs_project self.path = path self.locales = locales or [] self.files = {} self.entities = {} # Create entities using resources from the source directory, source_resource_path = os.path.join(vcs_project.source_directory_path, self.path) source_resource_path = locale_to_source_path(source_resource_path) source_resource_file = formats.parse( source_resource_path, locale=Locale.objects.get(code='en-US')) for index, translation in enumerate(source_resource_file.translations): vcs_entity = VCSEntity( resource=self, key=translation.key, string=translation.source_string, string_plural=translation.source_string_plural, comments=translation.comments, source=translation.source, order=translation.order or index) self.entities[vcs_entity.key] = vcs_entity # Fill in translations from the locale resources. for locale in locales: locale_directory = self.vcs_project.locale_directory_paths[ locale.code] resource_path = os.path.join(locale_directory, self.path) log.debug('Parsing resource file: %s', resource_path) try: resource_file = formats.parse(resource_path, source_resource_path, locale) except (IOError, ParseError): continue # File doesn't exist or is invalid, let's move on self.files[locale] = resource_file log.debug('Discovered %s translations.', len(resource_file.translations)) for translation in resource_file.translations: try: self.entities[translation.key].translations[ locale.code] = translation except KeyError: # If the source is missing an entity, we consider it # deleted and don't add it. pass
def __init__(self, vcs_project, path, locales=None): """ Load the resource file for each enabled locale and store its translations in VCSEntity instances. """ from pontoon.base.models import Locale from pontoon.sync import formats # Avoid circular import. self.vcs_project = vcs_project self.path = path self.locales = locales or [] self.files = {} self.entities = {} # Create entities using resources from the source directory, source_resource_path = os.path.join(vcs_project.source_directory_path, self.path) source_resource_path = locale_to_source_path(source_resource_path) source_resource_file = formats.parse( source_resource_path, locale=Locale.objects.get(code='en-US') ) for index, translation in enumerate(source_resource_file.translations): vcs_entity = VCSEntity( resource=self, key=translation.key, string=translation.source_string, string_plural=translation.source_string_plural, comments=translation.comments, source=translation.source, order=translation.order or index ) self.entities[vcs_entity.key] = vcs_entity # Fill in translations from the locale resources. for locale in locales: locale_directory = self.vcs_project.locale_directory_paths[locale.code] resource_path = os.path.join(locale_directory, self.path) log.debug('Parsing resource file: %s', resource_path) try: resource_file = formats.parse(resource_path, source_resource_path, locale) except (IOError, ParseError): continue # File doesn't exist or is invalid, let's move on self.files[locale] = resource_file log.debug('Discovered %s translations.', len(resource_file.translations)) for translation in resource_file.translations: try: self.entities[translation.key].translations[locale.code] = translation except KeyError: # If the source is missing an entity, we consider it # deleted and don't add it. pass
def __init__(self, vcs_project, path, locales=None): """ Load the resource file for each enabled locale and store its translations in VCSEntity instances. """ from pontoon.base.models import Locale from pontoon.sync import formats # Avoid circular import. self.vcs_project = vcs_project self.path = path self.locales = locales or [] self.files = {} self.entities = {} # Create entities using resources from the source directory, source_resource_path = os.path.join(vcs_project.source_directory_path, self.path) source_resource_path = locale_to_source_path(source_resource_path) source_resource_file = formats.parse( source_resource_path, locale=Locale.objects.get(code="en-US")) for index, translation in enumerate(source_resource_file.translations): vcs_entity = VCSEntity( resource=self, key=translation.key, string=translation.source_string, string_plural=translation.source_string_plural, comments=translation.comments, group_comments=(translation.group_comments if hasattr( translation, "group_comments") else None), resource_comments=(translation.resource_comments if hasattr( translation, "resource_comments") else None), source=translation.source, order=translation.order or index, ) self.entities[vcs_entity.key] = vcs_entity # Fill in translations from the locale resources. for locale in locales: locale_directory = self.vcs_project.locale_directory_paths[ locale.code] if self.vcs_project.configuration: # Some resources might not be available for this locale resource_path = self.vcs_project.configuration.l10n_path( locale, source_resource_path, ) if resource_path is None: continue else: resource_path = os.path.join(locale_directory, self.path) log.debug("Parsing resource file: %s", resource_path) try: resource_file = formats.parse(resource_path, source_resource_path, locale) # File doesn't exist or is invalid: log it and move on except (IOError, ParseError) as err: log.error( u"Skipping resource {path} due to {type}: {err}".format( path=path, type=type(err).__name__, err=err)) continue self.files[locale] = resource_file log.debug("Discovered %s translations.", len(resource_file.translations)) for translation in resource_file.translations: try: self.entities[translation.key].translations[ locale.code] = translation except KeyError: # If the source is missing an entity, we consider it # deleted and don't add it. pass