Skip to content

Commit f689960

Browse files
committed
feat: move default message property
1 parent a19c788 commit f689960

File tree

11 files changed

+42
-86
lines changed

11 files changed

+42
-86
lines changed

packages/core/src/__tests__/on_error.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('Handle missing id', () => {
66
const translator = new Translator();
77
translator.onError = onError;
88

9-
translator.translate('test', {
9+
translator.translate('test', undefined, {
1010
onError,
1111
});
1212

packages/core/src/__tests__/select_other.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ const locales: Locale[] = [
2323
test('should use select other case as fallback', () => {
2424
const { translate } = new Translator('en', locales);
2525

26-
expect(translate('hello', { gender: 'male' })).toBe('hello man');
27-
expect(translate('hello', { gender: 'female' })).toBe('hello woman');
28-
expect(translate('hello', { gender: 'other' })).toBe('hello');
29-
expect(translate('hello', { gender: 'prefer-not-to' })).toBe('hello');
26+
expect(translate('hello', undefined, { gender: 'male' })).toBe('hello man');
27+
expect(translate('hello', undefined, { gender: 'female' })).toBe(
28+
'hello woman',
29+
);
30+
expect(translate('hello', undefined, { gender: 'other' })).toBe('hello');
31+
expect(translate('hello', undefined, { gender: 'prefer-not-to' })).toBe(
32+
'hello',
33+
);
3034
});
3135

3236
test('should use select other case as fallback for Arabic language', () => {
3337
const { translate } = new Translator('ar', locales);
3438

35-
expect(translate('minute', { count: 1 })).toBe('min 1');
36-
expect(translate('minute', { count: 2 })).toBe('min 2');
39+
expect(translate('minute', undefined, { count: 1 })).toBe('min 1');
40+
expect(translate('minute', undefined, { count: 2 })).toBe('min 2');
3741
});
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export interface FormatMessageOptions extends Record<string, any> {
2-
defaultMessage?: string;
3-
}
1+
export type FormatMessageOptions = Record<string, any>;

packages/core/src/parser/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { FormatMessageOptions } from '../model';
12
import { TokenType, Token, TokenStream } from './token_stream';
23

34
export function getTranslationParts(
45
language: string,
56
message: string,
6-
params: Record<string, any>,
7+
params: FormatMessageOptions = {},
78
): any[] {
89
const tokenStream = new TokenStream(message);
910
let result: any[] = [];

packages/core/src/translator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ export class Translator {
2727
return new Intl.NumberFormat(this.language, options).format(value);
2828
};
2929

30-
translate = (id: string, options: FormatMessageOptions = {}): string => {
31-
const message = this.getMessageById(id, options.defaultMessage);
30+
translate = (
31+
id: string,
32+
defaultMessage?: string,
33+
options?: FormatMessageOptions,
34+
): string => {
35+
const message = this.getMessageById(id, defaultMessage);
3236

3337
if (typeof message === 'string') {
3438
try {

packages/preact/src/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,22 @@ export const Numeric: FunctionalComponent<NumericProps> = ({
102102
};
103103

104104
export interface TextProps extends FormatMessageOptions {
105+
defaultMessage?: string;
106+
html?: boolean;
105107
id: string;
106-
107108
tagName?: string;
108-
html?: boolean;
109109
}
110110

111111
export const Text: FunctionalComponent<TextProps> = ({
112112
children,
113+
defaultMessage,
113114
html,
114115
id,
115116
tagName = 'span',
116117
...values
117118
}) => {
118119
const context = useContext(TranslationsContext);
119-
const result = context.translator.translate(id, values);
120+
const result = context.translator.translate(id, defaultMessage, values);
120121

121122
if (html) {
122123
return h(tagName, {

packages/react-native/src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,17 @@ export const Numeric: FC<NumericProps> = ({
8585
};
8686

8787
export interface TranslationProps extends FormatMessageOptions {
88+
defaultMessage?: string;
8889
id: string;
8990
}
9091

9192
export const Translation: FC<TranslationProps> = ({
9293
children,
94+
defaultMessage,
9395
id,
9496
...values
9597
}) => {
96-
return useTranslator().translate(id, values);
98+
return useTranslator().translate(id, defaultMessage, values);
9799
};
98100

99101
export interface TranslationsContextProps {

packages/react/src/__tests__/html.test.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,4 @@ describe('EOLocaleHtml', () => {
4747

4848
expect(strong?.textContent).toEqual('мир');
4949
});
50-
51-
it('Should render formatted message for en', () => {
52-
const { getByTestId } = render(
53-
<TestWrapper>
54-
<Text html id='hello' name={<Numeric value={1000} />} />
55-
</TestWrapper>,
56-
);
57-
58-
expect(getByTestId('translation')?.textContent).toEqual('Hello 1,000!');
59-
});
6050
});

packages/react/src/__tests__/on_error.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ describe('On error handler', () => {
1313
</TestWrapper>,
1414
);
1515

16-
expect(onError).toBeCalledTimes(1);
16+
expect(onError).toHaveBeenCalledTimes(1);
1717
});
1818
});

packages/react/src/__tests__/text.test.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render } from '@testing-library/react';
22
import * as React from 'react';
3-
import { Numeric, Text } from '../components';
3+
import { Text } from '../components';
44
import { TestWrapper } from './test_wrapper';
55

66
describe('Text', () => {
@@ -24,16 +24,6 @@ describe('Text', () => {
2424
expect(getByTestId('translation')).toHaveTextContent('test');
2525
});
2626

27-
it('Should render formatted message for en', () => {
28-
const { getByTestId } = render(
29-
<TestWrapper>
30-
<Text id='hello' name={<Numeric value={1000} />} />
31-
</TestWrapper>,
32-
);
33-
34-
expect(getByTestId('translation')).toHaveTextContent('Hello 1,000!');
35-
});
36-
3727
it('Should render formatted message for ru', () => {
3828
const { getByTestId } = render(
3929
<TestWrapper language='ru'>
Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import {
2-
FormatMessageOptions,
3-
getTranslationParts,
4-
TranslationError,
5-
} from '@eo-locale/core';
6-
import React, { FC, Fragment, PropsWithChildren } from 'react';
7-
import { renderToStaticMarkup } from 'react-dom/server';
1+
import { FormatMessageOptions } from '@eo-locale/core';
2+
import React, { createElement, FC, PropsWithChildren } from 'react';
83
import { useTranslator } from '../hooks';
94

105
interface Props extends FormatMessageOptions {
6+
defaultMessage?: string;
7+
html?: boolean;
118
id: string;
129
tagName?: keyof React.ReactHTML;
13-
html?: boolean;
1410
}
1511

1612
export const Text: FC<PropsWithChildren<Props>> = ({
@@ -22,45 +18,15 @@ export const Text: FC<PropsWithChildren<Props>> = ({
2218
...values
2319
}) => {
2420
const translator = useTranslator();
25-
const message = translator.getMessageById(id, defaultMessage);
26-
27-
if (typeof message === 'string') {
28-
try {
29-
const parts: React.ReactNode[] = getTranslationParts(
30-
translator.language,
31-
message,
32-
values,
33-
).map((part, index) => {
34-
if (React.isValidElement(part)) {
35-
if (html) {
36-
return renderToStaticMarkup(part);
37-
}
38-
39-
return React.cloneElement(part, {
40-
key: index,
41-
});
42-
}
43-
44-
if (html) {
45-
return part;
46-
}
47-
48-
return <Fragment key={index}>{part}</Fragment>;
49-
});
50-
51-
if (html) {
52-
return React.createElement(tagName, {
53-
dangerouslySetInnerHTML: {
54-
__html: parts.join(''),
55-
},
56-
});
57-
}
58-
59-
return <Fragment>{parts}</Fragment>;
60-
} catch (error) {
61-
translator.onError(new TranslationError(id, translator.language));
62-
}
21+
const result = translator.translate(id, defaultMessage, values);
22+
23+
if (html) {
24+
return createElement(tagName, {
25+
dangerouslySetInnerHTML: {
26+
__html: result,
27+
},
28+
});
6329
}
6430

65-
return <Fragment>{String(message)}</Fragment>;
31+
return result;
6632
};

0 commit comments

Comments
 (0)