Refactor i18n functions a bit

This commit is contained in:
Deluan 2020-05-02 17:44:24 -04:00
parent 055c77b38c
commit 151f43b95f
6 changed files with 26 additions and 24 deletions

View File

@ -16,7 +16,7 @@ import { albumViewReducer } from './album/albumState'
import customRoutes from './routes'
import themeReducer from './personal/themeReducer'
import createAdminStore from './store/createAdminStore'
import i18nProvider from './i18nProvider'
import { i18nProvider } from './i18n'
const history = createHashHistory()

4
ui/src/i18n/index.js Normal file
View File

@ -0,0 +1,4 @@
import i18nProvider from './provider'
import useGetLanguageChoices from './useGetLanguageChoices'
export { i18nProvider, useGetLanguageChoices }

View File

@ -1,5 +1,4 @@
import polyglotI18nProvider from 'ra-i18n-polyglot'
import { useGetList } from 'react-admin'
import deepmerge from 'deepmerge'
import dataProvider from '../dataProvider'
import en from './en.json'
@ -36,7 +35,7 @@ const prepareLanguage = (lang) => {
return deepmerge(en, lang)
}
const i18nProvider = polyglotI18nProvider((locale) => {
export default polyglotI18nProvider((locale) => {
// English is bundled
if (locale === 'en') {
return prepareLanguage(en)
@ -52,23 +51,3 @@ const i18nProvider = polyglotI18nProvider((locale) => {
return prepareLanguage(JSON.parse(res.data.data))
})
}, defaultLocale())
export default i18nProvider
// React Hook to get a list of all languages available. English is hardcoded
export const useGetLanguageChoices = () => {
const { ids, data, loaded, loading } = useGetList(
'translation',
{ page: 1, perPage: -1 },
{ field: '', order: '' },
{}
)
const choices = [{ id: 'en', name: 'English' }]
if (loaded) {
ids.forEach((id) => choices.push({ id: id, name: data[id].name }))
}
choices.sort((a, b) => a.name.localeCompare(b.name))
return { choices, loaded, loading }
}

View File

@ -0,0 +1,19 @@
// React Hook to get a list of all languages available. English is hardcoded
import { useGetList } from 'react-admin'
export default () => {
const { ids, data, loaded, loading } = useGetList(
'translation',
{ page: 1, perPage: -1 },
{ field: '', order: '' },
{}
)
const choices = [{ id: 'en', name: 'English' }]
if (loaded) {
ids.forEach((id) => choices.push({ id: id, name: data[id].name }))
}
choices.sort((a, b) => a.name.localeCompare(b.name))
return { choices, loaded, loading }
}

View File

@ -14,7 +14,7 @@ import HelpOutlineIcon from '@material-ui/icons/HelpOutline'
import { changeTheme } from './actions'
import themes from '../themes'
import { docsUrl } from '../utils/docsUrl'
import { useGetLanguageChoices } from '../i18nProvider'
import { useGetLanguageChoices } from '../i18n'
const useStyles = makeStyles({
root: { marginTop: '1em' },