useLanguageNames

This commit is contained in:
Minseo Lee 2024-08-28 17:04:57 +09:00
parent 170d1dff7a
commit e65255794e
3 changed files with 3 additions and 37 deletions

View file

@ -35,7 +35,6 @@ export * from './useFields';
export * from './useFilters';
export * from './useForceUpdate';
export * from './useFormat';
export * from './useLanguageNames';
export * from './useLocale';
export * from './useMessages';
export * from './useModified';

View file

@ -1,34 +0,0 @@
import { useState, useEffect } from 'react';
import { httpGet } from 'next-basics';
import enUS from '../../../public/intl/language/en-US.json';
const languageNames = {
'en-US': enUS,
};
export function useLanguageNames(locale) {
const [list, setList] = useState(languageNames[locale] || enUS);
async function loadData(locale) {
const { data } = await httpGet(`${process.env.basePath || ''}/intl/language/${locale}.json`);
if (data) {
languageNames[locale] = data;
setList(languageNames[locale]);
} else {
setList(enUS);
}
}
useEffect(() => {
if (!languageNames[locale]) {
loadData(locale);
} else {
setList(languageNames[locale]);
}
}, [locale]);
return list;
}
export default useLanguageNames;

View file

@ -1,6 +1,6 @@
import MetricsTable, { MetricsTableProps } from './MetricsTable';
import { percentFilter } from 'lib/filters';
import { useLanguageNames } from 'components/hooks';
import { useIntl } from 'react-intl';
import { useLocale } from 'components/hooks';
import { useMessages } from 'components/hooks';
@ -10,7 +10,8 @@ export function LanguagesTable({
}: { onDataLoad: (data: any) => void } & MetricsTableProps) {
const { formatMessage, labels } = useMessages();
const { locale } = useLocale();
const languageNames = useLanguageNames(locale);
const intl = useIntl();
const languageNames = intl.formatDisplayName(locale, { type: 'language' });
const renderLabel = ({ x }) => {
return <div className={locale}>{languageNames[x?.split('-')[0]] ?? x}</div>;