i18next#StringMap TypeScript Examples

The following examples show how to use i18next#StringMap. 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: lang.ts    From brackets-viewer.js with MIT License 6 votes vote down vote up
/**
 * Returns an internationalized version of a locale key.
 * 
 * @param key A locale key.
 * @param options Data to pass to the i18n process.
 */
export function t<Scope extends keyof Locale, SubKey extends string & keyof Locale[Scope], T extends TOptions>(
    key: `${Scope}.${SubKey}`, options?: T,
): T['returnObjects'] extends true ? StringMap : string {
    return i18next.t(key, options);
}
Example #2
Source File: usePrefixedTranslation.ts    From lightning-terminal with MIT License 6 votes vote down vote up
usePrefixedTranslation = (prefix: string) => {
  const { t } = useTranslation();
  // the new `t` function that will append the prefix
  const translate = useCallback(
    (key: string, options?: string | StringMap) => {
      // if the key contains a '.', then don't add the prefix
      return key.includes('.') ? t(key, options) : t(`${prefix}.${key}`, options);
    },
    [prefix, t],
  );

  return {
    l: translate,
  };
}
Example #3
Source File: translate.ts    From lightning-terminal with MIT License 6 votes vote down vote up
prefixTranslation = (prefix: string) => {
  // the new `t` function that will append the prefix
  const translate = (key: string, options?: string | StringMap) => {
    return key.includes('.') ? i18n.t(key, options) : i18n.t(`${prefix}.${key}`, options);
  };

  return {
    l: translate,
  };
}
Example #4
Source File: useTranslationSafe.ts    From nextclade with MIT License 6 votes vote down vote up
export function useTranslationSafe<TInterpolationMap extends object = StringMap>() {
  const response = useTranslation()

  function t(key: string, options?: TOptions<TInterpolationMap> | string) {
    return response.t(key, options) ?? key
  }

  return { ...response, t }
}