Skip to content

Instantly share code, notes, and snippets.

@nilsandrey
Created May 3, 2022 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilsandrey/9d091c5a073f96a4c680d05d2aab9214 to your computer and use it in GitHub Desktop.
Save nilsandrey/9d091c5a073f96a4c680d05d2aab9214 to your computer and use it in GitHub Desktop.
i18n useTranslation().t wrapper with placeholder loader from MUI
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