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

i18n: Create new function addLocaleData to merge domain configuration #37704

Merged
merged 9 commits into from
Jan 17, 2022

Conversation

arthur791004
Copy link
Contributor

@arthur791004 arthur791004 commented Jan 4, 2022

Description

As #34101 and #37686 mentioned, calling setLocaleData multiple times, the domain configuration will be overwritten by the latest one. It might be better to merge the domain configuration rather than overwrite. However, we should avoid any breaking change. So, create a new function addLocaleData to merge the domain configuration and keep the behavior of setLocaleData the same.

How has this been tested?

  1. Go to http://localhost:8888/wp-admin/post.php?post=1&action=edit
  2. Open Developer Tool
  3. Do the following commands
    wp.i18n.addLocaleData({ "": { lang: 'en' } })
    wp.i18n.addLocaleData({ "": {} })
    wp.i18n.getLocaleData()[""]
  4. Output
    { lang: "en", plural_forms: f } 

Types of changes

New feature

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@sirreal
Copy link
Member

sirreal commented Jan 13, 2022

👋 Thanks for pinging me, @swissspidy.

I haven't been working with this package lately. I'm not in a good position to make decisions about introducing new APIs.

Once we're confident we'd like to introduce a new API, I'm happy to help with code review.

Copy link
Member

@jsnajdr jsnajdr left a comment

Choose a reason for hiding this comment

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

This is a great addition, thank you! The new API makes sense.

There's a pre-existing little bug in both setLocaleData and addLocaleData: as they also support updating the locale "metadata" under the '' key, they can also update the [ '' ][ 'plural-forms' ] key. But the tannin library internally caches that. Both functions should therefore do:

tannin.pluralForms[ domain ] = null;

to force updating the cache. That would be nice to fix, either here or in a follow-up PR.

*/
const doAddLocaleData = ( data, domain = 'default' ) => {
tannin.data[ domain ] = {
...DEFAULT_LOCALE_DATA,
Copy link
Member

Choose a reason for hiding this comment

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

The ...DEFAULT_LOCALE_DATA spread is redundant, because it has only the '' key and that's gurananteed to be completely overwritten by the '' key few lines later. The doSetLocaleData function has the same redundancy.

'': {
...DEFAULT_LOCALE_DATA[ '' ],
...( tannin.data[ domain ] || {} )[ '' ],
...( data || {} )[ '' ],
Copy link
Member

Choose a reason for hiding this comment

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

You can use optional chaining for this: ...tannin.data[ domain ]?.[ '' ]

/** @type {SetLocaleData} */
const setLocaleData = ( data, domain ) => {
doSetLocaleData( data, domain );
notifyListeners();
};

/** @type {AddLocaleData} */
const addLocaleData = ( data, domain ) => {
doAddLocaleData( data, domain );
Copy link
Member

Choose a reason for hiding this comment

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

The doAddLocaleData function could be inlined here. There is a standalone doSetLocaleData function because we sometimes want to do the operation without firing notifications, but addLocaleData doesn't have that requirement.

Copy link
Member

Choose a reason for hiding this comment

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

Now you can remove doAddLocaleData completely, and do the tannin assignment directly in the addLocaleData body.

@arthur791004
Copy link
Contributor Author

@jsnajdr Thanks for your review! I've fixed pluralForms cache issue and made some changes according to your feedback 👍

As the value of tannin.pluralForms[ domain ] should be function, I deleted the value to force updating the cache.

Copy link
Member

@jsnajdr jsnajdr left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the feedback, the PR looks good 👍 Do you need any help with merging or do you have permission to do that yourself.

There are e2e failures in editor/blocks/navigation.test.js -- are they possibly related? The test fails to find an element by it text content, where the text is produced by __(), so __() malfunction is a possible reason for failure.

/** @type {SetLocaleData} */
const setLocaleData = ( data, domain ) => {
doSetLocaleData( data, domain );
notifyListeners();
};

/** @type {AddLocaleData} */
const addLocaleData = ( data, domain ) => {
doAddLocaleData( data, domain );
Copy link
Member

Choose a reason for hiding this comment

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

Now you can remove doAddLocaleData completely, and do the tannin assignment directly in the addLocaleData body.

@arthur791004
Copy link
Contributor Author

arthur791004 commented Jan 14, 2022

Do you need any help with merging or do you have permission to do that yourself.

@jsnajdr Oh yes, I need help to merge this one as I don't have permission. Thanks!

There are e2e failures in editor/blocks/navigation.test.js -- are they possibly related?

I don't think the e2e failures are related to this one as everything keeps the same except removing redundant
spread and fixing the cache issue. But I think both of them won't affect the e2e testing 🤔

@swissspidy
Copy link
Member

A changelog entry for this new function would be great

@arthur791004
Copy link
Contributor Author

arthur791004 commented Jan 17, 2022

A changelog entry for this new function would be great

@swissspidy I've updated the changelog, thanks 👍

@@ -2,6 +2,8 @@

## Unreleased

- Add new `addLocaleData` method to merges locale data into the Tannin instance by domain. Note that this function will also merges the domain configuration.

Copy link
Member

Choose a reason for hiding this comment

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

Let's fix the typo in "merges" which should read as "merge". And I would personally remove the "Note" because a changelog entry doesn't need this level of detail about the new method.

The PR will be finally ready to land after this last fixup is done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops...I'm sorry for the typo 😅.

And I would personally remove the "Note" because a changelog entry doesn't need this level of detail about the new method.

Okay 👍

@swissspidy swissspidy merged commit ae55b94 into WordPress:trunk Jan 17, 2022
@github-actions github-actions bot added this to the Gutenberg 12.5 milestone Jan 17, 2022
@arthur791004 arthur791004 deleted the feat/add-locale-data branch January 27, 2022 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] i18n /packages/i18n
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants