Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 46x 4x 8x 40x 4x 2x 1x 42x 42x 42x 42x 42x 42x 46x 4x 4x 42x 42x 34x 8x 42x 46x 4x 4x 42x 42x 31x 31x 2x 2x 48x 48x 50x 50x 42x 42x 42x | <template>
<div class="multilingual-textarea">
<div
v-if="label && showLanguageSelector"
class="d-flex justify-content-between align-items-center mb-2"
>
<label :for="textareaId" class="form-label mb-0">
{{ label }}
<span v-if="required" class="text-danger">*</span>
</label>
<div class="language-flags">
<button
v-for="lang in enabledLanguages"
:key="lang"
type="button"
:class="['btn btn-sm language-flag', { active: currentLang === lang }]"
:title="getLanguageName(lang)"
@click="currentLang = lang"
>
{{ getFlag(lang) }}
</button>
</div>
</div>
<div v-else-if="showLanguageSelector" class="language-flags mb-2">
<button
v-for="lang in enabledLanguages"
:key="lang"
type="button"
:class="['btn btn-sm language-flag', { active: currentLang === lang }]"
:title="getLanguageName(lang)"
@click="currentLang = lang"
>
{{ getFlag(lang) }}
</button>
</div>
<label v-else-if="label" :for="textareaId" class="form-label">
{{ label }}
<span v-if="required" class="text-danger">*</span>
</label>
<textarea
:id="textareaId"
v-model="currentValue"
:class="['form-control', textareaClass]"
:placeholder="placeholder"
:required="required && isDefaultLanguage"
:rows="rows"
@input="updateValue"
@blur="$emit('blur')"
/>
<small v-if="showLanguageHint" class="text-muted">
<span v-if="!currentValue && hasOtherTranslations" class="text-warning">
(empty, will fallback to {{ getLanguageName(defaultLanguage) }})
</span>
</small>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { getLanguageInfo } from '@/utils/languages'
interface Props {
modelValue: string | Record<string, string>
label?: string
placeholder?: string
required?: boolean
textareaClass?: string
rows?: number
defaultLanguage?: string
enabledLanguages?: string[]
showLanguageHint?: boolean
currentLanguage?: string
showLanguageSelector?: boolean
inputId?: string
}
const props = withDefaults(defineProps<Props>(), {
label: '',
placeholder: '',
textareaClass: '',
required: false,
rows: 3,
defaultLanguage: 'en',
enabledLanguages: () => ['en', 'fr'],
showLanguageHint: true,
currentLanguage: undefined,
showLanguageSelector: true,
inputId: undefined
})
const emit = defineEmits<{
'update:modelValue': [value: string | Record<string, string>]
blur: []
}>()
const textareaId = computed(
() => props.inputId ?? `multilingual-textarea-${Math.random().toString(36).substr(2, 9)}`
)
const internalCurrentLang = ref(props.defaultLanguage)
const currentLang = computed({
get() {
return props.currentLanguage !== undefined ? props.currentLanguage : internalCurrentLang.value
},
set(value: string) {
Eif (props.currentLanguage === undefined) {
internalCurrentLang.value = value
}
}
})
const normalizedValue = computed(() => {
if (typeof props.modelValue === 'string') {
return { [props.defaultLanguage]: props.modelValue }
}
return props.modelValue || {}
})
const currentValue = computed({
get() {
return normalizedValue.value[currentLang.value] || ''
},
set(value: string) {
const updated = { ...normalizedValue.value, [currentLang.value]: value }
emit('update:modelValue', updated)
}
})
const isDefaultLanguage = computed(() => currentLang.value === props.defaultLanguage)
const hasOtherTranslations = computed(() => {
return Object.keys(normalizedValue.value).some(
(lang) => lang !== currentLang.value && normalizedValue.value[lang]
)
})
function updateValue(event: Event) {
const target = event.target as HTMLTextAreaElement
currentValue.value = target.value
}
function getFlag(lang: string): string {
const info = getLanguageInfo(lang)
return info?.flag || lang.toUpperCase()
}
function getLanguageName(lang: string): string {
const info = getLanguageInfo(lang)
return info?.nativeName || lang
}
// Auto-switch to default language if current language becomes disabled
watch(
() => props.enabledLanguages,
(newLangs) => {
Iif (props.currentLanguage === undefined && !newLangs.includes(internalCurrentLang.value)) {
internalCurrentLang.value = props.defaultLanguage
}
},
{ immediate: true }
)
</script>
<style scoped>
.multilingual-textarea {
width: 100%;
}
.language-flags {
display: flex;
gap: 0.25rem;
}
.language-flag {
padding: 0.125rem 0.375rem;
border: 1px solid #ddd;
background: white;
border-radius: 3px;
font-size: 0.875rem;
line-height: 1;
cursor: pointer;
transition: all 0.2s;
opacity: 0.6;
}
.language-flag:hover {
opacity: 0.8;
border-color: #0d6efd;
}
.language-flag.active {
opacity: 1;
border-color: #0d6efd;
background: #e7f1ff;
box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.15);
}
</style>
|