|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | import { PARAM_LANGUAGE } from '../../utils/config-params';
|
9 |
| -import { useCallback, useEffect, useMemo, useState } from 'react'; |
10 |
| -import { getComputedLanguage } from '../../utils/language'; |
11 |
| -import localizedCountries from 'localized-countries'; |
12 |
| -import countriesFr from 'localized-countries/data/fr.json'; |
13 |
| -import countriesEn from 'localized-countries/data/en.json'; |
14 | 9 | import { useParameterState } from 'components/dialogs/parameters/use-parameters-state';
|
| 10 | +import { useLocalizedCountries as useLocalizedCountriesCUI } from '@gridsuite/commons-ui'; |
15 | 11 |
|
16 | 12 | export const useLocalizedCountries = () => {
|
17 | 13 | const [languageLocal] = useParameterState(PARAM_LANGUAGE);
|
18 |
| - const [localizedCountriesModule, setLocalizedCountriesModule] = useState<localizedCountries.LocalizedCountries>(); |
19 |
| - |
20 |
| - //TODO FM this is disgusting, can we make it better ? |
21 |
| - useEffect(() => { |
22 |
| - const lang = getComputedLanguage(languageLocal).substr(0, 2); |
23 |
| - let localizedCountriesResult: localizedCountries.LocalizedCountries; |
24 |
| - // vite does not support ESM dynamic imports on node_modules, so we have to imports the languages before and do this |
25 |
| - // https://github.com/vitejs/vite/issues/14102 |
26 |
| - if (lang === 'fr') { |
27 |
| - localizedCountriesResult = localizedCountries(countriesFr); |
28 |
| - } else if (lang === 'en') { |
29 |
| - localizedCountriesResult = localizedCountries(countriesEn); |
30 |
| - } else { |
31 |
| - console.warn('Unsupported language "' + lang + '" for countries translation, we use english as default'); |
32 |
| - localizedCountriesResult = localizedCountries(countriesEn); |
33 |
| - } |
34 |
| - setLocalizedCountriesModule(localizedCountriesResult); |
35 |
| - }, [languageLocal]); |
36 |
| - |
37 |
| - const countryCodes = useMemo( |
38 |
| - () => (localizedCountriesModule ? Object.keys(localizedCountriesModule.object()) : []), |
39 |
| - [localizedCountriesModule] |
40 |
| - ); |
41 |
| - |
42 |
| - const translate = useCallback( |
43 |
| - (countryCode: string) => (localizedCountriesModule ? localizedCountriesModule.get(countryCode) : ''), |
44 |
| - [localizedCountriesModule] |
45 |
| - ); |
| 14 | + const { translate, countryCodes } = useLocalizedCountriesCUI(languageLocal); |
46 | 15 |
|
47 | 16 | return { translate, countryCodes };
|
48 | 17 | };
|
0 commit comments