Use async/await moment loading

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2019-09-04 16:51:30 +02:00
parent c484ce4529
commit 9763d3f756
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
1 changed files with 19 additions and 21 deletions

View File

@ -130,33 +130,31 @@ export default {
}
},
mounted() {
async mounted() {
// Load the locale
// convert format like en_GB to en-gb for `moment.js`
let locale = getLocale().replace('_', '-').toLowerCase()
// default load e.g. fr-fr
console.debug('Importing locale', locale)
import('moment/locale/' + locale)
.then(e => {
// force locale change to update
// the component once done loading
this.locale = locale
})
.catch(e => {
try {
// default load e.g. fr-fr
await import('moment/locale/' + locale)
this.locale = locale
} catch (e) {
try {
// failure: fallback to fr
locale = locale.split('-')[0]
console.debug('Fallback to locale', locale)
import('moment/locale/' + locale)
.then(e => {
this.locale = locale
})
.catch(e => {
console.debug('Fallback to locale', 'en')
// failure, fallback to english
this.locale = 'en'
})
})
await import('moment/locale/' + locale)
} catch (e) {
// failure, fallback to english
console.debug('Fallback to locale', 'en')
locale = 'en'
}
} finally {
// force locale change to update
// the component once done loading
this.locale = locale
console.debug('Locale used', locale)
}
},
methods: {