mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-06-28 09:44:49 +02:00
fix: crash if message has syntax error (#128)
* fix crash if message has syntax error * fix custom number format test & lint require error Co-authored-by: Ivan Chalyk <boomfly@boomfly-pro.local>
This commit is contained in:
parent
8ad5250f71
commit
0d623f9884
|
@ -67,7 +67,18 @@ const formatMessage: MessageFormatter = (id, options = {}) => {
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
return getMessageFormatter(message, locale).format(values) as string;
|
let result = message;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = getMessageFormatter(message, locale).format(values) as string;
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(
|
||||||
|
`[svelte-i18n] Message with "${id}" has syntax error:`,
|
||||||
|
e.message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatTime: TimeFormatter = (t, options) => {
|
const formatTime: TimeFormatter = (t, options) => {
|
||||||
|
|
|
@ -7,6 +7,8 @@ import {
|
||||||
locale,
|
locale,
|
||||||
} from '../../../src/runtime';
|
} from '../../../src/runtime';
|
||||||
|
|
||||||
|
const formatsJson = require('../../fixtures/formats.json');
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
init({ fallbackLocale: undefined });
|
init({ fallbackLocale: undefined });
|
||||||
});
|
});
|
||||||
|
@ -35,7 +37,7 @@ describe('number formatter', () => {
|
||||||
it('formats a number with a custom format', () => {
|
it('formats a number with a custom format', () => {
|
||||||
init({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
formats: require('../../fixtures/formats.json'),
|
formats: formatsJson,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(getNumberFormatter({ format: 'brl' }).format(number)).toBe(
|
expect(getNumberFormatter({ format: 'brl' }).format(number)).toBe(
|
||||||
|
@ -81,7 +83,7 @@ describe('date formatter', () => {
|
||||||
it('throws if passed a non-existing format', () => {
|
it('throws if passed a non-existing format', () => {
|
||||||
init({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
formats: require('../../fixtures/formats.json'),
|
formats: formatsJson,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(() =>
|
expect(() =>
|
||||||
|
@ -92,7 +94,7 @@ describe('date formatter', () => {
|
||||||
it('formats a date with a custom format', () => {
|
it('formats a date with a custom format', () => {
|
||||||
init({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
formats: require('../../fixtures/formats.json'),
|
formats: formatsJson,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(getDateFormatter({ format: 'customDate' }).format(date)).toBe(
|
expect(getDateFormatter({ format: 'customDate' }).format(date)).toBe(
|
||||||
|
@ -138,7 +140,7 @@ describe('time formatter', () => {
|
||||||
it('formats a time with a custom format', () => {
|
it('formats a time with a custom format', () => {
|
||||||
init({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
formats: require('../../fixtures/formats.json'),
|
formats: formatsJson,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(getTimeFormatter({ format: 'customTime' }).format(time)).toBe(
|
expect(getTimeFormatter({ format: 'customTime' }).format(time)).toBe(
|
||||||
|
@ -149,7 +151,7 @@ describe('time formatter', () => {
|
||||||
it('throws if passed a non-existing format', () => {
|
it('throws if passed a non-existing format', () => {
|
||||||
init({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
formats: require('../../fixtures/formats.json'),
|
formats: formatsJson,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(() =>
|
expect(() =>
|
||||||
|
@ -189,6 +191,7 @@ describe('message formatter', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('formats number with custom formats', () => {
|
it('formats number with custom formats', () => {
|
||||||
|
locale.set('en');
|
||||||
expect(
|
expect(
|
||||||
getMessageFormatter('Number: {n, number, compactShort}', 'en').format({
|
getMessageFormatter('Number: {n, number, compactShort}', 'en').format({
|
||||||
n: 2000000,
|
n: 2000000,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user