All files / src/components MultilingualInput.vue

95.12% Statements 39/41
89.13% Branches 41/46
95% Functions 19/20
94.87% Lines 37/39

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 201 202 203 204 205  193x                 9x 4x                         57x           6x                       29x             1x                                                             144x                             144x         144x 144x   144x   144x   151x     6x 6x         144x 185x 17x   168x     144x   192x     58x 58x       144x   144x 68x 31x         29x 29x     144x       61x       66x 66x       144x 144x   144x                                                                                    
<template>
  <div class="multilingual-input">
    <div
      v-if="label && showLanguageSelector"
      class="d-flex justify-content-between align-items-center mb-2"
    >
      <label :for="inputId" 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"
        >
          {{ getLanguageLabel(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"
      >
        {{ getLanguageLabel(lang) }}
      </button>
    </div>
    <label v-else-if="label" :for="inputId" class="form-label">
      {{ label }}
      <span v-if="required" class="text-danger">*</span>
    </label>
 
    <input
      :id="inputId"
      v-model="currentValue"
      type="text"
      :class="['form-control', inputClass]"
      :placeholder="placeholder"
      :required="required && isDefaultLanguage"
      :maxlength="maxlength"
      @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'
import { useLanguageFlags } from '@/composables/useLanguageFlags'
 
interface Props {
  modelValue?: string | Record<string, string>
  label?: string
  placeholder?: string
  required?: boolean
  inputClass?: string
  defaultLanguage?: string
  enabledLanguages?: string[]
  showLanguageHint?: boolean
  currentLanguage?: string
  showLanguageSelector?: boolean
  inputId?: string
  maxlength?: number
}
 
const props = withDefaults(defineProps<Props>(), {
  modelValue: () => ({}),
  label: '',
  placeholder: '',
  inputClass: '',
  required: false,
  defaultLanguage: 'en',
  enabledLanguages: () => ['en', 'fr'],
  showLanguageHint: true,
  currentLanguage: undefined,
  showLanguageSelector: true,
  maxlength: undefined,
  inputId: undefined
})
 
const emit = defineEmits<{
  'update:modelValue': [value: string | Record<string, string>]
  blur: []
}>()
 
const inputId = computed(
  () => props.inputId ?? `multilingual-input-${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 HTMLInputElement
  currentValue.value = target.value
}
 
const { flagFor } = useLanguageFlags()
 
/** The flag when flags are shown, else the language name: never a code like "FR" */
function getLanguageLabel(lang: string): string {
  return flagFor(lang) || getLanguageName(lang)
}
 
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-input {
  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>