-
Notifications
You must be signed in to change notification settings - Fork 28
i18n: Support accumulatively registering additional locale data for domain #105
Changes from 6 commits
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,6 +3,7 @@ | |
*/ | ||
import Jed from 'jed'; | ||
import memoize from 'memize'; | ||
import { merge } from 'lodash'; | ||
|
||
let i18n; | ||
|
||
|
@@ -34,7 +35,9 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { | |
} ); | ||
} | ||
|
||
i18n.options.locale_data[ domain ] = localeData; | ||
i18n.options.locale_data[ domain ] = i18n.options.locale_data.hasOwnProperty(domain) | ||
? 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 ⚡️ |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,10 @@ const localeData = { | |
|
||
"fruit\u0004%d apple" : [ "une pomme", "%d pommes" ], | ||
} | ||
const additionalLocaleData = { | ||
"cheeseburger" : ["hamburger au fromage"] | ||
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. |
||
} | ||
|
||
setLocaleData( localeData, 'test_domain' ); | ||
|
||
describe( 'i18n', () => { | ||
|
@@ -84,4 +88,21 @@ describe( 'i18n', () => { | |
expect( result ).toBe( 'bonjour Riad' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'setAdditionalLocale' , () => { | ||
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. Code style: No space before comma (reference) |
||
beforeAll( () => { | ||
setLocaleData( additionalLocaleData, 'test_domain' ); | ||
} ); | ||
describe( '__', () => { | ||
it( 'existing translation still available', () => { | ||
expect( __( 'hello', 'test_domain' ) ).toBe( 'bonjour' ); | ||
} ); | ||
} ); | ||
|
||
describe( '__', () => { | ||
it( 'new translation available.', () => { | ||
expect( __( 'cheeseburger', 'test_domain' ) ).toBe( 'hamburger au fromage' ); | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
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.
Code style†: Space between parentheses (reference
† This repository is not (yet) enforced by ESLint standards.