-
Notifications
You must be signed in to change notification settings - Fork 28
i18n: Support accumulatively registering additional locale data for domain #105
Changes from 1 commit
601b638
b3775ae
1e430cf
0757767
3bb5b9c
9a6d78e
3f56469
25c686f
8dfe686
a39924a
533c64e
1b31bc8
aed293b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
*/ | ||
import Jed from 'jed'; | ||
import memoize from 'memize'; | ||
import { merge, has } from 'lodash'; | ||
import { merge } from 'lodash'; | ||
|
||
let i18n; | ||
|
||
|
@@ -35,7 +35,7 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { | |
} ); | ||
} | ||
|
||
i18n.options.locale_data[ domain ] = has( i18n.options.locale_data, domain ) | ||
i18n.options.locale_data[ domain ] = domain in i18n.options.locale_data | ||
? merge( {}, i18n.options.locale_data[ domain ], localeData ) | ||
: localeData; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems reasonable to me, A second opinion might be good. I wonder if we do need the deep Also, should we add a unit test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got thinking about this, the other concern we might have here is protecting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably good argument for making sure Jed is ALWAYS instantiated with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What consequences would there be, out of curiosity? I guess in my head I'm wondering, if a plugin developer erroneously set locale data for the default domain, with the merging behavior would it just be as though they're adding the existing set? I'd be fine to be explicit with passing the argument, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly I'm a bit confused with the behaviour of
This seems kinda problematic to me, but may just be me not fully grokking the variations there. Walking through what I understand (mostly for my own benefit), it looks like jed.textdomain is only set from So... running through a scenario. If However, if in a subsequent call to localeData[''].domain = 'some_other_domain' ...it looks like 'some_other_domain' is never really referenced anywhere by jed (its just ignored). So long story short... ya looks like we don't have to worry about the index getting trounced when it comes to the value of However there is the possibility of a plugin author messing up the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Interesting. I guess it makes sense, since we're already kinda doing it wrong in Gutenberg: <script type='text/javascript'>
wp.i18n.setLocaleData( {"":{"domain":"gutenberg","lang":"en_US"}} );
</script> It's assigning data for the
I'm not entirely clear what you're referring to by "setLocale directly". In any case, I wouldn't overthink it too much, unless it's a footgun. In any case, the remaining question I have here is if we should do as Riad originally suggested, and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah I agree. But since its ignored we probably don't even need to set
Well the statement I made is on the assumption that something will land in WordPress core that will automate queuing up
Agreed, and I forgot about Riad's comment (thanks for the reminder). Pushed the change ⚡️ |
||
} | ||
|
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.
so @aduth I ended up using
in
because it appears that's the most performant optionThere 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.
Well to be more accurate, its the most performant option in firefox and chrome. In Safari
hasOwnProperty
wins. I'm happy to use whatever you prefer.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.
It's important to understand that
in
has different behavior thanhasOwnProperty
though, where the former traverses up through the prototype chain of the object.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
As implemented here, this is susceptible to errors / oddities when doing something like assigning
valueOf
as a domain, or any other member of the object prototype.This line of code is likely to be called only a handful of times for an entire page session, so optimizing for performance here is a bit overkill.
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.
Related: #85
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.
gotcha, makes sense, changed it.