Files
pybricks-code/src/notifications/NotificationMessage.tsx
T
David Lechner a3e55c828a move useI18n to i18n.ts
Since we are using the react-i18n babel loader plugin, useI18n can only
be used once per file. Also we have to remember to add the istanbul
ignore next comment each time we use it. By moving this to a common
file, it can be easily copied and pasted when a new submodule is
created and we don't have to remember the rules when calling it.
2022-05-21 13:57:58 -05:00

40 lines
940 B
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2021-2022 The Pybricks Authors
// provides translation for notification text
import { Replacements } from '@shopify/react-i18n';
import React from 'react';
import { I18nId, useI18n } from './i18n';
type NotificationMessageProps = {
messageId: I18nId;
replacements?: Replacements;
};
const NotificationMessage: React.VoidFunctionComponent<NotificationMessageProps> = ({
messageId,
replacements,
}) => {
const i18n = useI18n();
let message = i18n.translate(messageId, replacements) as
| React.ReactElement
| string;
// Use newline characters to create paragraphs
if (typeof message === 'string') {
message = (
<>
{message.split('\n').map((x, i) => (
<p key={i}>{x}</p>
))}
</>
);
}
return message;
};
export default NotificationMessage;