Skip to content

Commit

Permalink
feat: getRawMessage option
Browse files Browse the repository at this point in the history
  • Loading branch information
jungpaeng committed Oct 3, 2023
1 parent 51c2d4f commit 308e510
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/core/src/use-translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ export function useTranslation(namespace?: string) {
}, [allMessage, onError, namespace]);

return React.useCallback(
(key: string, value?: TranslationValue, format?: Partial<Formats>) => {
(
key: string,
value?: TranslationValue & { __rawValue?: boolean },
format?: Partial<Formats>,
) => {
const cachedFormatByLocale = cachedFormatByLocaleRef.current;

if (messageOrError instanceof IntlError) {
Expand Down Expand Up @@ -150,6 +154,10 @@ export function useTranslation(namespace?: string) {
);
}

if (value?.__rawValue === true) {
return message;
}

try {
// IntlMessageFormat을 실행시키며, 실패시 INVALID_MESSAGE를 반환합니다.
messageFormat = new IntlMessageFormat(
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/use-translation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ describe('use-translation', () => {
screen.getByText("It's my cat's 1st birthday!");
});

it('can return raw messages without processing them', () => {
function Component() {
const t = useTranslation();
return (
<span dangerouslySetInnerHTML={{ __html: String(t('message', { __rawValue: true })) }} />
);
}

const { container } = render(
<IntlProvider locale="en" message={{ message: '<a href="/test">Test</a><p>{hello}</p>' }}>
<Component />
</IntlProvider>,
);

expect(container.innerHTML).toBe('<span><a href="/test">Test</a><p>{hello}</p></span>');
});

it('should be print rich text', () => {
const { container } = renderMessage(
'Our price is <boldThis>{price, number, ::currency/USD precision-integer}</boldThis> with <link>{pct, number, ::percent} discount</link>',
Expand Down

0 comments on commit 308e510

Please sign in to comment.