Skip to content

Commit d4a7450

Browse files
authored
Add language switch (#556)
* Add language switch * Do not break the app when DisplayNames is not available in the browser
1 parent f411f8f commit d4a7450

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

src/App.vue

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
<template>
22
<div id="app">
3+
<select class="language-switch" v-model="$i18n.locale">
4+
<option v-for="locale in displayLocales" :key="`locale-${locale.value}`" :value="locale.value">
5+
{{ locale.name }}
6+
</option>
7+
</select>
8+
39
<div class="head">
410
<a href="#" class="logo">
511
<img src="./assets/logo.png" alt="asf logo">
612
</a>
713
<h1 class="text-center" v-html="$t('app.name')"></h1>
814
</div>
15+
916
<div class="menu">
1017
<ul>
1118
<li><router-link :to="{ name: 'home' }" active-class="active" v-html="$t('link.home')" exact></router-link></li>
@@ -21,8 +28,27 @@
2128
<script>
2229
export default {
2330
name: 'app',
24-
data() {
25-
return {}
31+
computed: {
32+
availableLocales() {
33+
return this.$i18n.availableLocales.map((locale) => {
34+
return {
35+
value: locale,
36+
code: locale === 'strings' ? 'en-US' : locale
37+
}
38+
}).sort((lhs, rhs) => {
39+
return lhs.code.localeCompare(rhs.code)
40+
})
41+
},
42+
displayLocales() {
43+
return this.availableLocales.map((locale) => {
44+
if ('Intl' in window && 'DisplayNames' in Intl) {
45+
const displayNamesTranslator = new Intl.DisplayNames([locale.code], { type: 'language' });
46+
return { value: locale.value, name: displayNamesTranslator.of(locale.code) }
47+
}
48+
49+
return { value: locale.value, name: locale.code }
50+
})
51+
}
2652
}
2753
}
2854
</script>
@@ -84,4 +110,11 @@
84110
.logo {
85111
margin: 0 auto;
86112
}
113+
114+
.language-switch {
115+
position: absolute;
116+
top: 8px;
117+
left: 8px;
118+
width: 220px;
119+
}
87120
</style>

0 commit comments

Comments
 (0)