-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
feat: allow tokens in localize, localize progress bar time #4060
Conversation
src/js/component.js
Outdated
return primaryLang[string]; | ||
if (tokens) { | ||
localizedString = localizedString.replace(/\{(\d+)\}/g, function(match, index) { | ||
return tokens[index - 1]; |
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.
Do we need to check the validity of index
here (is a number, is in-range for tokens[]
, etc.)?
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.
Yeah, we probably should. Should we just return the match
if the index isn't available in tokens[]
?
So, if you do this.localize('{1} {2} {3}', ['a', 'b'])
you would get a b {3}
.
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.
Seems like a reasonable default behavior. Of course, we're also going to need to clearly document what {1}, {2}
etc. are for each string.
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.
Just looking at this, I realize that the visual display next to the progress bar is actually the remaining time, not the duration of the video. Maybe {3}
should be remaining time, so that the text can be customized that way, too?
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.
Of course, we're also going to need to clearly document what {1}, {2} etc. are for each string
I've been planning to write a glossary explaining every string in en.json. The translations have grown a lot recently and there's a lot in there that's not all that clear out of context.
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.
Another thing I can think of to help with this is having the translation string and then default be two different things. So, for this case I can do something like this.localize('progress bar timing', [currentTime, duration], '{1} {2}')
and the en.json would have progress bar timing: '{1} {2}'
. That way we have a default, that's described, but people who only look at the json would have a better idea about what's happening.
Not quite sure if there's the best approach but a thought.
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.
To elaborate and update (after some discussion with @OwenEdwards).
The key should be progress bar timing: currentTime={1}, duration={2}
and the value would be {1} of {2}
. That way, as you're reading the JSON files, you have some idea of what the items are.
Then, we would want to add a 3rd argument to localize
, a defaults argument, which would be {1} of {2}
which is what should be used in the case a language file isn't included (and so we don't have to require en.json to be included).
Tested with IE11 + JAWS, and macOS 10.12.3 Safari + VoiceOver; other than the notes I've already made, LGTM |
@@ -220,28 +220,35 @@ class Component { | |||
* @return {string} | |||
* The localized string or if no localization exists the english string. | |||
*/ | |||
localize(string) { | |||
localize(string, tokens, defaultValue = string) { |
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.
Don't forget to update the JSDoc block for this function.
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, will do.
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.
Updated.
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.
do we actually need "defaultValue" as an argument here? Can't we just make a copy of string in the function
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.
yes, we need defaultValue
as an argument. The = string
just makes the default value of defaultValue
to be string. But if a defaultValue
is passed in to this function, it is used instead of the value of `string.
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 tried explaining it in the jsdoc, can you re-read it and let me know?
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.
Ah, yes, makes sense to me. 👍
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.
Why wouldn't we just make "{1} of {2}" the string we are localizing here? Seems weird to make the translation key and the value have different wording.
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 that the key in the lang file describes what it should be. If you see {1} of {2}
how would you know what is {1}
and {2}
and also what should you be translating?
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.
hmmm ok LGTM
e813c00
to
6022b0b
Compare
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.
1 comment LGTM otherwise
@@ -220,28 +220,35 @@ class Component { | |||
* @return {string} | |||
* The localized string or if no localization exists the english string. | |||
*/ | |||
localize(string) { | |||
localize(string, tokens, defaultValue = string) { |
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.
do we actually need "defaultValue" as an argument here? Can't we just make a copy of string in the function
6022b0b
to
753cfa6
Compare
Fix #4024.