mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-02-27 20:15:12 +01:00
fix(mobile): fix broken formatting on iOS (#10893)
This commit is contained in:
27
packages/utils/src/getLocaleSeparators.native.ts
Normal file
27
packages/utils/src/getLocaleSeparators.native.ts
Normal 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 };
|
||||
}
|
||||
38
packages/utils/tests/getLocaleSeparators.native.test.ts
Normal file
38
packages/utils/tests/getLocaleSeparators.native.test.ts
Normal 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
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user