-
Notifications
You must be signed in to change notification settings - Fork 49
/
localable.js
37 lines (33 loc) · 982 Bytes
/
localable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import Vue from 'vue';
import * as locales from '../locale';
import { DEFAULT_LOCALE_PROPERTIES } from '../constants';
export default Vue.extend({
name: 'Localable',
props: {
locale: { type: Object, default: () => ({ lang: undefined }) },
},
computed: {
currentLocale () {
const { lang } = this.locale;
return { ...this.locale, lang: this.getLocale(lang) };
},
},
methods: {
getDefaultLang () {
return (
(this.$vuedatepicker && this.$vuedatepicker.lang) ||
window.navigator.userLanguage ||
window.navigator.language ||
'en'
).substr(0, 2);
},
isValidLocale (lang = {}) {
const properties = Object.keys(lang);
return properties.length > 0 &&
properties.every(property => DEFAULT_LOCALE_PROPERTIES.includes(property));
},
getLocale (lang) {
return this.isValidLocale(lang) ? lang : locales[lang] || this.getLocale(this.getDefaultLang());
},
},
});