-
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
Interpolate Component Experiment #1 #16374
Conversation
*/ | ||
const Interpolate = ( props ) => { | ||
return createInterpolateElement( | ||
createInterpolateString( props.children, props ), |
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.
As noted in the pull description, right now this isn't localizing anything, but it does demonstrate how a localizable string can be built using this new helper method. I think if we decide to expose the nicer api via a component, we should use babel to transform the props.children
to the interpolation string and output it in the built code. I haven't written a babel plugin yet so that's something I might need help with.
Todo notes after slack chat
|
This is the initial push for the interpolate experiment. Exports are: - Interpolate custom component. - createInterpolateElement (similar to `createElement`).
e7c7897
to
cf1b969
Compare
Huh, tests started failing because |
Okay so I think I know why tests fail.
Not certain what should be done here. It seems to me we should be targeting builds that will be similar to the browser builds (with appropriate polyfills etc) right? cc @gziolo. For now, I'll just switch to lodash |
now that #17376 has been merged, this can be closed. Future iterations can incorporate some of the ideas around a |
Description
This is the initial experiment prompted by #9846.
In progress towards solving the problem posed in #9846, this pull was created with the following goals in mind:
In this pull:
Introduction of
createInterpolateElement
.This function works similarly to
createElement
in exposing an api for creating a WordPress (React) element.It accepts two arguments:
Example:
The function returns this as the equivalent:
This allows for usage immediately wherever interpolation is needed for translated strings.
Some notes:
Tags can be virtually anything, but for the purpose of localization it's recommended to use something that provides context for translators.
The conversion map is expected to be an array of configuration elements ordered by how the tags appear in the interpolation string. Each element in the array is expected to have two values:
tag
,props
, andhasChildren
keys, or a "prop value" type object withvalue
as the key. The former is similar to the arguments forwp.element.createElement
except rather then the third being an array of children, it is simply a boolean indicating whether the given tag represents an element with children. This is because children are extracted from the interpolation string rather than being predefined and may be nested elements. The latter simply signals that the given search string is a straight replacement with the given value.This api is a bit verbose and could be prone to human error, however it solves the majority of the issues with the current problem out of the box and allows for immediate use.
Interpolate
componentThis component is currently a demonstration of how an easier api could be exposed to developers for writing interpolation type components.
Usage:
Here's an example of how it could be used (using the same string as our example):
This will result in the same element as described in the previous example. Note the differences:
%%value%%
syntax and thevalue
should correspond to the name of the prop defined on theInterpolate
tag. The format of this syntax is negotiable but for the sake of this pull I used something that should be fairly straightforward.Internally,
Interpolate
callscreateInterpolateElement
and uses the new function,createInterpolateString
to generate the interpolation string from the element. The created string for this example is the same as what was fed intocreateInterpolateElement
in the previous example. Currently in the code this is not localized but this gives an example of how it could be so thatInterpolate
could be a suitable friendlier api for this use-case.Next steps
I've mostly submitted this pull to demonstrate what I think will be a good path to go forward with in solving #9846. Here's some next steps to keep momentum:
createInterpolateElement
seems to best fit as an export on@wordpress/element
however it's arguable whetherInterpolate
should live here also or be exported from@wordpress/i18n
. Typical usage will likely be for i18n so it'd probably be better to putInterpolate
there.<Translate>
or something like that. Feasibly in the future it could serve as the mechanism (via context hooks) for live language switching.Translate.Singular
andTranslate.Plural
) and should be fairly trivial to detect and implement. For actual localization, I think the best method would be to write a babel plugin that translates<Translate>{ children }</Translate>
to usecreateInterpolateElement
with the appropriatewp.i18n
function wrapping the interpolationString argument on the built code. That would allow all existing string extraction methods to "just" work against built code as before.There's probably some other thoughts I've missed here, I'll add them as I remember, but for now I think this is ready for initial feedback, review and direction. It is not in mergeable state yet , however we could decide to extract
createInterpolateElement
into its own pull as it could be released as is for a good interim solution.How has this been tested?
There are unit tests covering the public api and demonstrating expected behaviour.
Types of changes
This is a new feature introduced to solve problems outlined in #9846
Checklist: