Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle different locale #48

Merged
merged 19 commits into from
Nov 28, 2023
Merged

handle different locale #48

merged 19 commits into from
Nov 28, 2023

Conversation

ETLaurent
Copy link
Contributor

@ETLaurent ETLaurent commented Nov 20, 2023

Summary

Import nows detects if the locale found in the exported docs is different from the current site one.

  • If the site has only one locale configured, then the docs are automatically re-written with the site locale.
  • If the site has multiple locales configured, the user is given the possibility to abort the import or re-write the docs with the site locale

Note that the verification is done beforehand, meaning that no doc is imported yet when the user is asked about continuing or aborting the import.

Technically, when a locale is found different in the docs, if the site is configured with only one locale then the docs are automatically re-written with req.locale.

However, if multiple locales are configured in the i18n module, then we stop the import and ask the user what we should do.

  • If he chooses No, the import is stopped and the export docs folder is deleted.
  • If he chooses Yes, the import route is called again with a parameter forcing the re-writing of the docs locale with req.locale

What are the specific steps to test this change?

On testbed or any a3 project, change the modules/@apostrophecms/i18n/index.js config to either have one or multiple locales.

Export some docs in a locale and re-import them on another locale.

  • If the site has only one locale configured in i18n, then the docs should be imported in the current site locale.
  • If the site has multiple locales configured in i18n, then a confirm modal should be displayed. The rest of the test is pretty straightforward, if we choose Yes or No.

What kind of change does this PR introduce?

(Check at least one)

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Build-related changes
  • Other

Make sure the PR fulfills these requirements:

  • It includes a) the existing issue ID being resolved, b) a convincing reason for adding this feature, or c) a clear description of the bug it resolves
  • The changelog is updated
  • Related documentation has been updated
  • Related tests have been updated

Copy link

linear bot commented Nov 20, 2023

PRO-5128 As a user I can import content that was exported from a locale with a different name into the current locale, or decide to cancel

Per discussion with stuart in PRO-5123 this is our interim solution, ready to build and unlock productivity for Michelin's team:

If the locale of the first document in the file does not match the current locale in the UI, and the site we're importing into has more than one locale, then we will display a simple AposModalConfirm confirmation prompt:

"This file was exported from the en-gb locale. Are you sure you want to import it into the en locale?"

If the site has only one locale we will skip this prompt and assume the answer was "Yes."

Note the use of locale names rather than locale labels because we have no idea what the labels were in the exporting site and that's not what matters here anyway, it's the locale name that has to be harmonized.

If the user says no the import is cancelled, if they say yes then the aposLocale and _id properties are remapped on the fly to the current locale so that the import can succeed. Note that relationship fields already use aposDocId which is locale independent and therefore should not be an issue.

Ideally we scan the file until we encounter a document with an aposLocale property (or run out of documents) before displaying this prompt, so clicking "cancel" can do absolutely nothing, but if this is a major implementation issue we could display it the first time we hit a document that does have an aposLocale property and cancel just from that point forward (in other words, somehow backing out imports of localized: false documents reached before that point is not something I want to put on anyone's plate right now, so please do whatever minimizes the scope).

For now we may assume there will only be one locale in the file because we will bump the major version of the export format before we add support for exporting multiple locales. Therefore we only have to check the first document that has an aposLocale property.

Acceptance Criteria

See above novella.

Cypress tests will have to be included in this.

}
if (notificationId) {
self.apos.notification.dismiss(req, notificationId, 2000).catch(self.apos.util.error);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean job and notification only if existing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see /api/v1/@apostrophecms/import-export/clean-export call in app.js below, no job nor notification is instantiated at this step, but we still want to remove the export path

@@ -194,7 +194,7 @@ export default {
}
});
} catch (error) {
apos.notify(this.$t('aposImportExport:importCleanFailed'), {
apos.notify('aposImportExport:importCleanFailed', {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix interpolation

before after
Screenshot 2023-11-21 at 18 00 02 Screenshot 2023-11-21 at 17 59 38
ell

({ docs, attachmentsInfo } = exportFile);
exportPath = exportFile.exportPath;

await self.cleanFile(exportFile.filePath);
Copy link
Contributor Author

@ETLaurent ETLaurent Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we now clean the archive right after having extracted it so we don't have to think about removing it at several other places

@@ -342,20 +421,20 @@ module.exports = self => {
}
},

async cleanFile(exportPath) {
async cleanFile(path) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor change: cleanFile also cleans the archive path

@ETLaurent ETLaurent changed the title wip warn when locale is different Nov 22, 2023
@ETLaurent ETLaurent changed the title warn when locale is different handle different locale Nov 23, 2023
Comment on lines +455 to +464
const differentDocsLocale = self.getFirstDifferentLocale(req, docs);
const siteHasMultipleLocales = Object.keys(self.apos.i18n.locales).length > 1;

// Re-write locale if `overrideLocale` param is passed-on from the import process
// (i.e if the user chose "Yes")
// or re-write locale automatically on a single-locale site
if (differentDocsLocale && (!siteHasMultipleLocales || overrideLocale)) {
self.rewriteDocsWithCurrentLocale(req, docs);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also need to override the locale here!

@ETLaurent ETLaurent marked this pull request as ready for review November 23, 2023 16:50
@ETLaurent
Copy link
Contributor Author

@BoDonkey adding you as a reviewer since I edited the readme ;)

CHANGELOG.md Outdated
Comment on lines 7 to 9
* Import nows detects if the locale found in the exported docs is different from the current site one.
If the site has only one locale configured, then the docs are automatically re-written with the site locale.
If the site has multiple locales configured, the user is given the possibility to abort the import or re-write the docs with the site locale.
Copy link
Contributor

@BoDonkey BoDonkey Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Import nows detects if the locale found in the exported docs is different from the current site one.
If the site has only one locale configured, then the docs are automatically re-written with the site locale.
If the site has multiple locales configured, the user is given the possibility to abort the import or re-write the docs with the site locale.
* Import now detects if the locale found in the exported docs is different from the current site one. If the site has only one locale configured, then the docs are automatically re-written with the site locale. If the site has multiple locales configured, the user can abort the import or re-write the docs with the site locale. Note that this requires Apostrophe 3.60.0 or higher for proper functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BoDonkey removed the whitespaces to make it single line

README.md Outdated
Comment on lines 145 to 147
Documents will automatically be re-written with the current site locale if only one locale is configured in the `@apostrophecms/i18n` module.

If multiple locales are configured, the user is asked about aborting the import or continuing it:
Copy link
Contributor

@BoDonkey BoDonkey Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Documents will automatically be re-written with the current site locale if only one locale is configured in the `@apostrophecms/i18n` module.
If multiple locales are configured, the user is asked about aborting the import or continuing it:
Exported documents maintain their locale settings. If the locale during import differs from the export locale, and only one locale is configured in the @apostrophecms/i18n module, the documents will be automatically rewritten to align with the new import locale.
If multiple locales are set up, the user will be prompted to choose between canceling the import or proceeding with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much better, thanks

Copy link
Contributor

@BoDonkey BoDonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some documentation changes. Code looks okay to me and works as expected. A "real" engineer might be able to find something that could use improvement. 😃

if (exportPath) {
const filesData = await self.getFilesData(exportPath);

({ docs, attachmentsInfo } = filesData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what means the fact that this method receives an exportPath? Does it mean that the gzip file has already been cleaned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gzip file is now cleaned as soon as it's extracted (cf. comment below)

This method is provided exportPath when the user chooses Yes (continue the import with the current locale), because is archive has already been extracted during the import method first call.
It just early returned to display the confirmation modal.
So at the 2nd call, we simply give the method back the exportPath so it knows where the files were extracted during the 1st call

Copy link
Contributor

@ValJed ValJed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few questions, but LGTM


// Re-write if user decided to continue the import (`overrideLocale` param sent)
// or if the site has only one locale configured
self.rewriteDocsWithCurrentLocale(req, docs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this method is called twice, another time with the confirmation to change locale?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is called only during the 2nd call (when the user chooses Yes), see the early return above

});
}

return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we agree that in this case the export file is cleaned from the import route directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it should be done at line 107, as in a "normal" case where no locale overriding is involved

Copy link
Contributor

@BoDonkey BoDonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the typo, please.

CHANGELOG.md Outdated
@@ -2,6 +2,12 @@

## UNRELEASED

### Adds

* Import nows detects if the locale found in the exported docs is different from the current site one.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Import now..."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BoDonkey done

@ETLaurent
Copy link
Contributor Author

Copy link
Contributor

@BoDonkey BoDonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ETLaurent ETLaurent merged commit 159bf25 into main Nov 28, 2023
9 checks passed
@ETLaurent ETLaurent deleted the pro-5128-different-locale branch November 28, 2023 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants