|
| 1 | +<template> |
| 2 | + <Combobox |
| 3 | + clearable |
| 4 | + taggable |
| 5 | + :options="options" |
| 6 | + :read-only="isReadOnly" |
| 7 | + :placeholder="null" |
| 8 | + :model-value="value" |
| 9 | + @update:modelValue="comboboxUpdated" |
| 10 | + > |
| 11 | + <template #selected-option="{ option: { value, label, sample } }"> |
| 12 | + <template v-if="value === 'language'">{{ label }}</template> |
| 13 | + <div v-else class="w-full flex justify-between"> |
| 14 | + <div class="text-start flex-1"> |
| 15 | + {{ getLabel(value) }} |
| 16 | + <span class="ms-4 text-gray-500 dark:text-gray-400" v-text="value" /> |
| 17 | + </div> |
| 18 | + <span class="text-gray-500 dark:text-gray-400" v-text="getSample(value)" /> |
| 19 | + </div> |
| 20 | + </template> |
| 21 | + <template #option="{ value, label, sample }"> |
| 22 | + <template v-if="value === 'language'">{{ label }}</template> |
| 23 | + <div v-else class="w-full flex justify-between"> |
| 24 | + <div class="text-start flex-1"> |
| 25 | + {{ label }} |
| 26 | + <span class="ms-4 text-gray-500 dark:text-gray-400" v-text="value" /> |
| 27 | + </div> |
| 28 | + <span class="text-gray-500 dark:text-gray-400" v-text="sample" /> |
| 29 | + </div> |
| 30 | + </template> |
| 31 | + </Combobox> |
| 32 | +</template> |
| 33 | + |
| 34 | +<script setup> |
| 35 | +import Fieldtype from '@/components/fieldtypes/fieldtype.js'; |
| 36 | +import { dateFormatter, toast } from '@api'; |
| 37 | +import { Combobox } from '@/components/ui'; |
| 38 | +import { computed } from 'vue'; |
| 39 | +
|
| 40 | +const emit = defineEmits(Fieldtype.emits); |
| 41 | +const props = defineProps(Fieldtype.props); |
| 42 | +const { isReadOnly, update } = Fieldtype.use(emit, props); |
| 43 | +
|
| 44 | +const candidateLocales = [ |
| 45 | + 'ar', 'az', 'cs', 'da', 'de', 'de-CH', 'en', 'es', 'et', 'fa', 'fr', |
| 46 | + 'hu', 'id', 'it', 'ja', 'ms', 'nb', 'nl', 'pl', 'pt', 'pt-BR', 'ru', |
| 47 | + 'sl', 'sv', 'tr', 'uk', 'vi', 'zh-CN', 'zh-TW', |
| 48 | +]; |
| 49 | +
|
| 50 | +const displayNames = typeof Intl.DisplayNames !== 'undefined' |
| 51 | + ? new Intl.DisplayNames([document.documentElement.lang || 'en'], { type: 'language' }) |
| 52 | + : null; |
| 53 | +
|
| 54 | +const options = computed(() => { |
| 55 | + const locales = Intl.DateTimeFormat.supportedLocalesOf(candidateLocales); |
| 56 | +
|
| 57 | + const formatted = locales.map((locale) => ({ |
| 58 | + value: locale, |
| 59 | + label: getLabel(locale), |
| 60 | + sample: getSample(locale), |
| 61 | + })); |
| 62 | +
|
| 63 | + return [ |
| 64 | + { value: 'language', label: __('Same as language') }, |
| 65 | + ...formatted, |
| 66 | + ]; |
| 67 | +}); |
| 68 | +
|
| 69 | +function getLabel(locale) { |
| 70 | + return displayNames?.of(locale.split('-')[0]); |
| 71 | +} |
| 72 | +
|
| 73 | +function getSample(locale) { |
| 74 | + return dateFormatter.withLocale(locale, (formatter) => formatter.format(new Date, 'datetime')); |
| 75 | +} |
| 76 | +
|
| 77 | +function comboboxUpdated(value) { |
| 78 | + if (value && !isValidLocale(value)) { |
| 79 | + update(null); |
| 80 | + toast.error(__('statamic::messages.preference_formatting_locale_invalid')); |
| 81 | + return; |
| 82 | + } |
| 83 | +
|
| 84 | + update(value || null); |
| 85 | +} |
| 86 | +
|
| 87 | +function isValidLocale(value) { |
| 88 | + if (value === 'language') { |
| 89 | + return true; |
| 90 | + } |
| 91 | +
|
| 92 | + try { |
| 93 | + return Intl.DateTimeFormat.supportedLocalesOf([value]).length > 0; |
| 94 | + } catch { |
| 95 | + return false; |
| 96 | + } |
| 97 | +} |
| 98 | +</script> |
0 commit comments