Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

functions.tsx 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React from 'react';
  2. import { WithTranslation, withTranslation } from 'react-i18next';
  3. import i18next from './i18next';
  4. /**
  5. * Changes the main translation bundle.
  6. *
  7. * @param {string} language - The language e.g. 'en', 'fr'.
  8. * @param {string} url - The url of the translation bundle.
  9. * @returns {void}
  10. */
  11. export async function changeLanguageBundle(language: string, url: string) {
  12. const res = await fetch(url);
  13. const bundle = await res.json();
  14. i18next.addResourceBundle(language, 'main', bundle, true, true);
  15. }
  16. /**
  17. * Wraps a specific React Component in order to enable translations in it.
  18. *
  19. * @param {Component} component - The React Component to wrap.
  20. * @returns {Component} The React Component which wraps {@link component} and
  21. * enables translations in it.
  22. */
  23. export function translate<P extends WithTranslation>(component: React.ComponentType<P>) {
  24. // Use the default list of namespaces.
  25. return withTranslation([ 'main', 'languages', 'countries' ])(component);
  26. }
  27. /**
  28. * Translates a specific key to text containing HTML via a specific translate
  29. * function.
  30. *
  31. * @param {Function} t - The translate function.
  32. * @param {string} key - The key to translate.
  33. * @param {Array<*>} options - The options, if any, to pass to {@link t}.
  34. * @returns {ReactElement} A ReactElement which depicts the translated HTML
  35. * text.
  36. */
  37. export function translateToHTML(t: Function, key: string, options: Object = {}) {
  38. // eslint-disable-next-line react/no-danger
  39. return <span dangerouslySetInnerHTML = {{ __html: t(key, options) }} />;
  40. }