Skip to content
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

Localise user count in search results #1627

Merged
merged 2 commits into from
Jan 18, 2017

Conversation

mstriemer
Copy link
Contributor

@mstriemer mstriemer commented Jan 16, 2017

I updated how the lang is acquired. Rather than using state it uses i18n.lang and the lang is passed to the makeI18n() function to be set rather than pulling it from the first message (I'm curious what that would do if the string wasn't translated or we maybe didn't have any translations for a locale).

I updated the string to match the one from AddonMeta since they have different names for the number so it would have caused there to be an extra translation string (I think).

Screenshots:

Englishscreen shot 2017-01-16 at 16 31 59
French

screen shot 2017-01-16 at 16 45 00

 

Fixes mozilla/addons#10040.

Copy link
Contributor

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love i18n.lang exposing the current locale 👍

Looks good to me, just that bit about the magic constant irked me so r+wc

@@ -71,7 +71,7 @@ export function unexpectedSuccess() {
/*
* Creates a stand-in for a jed instance,
*/
export function getFakeI18nInst() {
export function getFakeI18nInst({ lang = 'en-US' } = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is only a test, but do we declare en-US as the default locale anywhere else? Would be nice to use a constant rather than the magic one here.

const localisedRoot = renderResult(result, { lang: 'fr' });
const users = findRenderedDOMComponentWithClass(localisedRoot, 'SearchResult-users');
// \xa0 is a non-breaking space.
assert.match(users.textContent, /5\xa0253/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L'espace est bizarre ici pour moi.

How many results?
"5 253 !"

English must feel so cramped in comparison. 😉


// This adds the correct moment locale for the active locale so we can get
// localised dates, times, etc.
if (i18n.options && typeof i18n.options._momentDefineLocale === 'function') {
i18n.options._momentDefineLocale();
moment.locale(makeMomentLocale(i18n.options.locale_data.messages[''].lang));
moment.locale(makeMomentLocale(i18n.lang));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is quite a bit nicer, cheerio 👍

@tofumatt
Copy link
Contributor

Actually, only thing is: do we want to keep all our i18n stuff as methods off i18n? Maybe i18n.numberFormat(number) or even i18n.toLocaleString(number) would be nicer and more consistent with how we translate everything else?

Just a thought.

@mstriemer
Copy link
Contributor Author

I like the idea of i18n.toLocaleString(), I'll add it.

@kumar303
Copy link
Contributor

I'd like to preserve the ability to dispatch a Redux action and change lang everywhere -- that's what using mapStateToProps in AddonMeta did. This approach won't preserve it, as far as I can tell.

I know that our current language switcher doesn't work this way since it does a redirect but that's a hack IMO and we should fix it. Redux is made for state changes like this.

@kumar303
Copy link
Contributor

Maybe if I18nProvider was the only one to use mapStateToProps (which would reset i18n.lang) then it would propagate all the way through?

@@ -187,6 +187,8 @@ export function makeI18n(i18nData, lang, _Jed = Jed) {
const i18n = new _Jed(i18nData);
i18n.lang = lang;

i18n.formatNumber = (number) => number.toLocaleString(lang);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay

@mstriemer
Copy link
Contributor Author

We'd need to load the new i18n data and likely create a new Jed instance in I18nProvider. I don't think any of these changes prevent this.

The react docs [1] suggest that you do not update things that are in context because a parent component can return false in shouldComponentUpdate() which will prevent the children being checked, but they may depend on context.

They link to an article with potential solutions but I haven't read through it. Sounds like there is going to be some tricky bits getting the app to re-render after a lang change since we have that in context. Either way I don't think these changes prevent, or make more difficult, updating the app when the lang changes.

[1] https://facebook.github.io/react/docs/context.html#updating-context

@mstriemer mstriemer merged commit bb4981e into mozilla:master Jan 18, 2017
@mstriemer mstriemer deleted the localise-user-count-search-1608 branch January 18, 2017 18:57
@kumar303
Copy link
Contributor

Yes, the blog post you linked to says not to do exactly what you did :) None of this is a blocker for release but I think this patch would have been just as easy to write using Redux state. I'd like to fix it in the future because Redux will make the app a lot easier to work on. Filed: mozilla/addons#10052 (low priority)

@kumar303
Copy link
Contributor

To be clear: I know there are other challenges preventing us from ditching the locale redirect (like, how do we load the new translations?) but the redirect was already hooked up to Redux state so we might as well use it to prepare for the future.

@mstriemer
Copy link
Contributor Author

I didn't modify the context. I changed a value that it had when it was created, that is totally fine.

If we want to make lang switching without a full reload possible then we will need to modify the i18n object that is returned in the context because it has all of the strings that are being translated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Localise user count in search results
4 participants