fcdd30ba8f
* feat: create vite project * feat: it's alive! * feat: `make dev` working! * feat: replace custom serviceWorker with vite plugin * test: replace Jest with Vitest * fix: run prettier * fix: skip eslint for now. * chore: remove ui.old folder * refactor: replace lodash.pick with simple destructuring * fix: eslint errors (wip) * fix: eslint errors (wip) * fix: display-name eslint errors (wip) * fix: no-console eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: build * fix: pwa manifest * refactor: pwa manifest * refactor: simplify PORT configuration * refactor: rename simple JS files * test: cover playlistUtils * fix: react-image-lightbox * feat(ui): add sourcemaps to help debug issues
67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
import polyglotI18nProvider from 'ra-i18n-polyglot'
|
|
import deepmerge from 'deepmerge'
|
|
import dataProvider from '../dataProvider'
|
|
import en from './en.json'
|
|
import { i18nProvider } from './index'
|
|
|
|
// Only returns current selected locale if its translations are found in localStorage
|
|
const defaultLocale = function () {
|
|
const locale = localStorage.getItem('locale')
|
|
const current = JSON.parse(localStorage.getItem('translation'))
|
|
if (current && current.id === locale) {
|
|
// Asynchronously reload the translation from the server
|
|
retrieveTranslation(locale).then(() => {
|
|
i18nProvider.changeLocale(locale)
|
|
})
|
|
return locale
|
|
}
|
|
return 'en'
|
|
}
|
|
|
|
export function retrieveTranslation(locale) {
|
|
return dataProvider.getOne('translation', { id: locale }).then((res) => {
|
|
localStorage.setItem('translation', JSON.stringify(res.data))
|
|
return prepareLanguage(JSON.parse(res.data.data))
|
|
})
|
|
}
|
|
|
|
const removeEmpty = (obj) => {
|
|
for (let k in obj) {
|
|
if (
|
|
Object.prototype.hasOwnProperty.call(obj, k) &&
|
|
typeof obj[k] === 'object'
|
|
) {
|
|
removeEmpty(obj[k])
|
|
} else {
|
|
if (!obj[k]) {
|
|
delete obj[k]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const prepareLanguage = (lang) => {
|
|
removeEmpty(lang)
|
|
// Make "albumSong" and "playlistTrack" resource use the same translations as "song"
|
|
lang.resources.albumSong = lang.resources.song
|
|
lang.resources.playlistTrack = lang.resources.song
|
|
// ra.boolean.null should always be empty
|
|
lang.ra.boolean.null = ''
|
|
// Fallback to english translations
|
|
return deepmerge(en, lang)
|
|
}
|
|
|
|
export default 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'))
|
|
if (current && current.id === locale) {
|
|
return prepareLanguage(JSON.parse(current.data))
|
|
}
|
|
// If not, get it from the server, and store it in localStorage
|
|
return retrieveTranslation(locale)
|
|
}, defaultLocale())
|