Skip to content

Commit

Permalink
Provide a thin wrapper around toLocaleString()
Browse files Browse the repository at this point in the history
While this still doesn't offer a consistent and predictable experience due to
browser inconsistencies when it comes to `toLocaleString()` implementation, it
is the least bad option we can easily offer for the time being.

Refs. #59.
  • Loading branch information
julen committed Nov 9, 2016
1 parent df909c0 commit 04aa39f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pootle/static/js/browser/components/ColoredNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import React from 'react';

import { toLocaleString } from 'utils/i18n';


const ColoredNumber = React.createClass({
propTypes: {
Expand All @@ -19,7 +21,7 @@ const ColoredNumber = React.createClass({
return <span className="zero">0</span>;
}

return <span>{this.props.n.toLocaleString()}</span>;
return <span>{toLocaleString(this.props.n)}</span>;
},
});

Expand Down
4 changes: 3 additions & 1 deletion pootle/static/js/browser/components/NumberPill.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import React from 'react';

import { toLocaleString } from 'utils/i18n';


const NumberPill = React.createClass({
propTypes: {
Expand All @@ -25,7 +27,7 @@ const NumberPill = React.createClass({
className="stats-data"
href={this.props.url}
>
{this.props.n.toLocaleString()}
{toLocaleString(this.props.n)}
</a>
);
},
Expand Down
17 changes: 17 additions & 0 deletions pootle/static/js/shared/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,20 @@ export function nt(singular, plural, count, ctx = null) {
}
return interpolate(ngettext(singular, plural, count), ctx, true);
}


/**
* Locale-specific to-string conversion.
*
* This is a thin wrapper around `.toLocaleString()` to always use
* `navigator.language`, which is the least bad option we have to determine
* user's preferred language for the time being.
*
* Please note browsers implement `toLocaleString()` very differently and when
* it comes to number decimals, it doesn't work at all in Safari 9 and IE11,
* Chrome always omits the parameter passed to `toLocaleString()` and Firefox is
* the only one obeying it.
*/
export function toLocaleString(number) {
return number.toLocaleString(navigator.language);
}
3 changes: 2 additions & 1 deletion pootle/static/js/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import LastActivity from 'components/LastActivity';
import TimeSince from 'components/TimeSince';
import cookie from 'utils/cookie';
import { q } from 'utils/dom';
import { toLocaleString } from 'utils/i18n';

import BrowserTable from './browser/components/BrowserTable';
import Stats from './browser/components/Stats';
Expand All @@ -27,7 +28,7 @@ import msg from './msg';


function formattedValue(n) {
return n ? n.toLocaleString() : 0;
return n ? toLocaleString(n) : 0;
}

function nicePercentage(part, total, noTotalDefault) {
Expand Down

0 comments on commit 04aa39f

Please sign in to comment.