react-i18next#Namespace TypeScript Examples

The following examples show how to use react-i18next#Namespace. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LanguageKeySelect.tsx    From clearflask with Apache License 2.0 5 votes vote down vote up
LanguageKeySelect = (props: {
  ns: Namespace;
  value: string;
  setValue: (value: string) => void;
}) => {
  const { i18n } = useTranslation(props.ns);
  const classes = useStyles();

  const [anchor, setAnchor] = useState<HTMLButtonElement>();

  const [fuse, setFuse] = useState<Fuse<SearchEntry>>();
  useEffect(() => {
    if (!anchor) return;
    const bundle = i18n.getResourceBundle(i18n.language, props.ns as string) as { [k: string]: string };
    const bundleArr: SearchEntry[] = Object.entries(bundle)
      .filter(([k, v]) => !k.includes('{{'))
      .map(([k, v]) => ({ k, v }));
    setFuse(new Fuse(bundleArr, {
      keys: ['k', 'v'],
    }));
  }, [anchor, props.ns, i18n.language]); // eslint-disable-line react-hooks/exhaustive-deps

  const results = fuse?.search(props.value || '', {
    limit: 5,
  });

  return (
    <IconButton
      aria-label='Select translated text'
      onClick={e => setAnchor(e.currentTarget)}
      color={i18n.exists(props.value) ? 'primary' : undefined}
    >
      <TranslateIcon fontSize='inherit' />
      <ClosablePopper
        anchorType='element'
        anchor={anchor}
        open={!!anchor}
        onClose={() => setAnchor(undefined)}
        placement='bottom-end'
        arrow
        clickAway
        closeButtonPosition='disable'
      >
        {!results?.length && (
          <div className={classes.labelMessage}>
            No translation found
          </div>
        )}
        <div className={classes.table}>
          <Table size='medium'>
            <TableBody>
              {results?.map(result => (
                <TableRow
                  key={result.item.k}
                  hover
                  selected={result.item.k === props.value}
                  onClick={() => {
                    props.setValue(result.item.k);
                    setAnchor(undefined);
                  }}
                >
                  <div className={classes.labelOptionContainer}>
                    {result.item.v}
                  </div>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </div>
      </ClosablePopper>
    </IconButton>
  );
}
Example #2
Source File: i18n.ts    From clearflask with Apache License 2.0 5 votes vote down vote up
T = <N extends Namespace>(key: TFuncKey<N>): string => key
Example #3
Source File: useI18next.ts    From gatsby-plugin-react-i18next with MIT License 5 votes vote down vote up
useI18next = (ns?: Namespace, options?: UseTranslationOptions) => {
  const {i18n, t, ready} = useTranslation(ns, options);
  const context = useContext(I18nextContext);

  const {routed, defaultLanguage, generateDefaultLanguagePage} = context;

  const getLanguagePath = (language: string) => {
    return generateDefaultLanguagePage || language !== defaultLanguage ? `/${language}` : '';
  };

  const removePrefix = (pathname: string) => {
    const base = typeof __BASE_PATH__ !== `undefined` ? __BASE_PATH__ : __PATH_PREFIX__;
    if (base && pathname.indexOf(base) === 0) {
      pathname = pathname.slice(base.length);
    }
    return pathname;
  };

  const removeLocalePart = (pathname: string) => {
    if (!routed) return pathname;
    const i = pathname.indexOf(`/`, 1);
    return pathname.substring(i);
  };

  const navigate = (to: string, options?: NavigateOptions<{}>) => {
    const languagePath = getLanguagePath(context.language);
    const link = routed ? `${languagePath}${to}` : `${to}`;
    return gatsbyNavigate(link, options);
  };

  const changeLanguage = (language: string, to?: string, options?: NavigateOptions<{}>) => {
    const languagePath = getLanguagePath(language);
    const pathname = to || removeLocalePart(removePrefix(window.location.pathname));
    const link = `${languagePath}${pathname}${window.location.search}`;
    localStorage.setItem(LANGUAGE_KEY, language);
    return gatsbyNavigate(link, options);
  };

  return {
    ...context,
    i18n,
    t,
    ready,
    navigate,
    changeLanguage
  };
}