Skip to content

Commit

Permalink
Admin Page: add custom numberFormat implementation
Browse files Browse the repository at this point in the history
This would allow us to get rid of the i18n-calypso package; the only reason we keep this package around today is for its #16397 (comment)

This implementation relies on the browser to do all the work, based on the locale given by the browser or by WordPress.
  • Loading branch information
jeherve committed Oct 2, 2020
1 parent 85aedcb commit 0212fb4
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions _inc/client/components/number-format/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { __experimentalGetSettings } from '@wordpress/date';

/**
* Site locale, or browser locale as fallback.
*
* @returns {string} Formatted Site locale (e.g. `en-US` or `fr-FR`).
*/
export const getLocale = () => {
const {
l10n: { locale },
} = __experimentalGetSettings();

if ( locale ) {
// WP uses underscores, but browsers use hyphens.
return locale.replace( '_', '-' );
}

// Fallback to the browser locale if necessary.
const language = global?.window?.navigator?.language ?? 'en-US';

return language;
};

/**
* Format a number using the locale in use on the site.
*
* @param {number} number - The number to format.
*
* @returns {string} Formatted number.
*/
export const numberFormat = number => {
const locale = getLocale();

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

0 comments on commit 0212fb4

Please sign in to comment.