Skip to content

Fix Adsense dash replace error and enhance formatted number i18n#1261

Merged
felixarntz merged 9 commits intodevelopfrom
fix/1092-adsense-dash-replace-error
Mar 18, 2020
Merged

Fix Adsense dash replace error and enhance formatted number i18n#1261
felixarntz merged 9 commits intodevelopfrom
fix/1092-adsense-dash-replace-error

Conversation

@aaemnnosttv
Copy link
Copy Markdown
Collaborator

Summary

Addresses issue #1092, #293, #1107

Relevant technical choices

  • Added a new getLocale() utility function
    Gets the user's chosen locale as defined in Site Kit's googlesitekit.locale if available, falls back to the language set in the browser
  • Updated the existing numberFormat utility to accept an object of formatting options, and use the new locale helper for using the correct locale by default
  • Updated readableLargeNumber
    • Sanitizes input number to ensure it is always working with a valid numeric type
    • Leverage numberFormat and available options as much as possible rather than manual parsing
    • Made numeric abbreviations translatable for better i18n
  • Updated DataBlock components to use numberFormat for consistency when displaying change (should ideally take the change unit from props for formatting but this is still an improvement)

E.g. using de-DE

image
image
image

using en-US

image

Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 4.7 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

tofumatt
tofumatt previously approved these changes Mar 18, 2020
Copy link
Copy Markdown
Collaborator

@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 like it, and I super-dig how many issues this fixes! 👍

The VRTs are failing, but that makes sense as this would have modified how things look. Once those are updated I'm happy with this approach, so approved assuming Travis passes 😄

@aaemnnosttv
Copy link
Copy Markdown
Collaborator Author

Found the problem and fixed it. I did need to approve visual changes to one test where xxxx% changed to x,xxx% now that it's properly formatted.
image

@aaemnnosttv aaemnnosttv marked this pull request as ready for review March 18, 2020 14:18
@aaemnnosttv aaemnnosttv requested a review from tofumatt March 18, 2020 14:18
// This line to make sure we use lower case local format, ex: en-us.
locale = locale.replace( '_', '-' ).toLocaleLowerCase();
if ( siteKitLocale ) {
return siteKitLocale.replace( '_', '-' );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Previously this was calling toLocaleLowerCase, but now it doesn't. Just wanna make sure this is working as expected. Do navigator.language and googlesitekit.locale have the same format?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I'm not sure why it was doing that. navigator.language is not fully lowercase, e.g. en-US although apparently some browsers like older iOS Safari return it in all lowercase. The docs show it used with both cases though as it comes from the property https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/NumberFormat

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The only difference is that WP uses the underscore-separated locale and the browser uses a hyphen.

if ( ! locale ) {
locale = navigator.language;
}
export const numberFormat = ( number, options = {}, locale = getLocale() ) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder whether locale should instead be options.locale - the current signature feels a bit strange to me, especially since the locale is another optional parameter that has a sane default.

Copy link
Copy Markdown
Collaborator Author

@aaemnnosttv aaemnnosttv Mar 18, 2020

Choose a reason for hiding this comment

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

It's mostly for testing I think. I don't think you'd ever want/need to pass the locale directly, but I don't see why it shouldn't be supported. I agree the signature is a bit odd as-is.

We could implement it like this, which doesn't look bad

export const numberFormat = ( number, options = {} ) => {
	const { locale = getLocale(), ...formatOptions } = options;

	return new Intl.NumberFormat( locale, formatOptions ).format( number );
};

Edit: pushed! Take a look 😄

Copy link
Copy Markdown
Contributor

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

LGTM!

Happy to see you put de-DE as example, makes me feel at home 😆

@felixarntz felixarntz merged commit c2ec75e into develop Mar 18, 2020
@felixarntz felixarntz deleted the fix/1092-adsense-dash-replace-error branch March 18, 2020 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants