2020-05-01 14:29:50 -08:00
|
|
|
import polyglotI18nProvider from 'ra-i18n-polyglot'
|
2020-05-02 07:21:08 -08:00
|
|
|
import { useGetList } from 'react-admin'
|
|
|
|
|
import deepmerge from 'deepmerge'
|
2020-05-01 14:29:50 -08:00
|
|
|
import dataProvider from '../dataProvider'
|
|
|
|
|
import en from './en.json'
|
|
|
|
|
|
2020-05-02 07:21:08 -08:00
|
|
|
// Only returns current selected locale if its translations are found in localStorage
|
2020-05-01 14:29:50 -08:00
|
|
|
const defaultLocale = function () {
|
|
|
|
|
const locale = localStorage.getItem('locale')
|
2020-05-02 07:21:08 -08:00
|
|
|
const current = localStorage.getItem('translation')
|
2020-05-01 14:29:50 -08:00
|
|
|
if (current && current.id === locale) {
|
|
|
|
|
return locale
|
|
|
|
|
}
|
|
|
|
|
return 'en'
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-02 07:21:08 -08:00
|
|
|
const prepareLanguage = (lang) => {
|
|
|
|
|
// Make "albumSongs" resource use the same translations as "song"
|
|
|
|
|
lang.resources.albumSong = lang.resources.song
|
|
|
|
|
// ra.boolean.null should always be empty
|
|
|
|
|
lang.ra.boolean.null = ''
|
|
|
|
|
// Fallback to english translations
|
|
|
|
|
return deepmerge(en, lang)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 14:29:50 -08:00
|
|
|
const i18nProvider = polyglotI18nProvider((locale) => {
|
2020-05-02 07:21:08 -08:00
|
|
|
// English is bundled
|
2020-05-01 14:29:50 -08:00
|
|
|
if (locale === 'en') {
|
2020-05-02 07:21:08 -08:00
|
|
|
return prepareLanguage(en)
|
2020-05-01 14:29:50 -08:00
|
|
|
}
|
2020-05-02 07:21:08 -08:00
|
|
|
// If the requested locale is in already loaded, return it
|
2020-05-01 14:29:50 -08:00
|
|
|
const current = JSON.parse(localStorage.getItem('translation'))
|
|
|
|
|
if (current && current.id === locale) {
|
2020-05-02 07:21:08 -08:00
|
|
|
return prepareLanguage(JSON.parse(current.data))
|
2020-05-01 14:29:50 -08:00
|
|
|
}
|
2020-05-02 07:21:08 -08:00
|
|
|
// If not, get it from the server, and store it in localStorage
|
2020-05-01 14:29:50 -08:00
|
|
|
return dataProvider.getOne('translation', { id: locale }).then((res) => {
|
|
|
|
|
localStorage.setItem('translation', JSON.stringify(res.data))
|
2020-05-02 07:21:08 -08:00
|
|
|
return prepareLanguage(JSON.parse(res.data.data))
|
2020-05-01 14:29:50 -08:00
|
|
|
})
|
|
|
|
|
}, defaultLocale())
|
|
|
|
|
|
|
|
|
|
export default i18nProvider
|
2020-05-02 07:21:08 -08:00
|
|
|
|
|
|
|
|
// 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 }
|
|
|
|
|
}
|