Skip to content

Commit fe6a6c7

Browse files
committed
Display locales as country flags rather than language codes
Thanks @fnxpt for the suggestion! Signed-off-by: nscuro <nscuro@protonmail.com>
1 parent 6875e37 commit fe6a6c7

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/views/components/LocalePicker.vue

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
:value="locale"
1515
:title="$t(`language.${locale}`)"
1616
>
17-
{{ locale.toUpperCase() }}
17+
{{ localeToFlag(locale) }}
1818
</b-form-select-option>
1919
</b-form-select>
2020
</b-input-group>
@@ -36,6 +36,26 @@ export default {
3636
localStorage.setItem('Locale', value);
3737
this.$i18n.locale = value;
3838
},
39+
localeToFlag: function (locale) {
40+
// Largely taken from wojtekmaj/country-code-to-flag-emoji. Adopted to be able to deal with locale codes as inputs.
41+
// https://github.com/wojtekmaj/country-code-to-flag-emoji/blob/ff0d3d2dd9680b6f860d85fc9e713e93e396adb7/src/index.ts
42+
43+
let countryCode = locale.split('-').pop().toUpperCase();
44+
if (countryCode === 'EN') {
45+
countryCode = 'US'; // Sorry Britain!
46+
} else if (countryCode === 'HI') {
47+
countryCode = 'IN';
48+
} else if (countryCode === 'JA') {
49+
countryCode = 'JP';
50+
} else if (countryCode === 'ZH') {
51+
countryCode = 'CN';
52+
}
53+
54+
return Array.from(countryCode)
55+
.map((letter) => letter.toLowerCase().charCodeAt(0) + 127365)
56+
.map((charCode) => String.fromCodePoint(charCode))
57+
.join('');
58+
},
3959
},
4060
};
4161
</script>

0 commit comments

Comments
 (0)