Created
May 3, 2022 20:30
-
-
Save nilsandrey/9d091c5a073f96a4c680d05d2aab9214 to your computer and use it in GitHub Desktop.
i18n useTranslation().t wrapper with placeholder loader from MUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Skeleton } from '@mui/material'; | |
import { useTranslation } from 'next-i18next'; | |
import * as React from 'react'; | |
export interface Props { | |
textId: string; | |
width?: string; | |
} | |
/** | |
* Wrap `userTranslation().t` object of i18n. Shows a loading placeholder | |
* until the translation resources are ready. | |
* | |
* ## Usage | |
* | |
* Sample: | |
* | |
* ```html | |
* <Lan textId='web.waiting.home.eventNotStartedDetails.upcomingEvent' /> | |
* ``` | |
* | |
* @returns string | |
*/ | |
function Lan({ textId, width }: Props) { | |
const { t, ready } = useTranslation(); | |
const lastLength = (strArr: string[]) => (strArr[strArr.length - 1].length); | |
const defaultWidth = 12 * (lastLength(textId.split('.')) || 1); | |
return <span>{ready ? t(textId) : <Skeleton variant="text" width={width || defaultWidth} />}</span>; | |
} | |
export default Lan; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment