2017-03-01 02:55:12 +00:00
|
|
|
import React from 'react';
|
2022-08-02 11:41:12 +00:00
|
|
|
import { WithTranslation, withTranslation } from 'react-i18next';
|
2017-03-01 02:55:12 +00:00
|
|
|
|
2021-12-07 10:04:33 +00:00
|
|
|
import i18next from './i18next';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the main translation bundle.
|
|
|
|
*
|
|
|
|
* @param {string} language - The language e.g. 'en', 'fr'.
|
|
|
|
* @param {string} url - The url of the translation bundle.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
export async function changeLanguageBundle(language: string, url: string) {
|
|
|
|
const res = await fetch(url);
|
|
|
|
const bundle = await res.json();
|
|
|
|
|
|
|
|
i18next.addResourceBundle(language, 'main', bundle, true, true);
|
|
|
|
}
|
|
|
|
|
2017-03-01 02:55:12 +00:00
|
|
|
/**
|
|
|
|
* Wraps a specific React Component in order to enable translations in it.
|
|
|
|
*
|
|
|
|
* @param {Component} component - The React Component to wrap.
|
|
|
|
* @returns {Component} The React Component which wraps {@link component} and
|
|
|
|
* enables translations in it.
|
|
|
|
*/
|
2022-08-02 11:41:12 +00:00
|
|
|
export function translate<P extends WithTranslation>(component: React.ComponentType<P>) {
|
2017-03-01 02:55:12 +00:00
|
|
|
// Use the default list of namespaces.
|
2019-07-22 10:10:25 +00:00
|
|
|
return withTranslation([ 'main', 'languages', 'countries' ])(component);
|
2017-03-01 02:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates a specific key to text containing HTML via a specific translate
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* @param {Function} t - The translate function.
|
|
|
|
* @param {string} key - The key to translate.
|
|
|
|
* @param {Array<*>} options - The options, if any, to pass to {@link t}.
|
|
|
|
* @returns {ReactElement} A ReactElement which depicts the translated HTML
|
|
|
|
* text.
|
|
|
|
*/
|
2021-12-07 10:04:33 +00:00
|
|
|
export function translateToHTML(t: Function, key: string, options: Object = {}) {
|
2017-03-01 02:55:12 +00:00
|
|
|
// eslint-disable-next-line react/no-danger
|
|
|
|
return <span dangerouslySetInnerHTML = {{ __html: t(key, options) }} />;
|
|
|
|
}
|