-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
i18n: Create new function addLocaleData
to merge domain configuration
#37704
Conversation
d7ac53c
to
b918f09
Compare
👋 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. |
There was a problem hiding this 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.
packages/i18n/src/create-i18n.js
Outdated
*/ | ||
const doAddLocaleData = ( data, domain = 'default' ) => { | ||
tannin.data[ domain ] = { | ||
...DEFAULT_LOCALE_DATA, |
There was a problem hiding this comment.
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.
packages/i18n/src/create-i18n.js
Outdated
'': { | ||
...DEFAULT_LOCALE_DATA[ '' ], | ||
...( tannin.data[ domain ] || {} )[ '' ], | ||
...( data || {} )[ '' ], |
There was a problem hiding this comment.
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 ]?.[ '' ]
packages/i18n/src/create-i18n.js
Outdated
/** @type {SetLocaleData} */ | ||
const setLocaleData = ( data, domain ) => { | ||
doSetLocaleData( data, domain ); | ||
notifyListeners(); | ||
}; | ||
|
||
/** @type {AddLocaleData} */ | ||
const addLocaleData = ( data, domain ) => { | ||
doAddLocaleData( data, domain ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@jsnajdr Thanks for your review! I've fixed As the value of |
There was a problem hiding this 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.
packages/i18n/src/create-i18n.js
Outdated
/** @type {SetLocaleData} */ | ||
const setLocaleData = ( data, domain ) => { | ||
doSetLocaleData( data, domain ); | ||
notifyListeners(); | ||
}; | ||
|
||
/** @type {AddLocaleData} */ | ||
const addLocaleData = ( data, domain ) => { | ||
doAddLocaleData( data, domain ); |
There was a problem hiding this comment.
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.
@jsnajdr Oh yes, I need help to merge this one as I don't have permission. Thanks!
I don't think the e2e failures are related to this one as everything keeps the same except removing redundant |
A changelog entry for this new function would be great |
@swissspidy I've updated the changelog, thanks 👍 |
14eabc3
to
0c306d7
Compare
@@ -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. | |||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
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 functionaddLocaleData
to merge the domain configuration and keep the behavior ofsetLocaleData
the same.How has this been tested?
Types of changes
New feature
Checklist:
*.native.js
files for terms that need renaming or removal).