mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-12 05:12:22 +03:00
Reload translations when reloading the app
This commit is contained in:
parent
1825b29737
commit
1be79fa945
@ -2,17 +2,29 @@ import polyglotI18nProvider from 'ra-i18n-polyglot'
|
|||||||
import deepmerge from 'deepmerge'
|
import deepmerge from 'deepmerge'
|
||||||
import dataProvider from '../dataProvider'
|
import dataProvider from '../dataProvider'
|
||||||
import en from './en.json'
|
import en from './en.json'
|
||||||
|
import { i18nProvider } from './index'
|
||||||
|
|
||||||
// Only returns current selected locale if its translations are found in localStorage
|
// 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 = JSON.parse(localStorage.getItem('translation'))
|
||||||
if (current && current.id === locale) {
|
if (current && current.id === locale) {
|
||||||
|
// Asynchronously reload the translation from the server
|
||||||
|
retrieveTranslation(locale).then(() => {
|
||||||
|
i18nProvider.changeLocale(locale)
|
||||||
|
})
|
||||||
return locale
|
return locale
|
||||||
}
|
}
|
||||||
return 'en'
|
return 'en'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) => {
|
const removeEmpty = (obj) => {
|
||||||
for (let k in obj) {
|
for (let k in obj) {
|
||||||
if (obj.hasOwnProperty(k) && typeof obj[k] === 'object') {
|
if (obj.hasOwnProperty(k) && typeof obj[k] === 'object') {
|
||||||
@ -47,8 +59,5 @@ export default polyglotI18nProvider((locale) => {
|
|||||||
return prepareLanguage(JSON.parse(current.data))
|
return prepareLanguage(JSON.parse(current.data))
|
||||||
}
|
}
|
||||||
// If not, get it from the server, and store it in localStorage
|
// If not, get it from the server, and store it in localStorage
|
||||||
return dataProvider.getOne('translation', { id: locale }).then((res) => {
|
return retrieveTranslation(locale)
|
||||||
localStorage.setItem('translation', JSON.stringify(res.data))
|
|
||||||
return prepareLanguage(JSON.parse(res.data.data))
|
|
||||||
})
|
|
||||||
}, defaultLocale())
|
}, defaultLocale())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user