示例#1
0
    def run_save_translation_identical(self,
                                       source_string,
                                       input_string,
                                       expected_string,
                                       expected_translation=None):
        """
        If the updated translation is identical to the source
        translation, keep it.

        Source Example:
            String=Source String

        Input Example:
            String=Translated String

        Expected Example:
            String=Source String
        """
        path, resource = self.parse_string(input_string,
                                           source_string=source_string)

        translation = match_attr(resource.translations, key="String")
        translation.strings = {None: expected_translation or "Source String"}
        resource.save(self.locale)

        self.assert_file_content(path, expected_string)
示例#2
0
    def run_save_translation_missing(
        self, source_string, input_string, expected_string, expected_translation=None
    ):
        """
        If the source resource has a string but the translated resource
        doesn't, the returned resource should have an empty translation
        that can be modified and saved.

        Source Example:
            String=Source String
            MissingString=Missing Source String

        Input Example:
            String=Translated String

        Expected Example:
            String=Translated String
            MissingString=Translated Missing String
        """
        path, resource = self.parse_string(input_string, source_string=source_string)

        missing_translation = match_attr(resource.translations, key=self.key('Missing String'))
        missing_translation.strings = {None: expected_translation or 'Translated Missing String'}
        resource.save(self.locale)

        self.assert_file_content(path, expected_string)
示例#3
0
    def run_save_translation_missing(self,
                                     source_string,
                                     input_string,
                                     expected_string,
                                     expected_translation=None):
        """
        If the source resource has a string but the translated resource
        doesn't, the returned resource should have an empty translation
        that can be modified and saved.

        Source Example:
            String=Source String
            MissingString=Missing Source String

        Input Example:
            String=Translated String

        Expected Example:
            String=Translated String
            MissingString=Translated Missing String
        """
        path, resource = self.parse_string(input_string,
                                           source_string=source_string)
        missing_translation = match_attr(resource.translations,
                                         key=self.key("Missing String"))
        missing_translation.strings = {
            None: expected_translation or "Translated Missing String"
        }
        resource.save(self.locale)

        self.assert_file_content(path, expected_string)
示例#4
0
    def execute_update_db(self):
        for locale_code, db_entity, vcs_entity in self.changes['update_db']:
            for field, value in self.get_entity_updates(vcs_entity).items():
                setattr(db_entity, field, value)

            if db_entity.is_dirty(check_relationship=True):
                self.entities_to_update.append(db_entity)

            # Update translations for the entity.
            vcs_translation = vcs_entity.translations[locale_code]
            db_translations = db_entity.translation_set.filter(
                locale__code=locale_code,
            )
            approved_translations = []

            for plural_form, string in vcs_translation.strings.items():
                # Check if we need to modify an existing translation or
                # create a new one.
                db_translation = match_attr(db_translations,
                                            plural_form=plural_form,
                                            string=string)
                if db_translation:
                    if not db_translation.approved:
                        db_translation.approved = True
                        db_translation.approved_date = self.now
                    db_translation.fuzzy = vcs_translation.fuzzy
                    db_translation.extra = vcs_translation.extra

                    if db_translation.is_dirty():
                        self.translations_to_update.append(db_translation)
                    if not db_translation.fuzzy:
                        approved_translations.append(db_translation)
                else:
                    self.translations_to_create.append(Translation(
                        entity=db_entity,
                        locale=self.locales[locale_code],
                        string=string,
                        plural_form=plural_form,
                        approved=not vcs_translation.fuzzy,
                        approved_date=self.now if not vcs_translation.fuzzy else None,
                        fuzzy=vcs_translation.fuzzy,
                        extra=vcs_translation.extra
                    ))

            # Any existing translations that were not approved get unapproved.
            for translation in db_translations.filter(approved_date__lte=self.now):
                if translation not in approved_translations:
                    translation.approved = False
                    translation.approved_user = None
                    translation.approved_date = None

                    if translation.is_dirty():
                        self.translations_to_update.append(translation)
示例#5
0
    def execute_update_db(self):
        for locale_code, db_entity, vcs_entity in self.changes['update_db']:
            for field, value in self.get_entity_updates(vcs_entity).items():
                setattr(db_entity, field, value)

            if db_entity.is_dirty(check_relationship=True):
                self.entities_to_update.append(db_entity)

            # Update translations for the entity.
            vcs_translation = vcs_entity.translations[locale_code]
            db_translations = db_entity.translation_set.filter(
                locale__code=locale_code, )
            approved_translations = []

            for plural_form, string in vcs_translation.strings.items():
                # Check if we need to modify an existing translation or
                # create a new one.
                db_translation = match_attr(db_translations,
                                            plural_form=plural_form,
                                            string=string)
                if db_translation:
                    if not db_translation.approved:
                        db_translation.approved = True
                        db_translation.approved_date = self.now
                    db_translation.fuzzy = vcs_translation.fuzzy
                    db_translation.extra = vcs_translation.extra

                    if db_translation.is_dirty():
                        self.translations_to_update.append(db_translation)
                    if not db_translation.fuzzy:
                        approved_translations.append(db_translation)
                else:
                    self.translations_to_create.append(
                        Translation(entity=db_entity,
                                    locale=self.locales[locale_code],
                                    string=string,
                                    plural_form=plural_form,
                                    approved=not vcs_translation.fuzzy,
                                    approved_date=self.now
                                    if not vcs_translation.fuzzy else None,
                                    fuzzy=vcs_translation.fuzzy,
                                    extra=vcs_translation.extra))

            # Any existing translations that were not approved get unapproved.
            for translation in db_translations.filter(
                    approved_date__lte=self.now):
                if translation not in approved_translations:
                    translation.approved = False
                    translation.approved_user = None
                    translation.approved_date = None

                    if translation.is_dirty():
                        self.translations_to_update.append(translation)
示例#6
0
    def update_entity_translations_from_vcs(
            self, db_entity, locale_code, vcs_translation,
            user=None, db_translations=None, old_translations=None
    ):
        if db_translations is None:
            db_translations = db_entity.translation_set.filter(
                locale__code=locale_code,
            )
        approved_translations = []

        for plural_form, string in vcs_translation.strings.items():
            # Check if we need to modify an existing translation or
            # create a new one.
            db_translation = match_attr(db_translations,
                                        plural_form=plural_form,
                                        string=string)
            if db_translation:
                if not db_translation.approved and not vcs_translation.fuzzy:
                    db_translation.approved = True
                    db_translation.approved_date = self.now
                    db_translation.approved_user = user
                db_translation.fuzzy = vcs_translation.fuzzy
                db_translation.extra = vcs_translation.extra

                if db_translation.is_dirty():
                    self.translations_to_update.append(db_translation)
                if not db_translation.fuzzy:
                    approved_translations.append(db_translation)
            else:
                self.translations_to_create.append(Translation(
                    entity=db_entity,
                    locale=self.locales[locale_code],
                    string=string,
                    plural_form=plural_form,
                    approved=not vcs_translation.fuzzy,
                    approved_date=self.now if not vcs_translation.fuzzy else None,
                    approved_user=user,
                    user=user,
                    fuzzy=vcs_translation.fuzzy,
                    extra=vcs_translation.extra
                ))

        # Any existing translations that were not approved get unapproved.
        if old_translations is None:
            old_translations = db_translations.filter(approved_date__lte=self.now)
        for translation in old_translations:
            if translation not in approved_translations:
                translation.approved = False
                translation.approved_user = None
                translation.approved_date = None

                if translation.is_dirty():
                    self.translations_to_update.append(translation)
示例#7
0
    def execute_update_db(self):
        # TODO: Optimize to only update translations if necessary.
        for locale_code, db_entity, vcs_entity in self.changes["update_db"]:
            for field, value in self.get_entity_updates(vcs_entity).items():
                setattr(db_entity, field, value)
            self.entities_to_update.append(db_entity)

            # Update translations for the entity.
            vcs_translation = vcs_entity.translations[locale_code]
            db_translations = db_entity.translation_set.filter(locale__code=locale_code)
            approved_translations = []

            for plural_form, string in vcs_translation.strings.items():
                # Check if we need to modify an existing translation or
                # create a new one.
                db_translation = match_attr(db_translations, plural_form=plural_form, string=string)
                if db_translation:
                    if not db_translation.approved:
                        db_translation.approved = True
                        db_translation.approved_date = timezone.now()
                    db_translation.fuzzy = vcs_translation.fuzzy
                    db_translation.extra = vcs_translation.extra

                    self.translations_to_update.append(db_translation)
                    approved_translations.append(db_translation)
                else:
                    self.translations_to_create.append(
                        Translation(
                            entity=db_entity,
                            locale=self.locales[locale_code],
                            string=string,
                            plural_form=plural_form,
                            approved=True,
                            approved_date=timezone.now(),
                            fuzzy=vcs_translation.fuzzy,
                            extra=vcs_translation.extra,
                        )
                    )

            # Any existing translations that were not approved get unapproved.
            for translation in db_translations:
                if translation not in approved_translations:
                    translation.approved = False
                    translation.approved_user = None
                    translation.approved_date = None
                    self.translations_to_update.append(translation)
示例#8
0
    def run_save_translation_identical(self, source_string, input_string, expected_string):
        """
        If the updated translation is identical to the source
        translation, keep it.

        Source Example:
            String=Source String

        Input Example:
            String=Translated String

        Expected Example:
            String=Source String
        """
        path, resource = self.parse_string(input_string, source_string=source_string)

        translation = match_attr(resource.translations, key='String')
        translation.strings = {None: 'Source String'}
        resource.save(self.locale)

        self.assert_file_content(path, expected_string)
示例#9
0
    def update_entity_translations_from_vcs(
            self,
            db_entity,
            locale_code,
            vcs_translation,
            user=None,
            db_translations=None,
            db_translations_approved_before_sync=None):
        if db_translations is None:
            db_translations = db_entity.translation_set.filter(
                locale__code=locale_code, )

        if db_translations_approved_before_sync is None:
            db_translations_approved_before_sync = db_translations.filter(
                approved_date__lte=self.now)

        approved_translations = []
        fuzzy_translations = []

        for plural_form, string in vcs_translation.strings.items():
            db_translation = match_attr(db_translations,
                                        plural_form=plural_form,
                                        string=string)

            # Modify existing translation.
            if db_translation:
                if not db_translation.approved and not vcs_translation.fuzzy:
                    db_translation.approved = True
                    db_translation.approved_user = user
                    db_translation.approved_date = self.now
                db_translation.rejected = False
                db_translation.fuzzy = vcs_translation.fuzzy
                db_translation.extra = vcs_translation.extra

                if db_translation.is_dirty():
                    self.translations_to_update[
                        db_translation.pk] = db_translation
                if db_translation.fuzzy:
                    fuzzy_translations.append(db_translation)
                else:
                    approved_translations.append(db_translation)

            # Create new translation.
            else:
                self.translations_to_create.append(
                    Translation(entity=db_entity,
                                locale=self.locales[locale_code],
                                string=string,
                                plural_form=plural_form,
                                approved=not vcs_translation.fuzzy,
                                approved_user=user,
                                approved_date=self.now
                                if not vcs_translation.fuzzy else None,
                                user=user,
                                fuzzy=vcs_translation.fuzzy,
                                extra=vcs_translation.extra))

        # Unapprove translations that were approved before the sync job started unless sync
        # resolves them as active approved translations.
        # Note: If translations get approved after the sync starts, duplicate approvals can arise.
        # We take care of that at the and of the sync job in tasks.py.
        for translation in db_translations_approved_before_sync:
            if translation not in approved_translations:
                # Use the translation instance already set for update if it exists.
                translation = self.translations_to_update.get(
                    translation.pk, translation)
                translation.approved = False
                translation.approved_user = None
                translation.approved_date = None

                # Reject translations unless they became fuzzy during sync. Condition is sufficient
                # because they were approved previously.
                if not translation.fuzzy:
                    translation.rejected = True
                    translation.rejected_user = user
                    translation.rejected_date = self.now

                if translation.is_dirty():
                    self.translations_to_update[translation.pk] = translation

        # Unfuzzy existing translations unless sync resolves them as active fuzzy translations.
        # Note: Translations cannot get fuzzy after the sync job starts, because they cannot be
        # made fuzzy in Pontoon.
        for translation in db_translations:
            if translation not in fuzzy_translations:
                # Use the translation instance already set for update if it exists.
                translation = self.translations_to_update.get(
                    translation.pk, translation)
                translation.fuzzy = False

                if translation.is_dirty():
                    self.translations_to_update[translation.pk] = translation
示例#10
0
    def update_entity_translations_from_vcs(self,
                                            db_entity,
                                            locale_code,
                                            vcs_translation,
                                            user=None,
                                            db_translations=None,
                                            old_translations=None):
        if db_translations is None:
            db_translations = db_entity.translation_set.filter(
                locale__code=locale_code, )
        approved_translations = []
        fuzzy_translations = []

        for plural_form, string in vcs_translation.strings.items():
            # Check if we need to modify an existing translation or
            # create a new one.
            db_translation = match_attr(db_translations,
                                        plural_form=plural_form,
                                        string=string)
            if db_translation:
                if not db_translation.approved and not vcs_translation.fuzzy:
                    db_translation.approved = True
                    db_translation.approved_date = self.now
                    db_translation.approved_user = user
                db_translation.fuzzy = vcs_translation.fuzzy
                db_translation.extra = vcs_translation.extra

                if db_translation.is_dirty():
                    self.translations_to_update.append(db_translation)
                if not db_translation.fuzzy:
                    approved_translations.append(db_translation)
                else:
                    fuzzy_translations.append(db_translation)
            else:
                self.translations_to_create.append(
                    Translation(entity=db_entity,
                                locale=self.locales[locale_code],
                                string=string,
                                plural_form=plural_form,
                                approved=not vcs_translation.fuzzy,
                                approved_date=self.now
                                if not vcs_translation.fuzzy else None,
                                approved_user=user,
                                user=user,
                                fuzzy=vcs_translation.fuzzy,
                                extra=vcs_translation.extra))

        # Any existing translations that were not approved get unapproved.
        if old_translations is None:
            old_translations = db_translations.filter(
                approved_date__lte=self.now)

        for translation in old_translations:
            if translation not in approved_translations:
                translation.approved = False
                translation.approved_user = None
                translation.approved_date = None

                if translation.is_dirty():
                    self.translations_to_update.append(translation)

        # Any existing translations that are no longer fuzzy get unfuzzied.
        for translation in db_translations:
            if translation not in fuzzy_translations:
                translation.fuzzy = False

                if translation.is_dirty():
                    self.translations_to_update.append(translation)
示例#11
0
文件: changeset.py 项目: Pike/pontoon
    def update_entity_translations_from_vcs(
            self, db_entity, locale_code, vcs_translation,
            user=None, db_translations=None, db_translations_approved_before_sync=None
    ):
        if db_translations is None:
            db_translations = db_entity.translation_set.filter(
                locale__code=locale_code,
            )

        if db_translations_approved_before_sync is None:
            db_translations_approved_before_sync = db_translations.filter(
                approved_date__lte=self.now
            )

        approved_translations = []
        fuzzy_translations = []

        for plural_form, string in vcs_translation.strings.items():
            db_translation = match_attr(db_translations, plural_form=plural_form, string=string)

            # Modify existing translation.
            if db_translation:
                if not db_translation.approved and not vcs_translation.fuzzy:
                    db_translation.approved = True
                    db_translation.approved_user = user
                    db_translation.approved_date = self.now
                db_translation.rejected = False
                db_translation.fuzzy = vcs_translation.fuzzy
                db_translation.extra = vcs_translation.extra

                if db_translation.is_dirty():
                    self.translations_to_update[db_translation.pk] = db_translation
                if db_translation.fuzzy:
                    fuzzy_translations.append(db_translation)
                else:
                    approved_translations.append(db_translation)

            # Create new translation.
            else:
                self.translations_to_create.append(Translation(
                    entity=db_entity,
                    locale=self.locales[locale_code],
                    string=string,
                    plural_form=plural_form,
                    approved=not vcs_translation.fuzzy,
                    approved_user=user,
                    approved_date=self.now if not vcs_translation.fuzzy else None,
                    user=user,
                    fuzzy=vcs_translation.fuzzy,
                    extra=vcs_translation.extra
                ))

        # Unapprove translations that were approved before the sync job started unless sync
        # resolves them as active approved translations.
        # Note: If translations get approved after the sync starts, duplicate approvals can arise.
        # We take care of that at the and of the sync job in tasks.py.
        for translation in db_translations_approved_before_sync:
            if translation not in approved_translations:
                # Use the translation instance already set for update if it exists.
                translation = self.translations_to_update.get(translation.pk, translation)
                translation.approved = False
                translation.approved_user = None
                translation.approved_date = None

                # Reject translations unless they became fuzzy during sync. Condition is sufficient
                # because they were approved previously.
                if not translation.fuzzy:
                    translation.rejected = True
                    translation.rejected_user = user
                    translation.rejected_date = self.now

                if translation.is_dirty():
                    self.translations_to_update[translation.pk] = translation

        # Unfuzzy existing translations unless sync resolves them as active fuzzy translations.
        # Note: Translations cannot get fuzzy after the sync job starts, because they cannot be
        # made fuzzy in Pontoon.
        for translation in db_translations:
            if translation not in fuzzy_translations:
                # Use the translation instance already set for update if it exists.
                translation = self.translations_to_update.get(translation.pk, translation)
                translation.fuzzy = False

                if translation.is_dirty():
                    self.translations_to_update[translation.pk] = translation