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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
<!--
- SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div class="locale">
<NcSelect :aria-label-listbox="t('settings', 'Locales')"
class="locale__select"
:clearable="false"
:input-id="inputId"
label="name"
label-outside
:options="allLocales"
:value="locale"
@option:selected="updateLocale" />
<div class="example">
<MapClock :size="20" />
<div class="example__text">
<p>
<span>{{ example.date }}</span>
<span>{{ example.time }}</span>
</p>
<p>
{{ t('settings', 'Week starts on {firstDayOfWeek}', { firstDayOfWeek: example.firstDayOfWeek }) }}
</p>
</div>
</div>
</div>
</template>
<script>
import moment from '@nextcloud/moment'
import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js'
import MapClock from 'vue-material-design-icons/MapClock.vue'
import { ACCOUNT_SETTING_PROPERTY_ENUM } from '../../../constants/AccountPropertyConstants.js'
import { savePrimaryAccountProperty } from '../../../service/PersonalInfo/PersonalInfoService.js'
import { handleError } from '../../../utils/handlers.ts'
export default {
name: 'Locale',
components: {
MapClock,
NcSelect,
},
props: {
inputId: {
type: String,
default: null,
},
locale: {
type: Object,
required: true,
},
localesForLanguage: {
type: Array,
required: true,
},
otherLocales: {
type: Array,
required: true,
},
},
data() {
return {
initialLocale: this.locale,
intervalId: 0,
example: {
date: moment().format('L'),
time: moment().format('LTS'),
firstDayOfWeek: window.dayNames[window.firstDay],
},
}
},
computed: {
/**
* All available locale, sorted like: current, common, other
*/
allLocales() {
const common = this.localesForLanguage.filter(l => l.code !== this.locale.code)
const other = this.otherLocales.filter(l => l.code !== this.locale.code)
return [this.locale, ...common, ...other]
},
},
mounted() {
this.intervalId = window.setInterval(this.refreshExample, 1000)
},
beforeDestroy() {
window.clearInterval(this.intervalId)
},
methods: {
async updateLocale(locale) {
try {
const responseData = await savePrimaryAccountProperty(ACCOUNT_SETTING_PROPERTY_ENUM.LOCALE, locale.code)
this.handleResponse({
locale,
status: responseData.ocs?.meta?.status,
})
window.location.reload()
} catch (e) {
this.handleResponse({
errorMessage: t('settings', 'Unable to update locale'),
error: e,
})
}
},
handleResponse({ locale, status, errorMessage, error }) {
if (status === 'ok') {
this.initialLocale = locale
} else {
this.$emit('update:locale', this.initialLocale)
handleError(error, errorMessage)
}
},
refreshExample() {
this.example = {
date: moment().format('L'),
time: moment().format('LTS'),
firstDayOfWeek: window.dayNames[window.firstDay],
}
},
},
}
</script>
<style lang="scss" scoped>
.locale {
display: grid;
#{&}__select {
margin-top: 6px; // align with other inputs
}
}
.example {
margin: 10px 0;
display: flex;
gap: 0 10px;
color: var(--color-text-maxcontrast);
&:deep(.material-design-icon) {
align-self: flex-start;
margin-top: 2px;
}
}
</style>
|