-
Notifications
You must be signed in to change notification settings - Fork 88
Initialize ignore translate #91
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
Initialize ignore translate #91
Conversation
Amend the ignoreTranslateChildren logic in Translate to ensure that a false on the component instance overrides a true on the context state.
Codecov Report
@@ Coverage Diff @@
## master #91 +/- ##
==========================================
+ Coverage 92.75% 92.89% +0.13%
==========================================
Files 6 6
Lines 207 211 +4
Branches 59 61 +2
==========================================
+ Hits 192 196 +4
Misses 13 13
Partials 2 2
Continue to review full report at Codecov.
|
@thchia I have pushed some changes the simplifies some of that logic that you mentioned in both If all is well we can merge. |
src/Translate.js
Outdated
@@ -50,28 +50,20 @@ class WrappedTranslate extends React.Component<TranslateWithContextProps> { | |||
const fallbackRenderToStaticMarkup = value => value; | |||
const renderToStaticMarkup = | |||
context.renderToStaticMarkup || fallbackRenderToStaticMarkup; | |||
const hasId = id !== undefined; | |||
const hasDefaultLanguage = defaultLanguage !== undefined; | |||
const hasChildren = children === undefined; |
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 seems confusingly named, since it is checking that children is undefined
. It also means the if condition below reads as if (hasChildren...) return
which isn't the case?
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.
Yep that’s my bad 🤗. Will look at renaming some of these.
src/LocalizeContext.js
Outdated
} | ||
const ignoreTranslateChildren = | ||
options.ignoreTranslateChildren || | ||
defaultTranslateOptions.ignoreTranslateChildren; |
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.
I may be overthinking this one, but if one day it is decided that the default ignoreTranslateChildren
is true
, it will override a potential false
being sent in by the user. Since the default is determined by the library itself probably the best thing is to add a test to help current/future maintainers.
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.
Actually it looks like this will be tricky to test due to the Enzyme problem with Context... It seems like it's not possible to trigger the whole flow of:
- Instantiating the Provider (with default
ignoreTranslateChildren: true
) - Calling
initialize()
withignoreTranslateChildren: false
- Instantiating
Translate
and checking that the child was rendered.
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.
Yea I think this can be updated to evaluate similar to how I handled ignoreTranslateChildren in the Translate component, which should handle this scenario.
@thchia I've pushed up changes that address the two comments you made. I'm going to go ahead and merge this one so we can get it published. |
For #89.
Summary
This change allows
ignoreTranslateChildren
to be passed in theinitialize()
call. It allows global setting of this flag, which determines whether or notTranslate
children should be added into the default language.Review
In
LocalizeContext
I ran into issues with flow. BecauseignoreTranslateChildren
is optional in theinitialize()
options, but required in the context object, flow was complaining that a boolean or undefined cannot be assigned to a boolean. It did this even after my attempts to narrow the type, so the resulting logic is somewhat verbose. Any suggestions welcome (I'm not familiar with flow).I left a note in
Translate
regarding the logic that determines whether to proceed withaddDefaultTranslation
because the code might look unnecessarily complex. What I naively did at first (in pseudocode):This will not work because if
<Translate options={{ ignoreTranslateChildren: false }} />
, it will be overriden by a potentialcontext.ignoreTranslateChildren === true
that was set frominitialize()
. In fact, whatever is set onTranslate
should have the final say. Sinceundefined
andfalse
are falsy, there is a need to do an explicit check:Again, any suggestions of how to make this more concise are welcome. I have put a test in anyway for good measure so it should be pretty safe.