Use English as fallback language

This commit is contained in:
Deluan 2020-05-02 11:21:08 -04:00 committed by Deluan Quintão
parent 41cf99541d
commit 2f7443e4bd
3 changed files with 43 additions and 23 deletions

View File

@ -6,7 +6,7 @@
"fields": { "fields": {
"albumArtist": "Album Artist", "albumArtist": "Album Artist",
"duration": "Time", "duration": "Time",
"trackNumber": "Track #", "trackNumber": "#",
"playCount": "Plays", "playCount": "Plays",
"title": "Title", "title": "Title",
"artist": "Artist", "artist": "Artist",
@ -19,9 +19,7 @@
"updatedAt": "Uploaded at" "updatedAt": "Uploaded at"
}, },
"actions": { "actions": {
"addToQueue": "Play Later" "addToQueue": "Play Later",
},
"action": {
"playNow": "Play Now" "playNow": "Play Now"
} }
}, },

View File

@ -1,28 +1,61 @@
import polyglotI18nProvider from 'ra-i18n-polyglot' import polyglotI18nProvider from 'ra-i18n-polyglot'
import { useGetList } from 'react-admin'
import deepmerge from 'deepmerge'
import dataProvider from '../dataProvider' import dataProvider from '../dataProvider'
import en from './en.json' import en from './en.json'
// Only returns current selected locale if its translations are found in localStorage
const defaultLocale = function () { const defaultLocale = function () {
const locale = localStorage.getItem('locale') const locale = localStorage.getItem('locale')
const current = JSON.parse(localStorage.getItem('translation')) const current = localStorage.getItem('translation')
if (current && current.id === locale) { if (current && current.id === locale) {
return locale return locale
} }
return 'en' return 'en'
} }
const i18nProvider = polyglotI18nProvider((locale) => { const prepareLanguage = (lang) => {
if (locale === 'en') { // Make "albumSongs" resource use the same translations as "song"
return en 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)
} }
const i18nProvider = polyglotI18nProvider((locale) => {
// English is bundled
if (locale === 'en') {
return prepareLanguage(en)
}
// If the requested locale is in already loaded, return it
const current = JSON.parse(localStorage.getItem('translation')) const current = JSON.parse(localStorage.getItem('translation'))
if (current && current.id === locale) { if (current && current.id === locale) {
return JSON.parse(current.data) return prepareLanguage(JSON.parse(current.data))
} }
// If not, get it from the server, and store it in localStorage
return dataProvider.getOne('translation', { id: locale }).then((res) => { return dataProvider.getOne('translation', { id: locale }).then((res) => {
localStorage.setItem('translation', JSON.stringify(res.data)) localStorage.setItem('translation', JSON.stringify(res.data))
return JSON.parse(res.data.data) return prepareLanguage(JSON.parse(res.data.data))
}) })
}, defaultLocale()) }, defaultLocale())
export default i18nProvider 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

@ -5,7 +5,6 @@ import {
SelectInput, SelectInput,
SimpleForm, SimpleForm,
Title, Title,
useGetList,
useLocale, useLocale,
useSetLocale, useSetLocale,
useTranslate, useTranslate,
@ -15,6 +14,7 @@ import HelpOutlineIcon from '@material-ui/icons/HelpOutline'
import { changeTheme } from './actions' import { changeTheme } from './actions'
import themes from '../themes' import themes from '../themes'
import { docsUrl } from '../utils/docsUrl' import { docsUrl } from '../utils/docsUrl'
import { useGetLanguageChoices } from '../i18nProvider'
const useStyles = makeStyles({ const useStyles = makeStyles({
root: { marginTop: '1em' }, root: { marginTop: '1em' },
@ -35,23 +35,12 @@ const HelpMsg = ({ caption }) => (
) )
const SelectLanguage = (props) => { const SelectLanguage = (props) => {
const { ids, data, loaded } = useGetList(
'translation',
{ page: 1, perPage: -1 },
{ field: '', order: '' },
{}
)
const translate = useTranslate() const translate = useTranslate()
const setLocale = useSetLocale() const setLocale = useSetLocale()
const locale = useLocale() const locale = useLocale()
const { choices } = useGetLanguageChoices()
const langChoices = [{ id: 'en', name: 'English' }] choices.push({
if (loaded) {
ids.forEach((id) => langChoices.push({ id: id, name: data[id].name }))
}
langChoices.sort((a, b) => a.name.localeCompare(b.name))
langChoices.push({
id: helpKey, id: helpKey,
name: <HelpMsg caption={'Help to translate'} />, name: <HelpMsg caption={'Help to translate'} />,
}) })
@ -62,7 +51,7 @@ const SelectLanguage = (props) => {
source="language" source="language"
label={translate('menu.personal.options.language')} label={translate('menu.personal.options.language')}
defaultValue={locale} defaultValue={locale}
choices={langChoices} choices={choices}
translateChoice={false} translateChoice={false}
onChange={(event) => { onChange={(event) => {
if (event.target.value === helpKey) { if (event.target.value === helpKey) {