fix(mobile): fix broken formatting on iOS (#10893)

This commit is contained in:
Daniel Suchý
2024-01-30 10:17:44 +01:00
committed by GitHub
parent 906dcd476e
commit 4e5d028907
2 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
// on iOS we can't use formatToParts, so we need to find the separators differently
// maybe we can use for this for desktop too and don't have two implementations?
export function getLocaleSeparators(locale: string): {
decimalSeparator: string;
thousandsSeparator: string;
} {
// Format a number with both thousands and decimal parts
const formattedNumber = new Intl.NumberFormat(locale).format(1234567.89);
// Find the thousand and decimal separators
let thousandsSeparator = ' ';
let decimalSeparator = '.';
// Since the number is 1,234,567.89 or 1.234.567,89 or similar,
// the last non-numeric character before the last 2 digits is the decimal separator
// and the first non-numeric character is the thousand separator
for (let i = 0; i < formattedNumber.length; i++) {
if (!/\d/.test(formattedNumber[i])) {
thousandsSeparator = formattedNumber[i];
break;
}
}
decimalSeparator = formattedNumber[formattedNumber.length - 3];
return { decimalSeparator, thousandsSeparator };
}

View File

@@ -0,0 +1,38 @@
import { getLocaleSeparators } from '../src/getLocaleSeparators.native';
describe('getLocaleSeparators', () => {
it('should correctly identify separators for en-US', () => {
expect(getLocaleSeparators('en-US')).toEqual({
decimalSeparator: '.',
thousandsSeparator: ',',
});
});
it('should correctly identify separators for de-DE', () => {
expect(getLocaleSeparators('de-DE')).toEqual({
decimalSeparator: ',',
thousandsSeparator: '.',
});
});
it('should correctly identify separators for fr-FR', () => {
expect(getLocaleSeparators('fr-FR')).toEqual({
decimalSeparator: ',',
thousandsSeparator: '\u202F',
}); // note the non-breaking space
});
it('should correctly identify separators for ja-JP', () => {
expect(getLocaleSeparators('ja-JP')).toEqual({
decimalSeparator: '.',
thousandsSeparator: ',',
}); // Japanese uses the same separators as en-US
});
it('should correctly identify separators for cs-CZ', () => {
expect(getLocaleSeparators('cs-CZ')).toEqual({
decimalSeparator: ',',
thousandsSeparator: '\u00A0',
}); // note the non-breaking space
});
});