Skip to content

fix: allow partial I18N instances for Upload - #1906

Merged
sissbruecker merged 3 commits into
masterfrom
fix/allow_partial_i18n_for_upload
Jul 21, 2021
Merged

sissbruecker merged 3 commits into
masterfrom
fix/allow_partial_i18n_for_upload

Conversation

@sissbruecker

@sissbruecker sissbruecker commented Jul 16, 2021 •

Copy link
Copy Markdown
Member

Description

Currently the I18N feature of the Upload component only works when setting a fully specified UploadI18N instance. Just overriding a single label like this:

upload.setI18N(new UploadI18N().setAddFiles(new UploadI18N.AddFiles().setOne("Upload Report...")));

does not work, because it results in null values being set in the web components I18N config, which in turn results in null reference errors.

This change fixes that by:

  • removing all null values from the UploadI18N instance before setting it in the web component
  • merging the config with the existing default config from the web component, so that as a fallback the default translations from the web component are used

Fixes: No related issue

Type of change

  • Bugfix

Checklist

  • I have read the contribution guide: https://vaadin.com/docs-beta/latest/guide/contributing/overview/
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.

Comment on lines +571 to +591
// Assign new I18N object to WC, by deeply merging the existing
// WC I18N, and the values from the new UploadI18N instance,
// into an empty object
getElement().executeJs(
"const dropFiles = Object.assign({}, this.i18n.dropFiles, $0.dropFiles);"
+ "const addFiles = Object.assign({}, this.i18n.addFiles, $0.addFiles);"
+ "const error = Object.assign({}, this.i18n.error, $0.error);"
+ "const uploadingStatus = Object.assign({}, this.i18n.uploading.status, $0.uploading && $0.uploading.status);"
+ "const uploadingRemainingTime = Object.assign({}, this.i18n.uploading.remainingTime, $0.uploading && $0.uploading.remainingTime);"
+ "const uploadingError = Object.assign({}, this.i18n.uploading.error, $0.uploading && $0.uploading.error);"
+ "const uploading = {"
+ " status: uploadingStatus,"
+ " remainingTime: uploadingRemainingTime,"
+ " error: uploadingError," + "};"
+ "const units = $0.units || this.i18n.units;"
+ "this.i18n = Object.assign({}, this.i18n, $0, {"
+ " addFiles: addFiles,"
+ " dropFiles: dropFiles,"
+ " uploading: uploading," + " units: units,"
+ "});",
i18nJson);

@sissbruecker sissbruecker Jul 16, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Couldn't identify an easier / safer way to do a deep merge. Considered:

  • importing a merge function from a library like lodash - that might have too big of an impact for this change alone
  • copying a merge function from Github / SO - might have bugs or not work as expected in this use case (e.g. merging units array). Also would be terrible to put such a complex function inline into the Java code
  • introducing new API to the web component - probably won't happen

Still, I'm open for other ideas on improving this. I tried to make the tests rather extensive to make up for this.

Note that the units array is not merged. If the UploadI18N config specifies one, it will always replace the one from the web component, even if the number of elements don't match. However that is the same behaviour as we have today.

@sosa-vaadin sosa-vaadin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I can only consider using StringBuilder where there's a ton of concatenation. I don't see any major problems with this though.

@vaadin-bot

Copy link
Copy Markdown
Collaborator

SonarQube analysis reported 21 issues

  • MAJOR 8 major
  • MINOR 13 minor

Top 10 issues

  1. MAJOR Upload.java#L73: Either remove or fill this block of code. rule
  2. MAJOR Upload.java#L76: Either remove or fill this block of code. rule
  3. MAJOR Upload.java#L403: Remove this unused private "fireUploadInterrupted" method. rule
  4. MAJOR Upload.java#L625: Remove this unused private "getStringObject" method. rule
  5. MAJOR Upload.java#L636: Remove this unused private "getStringObject" method. rule
  6. MAJOR FileBufferView.java#L76: Define and throw a dedicated exception instead of using a generic one. rule
  7. MAJOR UploadTestsI18N.java#L25: Add a private constructor to hide the implicit public one. rule
  8. MAJOR UploadView.java#L56: Either log or rethrow this exception. rule
  9. MINOR Upload.java#L362: Move this method into "DefaultStreamVariable". rule
  10. MINOR Upload.java#L384: Move this method into "DefaultStreamVariable". rule

@sissbruecker
sissbruecker merged commit 42533a5 into master Jul 21, 2021
@sissbruecker
sissbruecker deleted the fix/allow_partial_i18n_for_upload branch July 21, 2021 13:11
vaadin-bot pushed a commit that referenced this pull request Jul 21, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues
sissbruecker added a commit that referenced this pull request Jul 22, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
ZheSun88 pushed a commit that referenced this pull request Jul 27, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues
ZheSun88 pushed a commit that referenced this pull request Jul 27, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues
sissbruecker added a commit that referenced this pull request Jul 27, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
sissbruecker added a commit that referenced this pull request Jul 27, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
ZheSun88 pushed a commit that referenced this pull request Jul 30, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues
ZheSun88 pushed a commit that referenced this pull request Jul 30, 2021
* fix: allow partial I18N instances for Upload

* fix code style issues

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with platform 22.0.0.alpha1 and is also targeting the upcoming stable 22.0.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants