All files / src/composables useLanguageFlags.ts

96.66% Statements 29/30
88% Branches 22/25
100% Functions 9/9
100% Lines 25/25

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                  34x   34x           34x       34x 34x 34x                         33x 33x 5x 5x 5x 5x 4x       883x 29x   883x         3x               883x 883x     18x 18x 18x           1485x 1471x     883x                
import { computed, readonly, ref } from 'vue'
import { getLanguageInfo } from '@/utils/languages'
 
/**
 * How languages are shown next to their names: with their flag emoji, or by
 * name only (a language is not a country: French is 🇫🇷 and 🇨🇦).
 */
export type LanguageFlagPreference = 'flags' | 'names'
 
const FLAG_STORAGE_KEY = 'uiLanguageFlagPreference'
 
const flagPreference = ref<LanguageFlagPreference>(readStoredPreference())
 
// Detected once, on first use: the answer depends on the device's fonts only.
let flagsSupportedCache: boolean | undefined
 
function isValidPreference(value: string | null): value is LanguageFlagPreference {
  return value === 'flags' || value === 'names'
}
 
function readStoredPreference(): LanguageFlagPreference {
  Iif (typeof window === 'undefined') return 'flags'
  const stored = window.localStorage.getItem(FLAG_STORAGE_KEY)
  return isValidPreference(stored) ? stored : 'flags'
}
 
/**
 * Whether the system emoji font draws flag emoji. A flag is two Regional
 * Indicator Symbols (🇫🇷 = F + R): a font with flags draws them as one glyph,
 * a font without (Segoe UI Emoji on Windows) draws two letters side by side,
 * about twice as wide as one of them alone.
 *
 * When the width cannot be measured (no canvas), flags are assumed to work:
 * hiding them on every such device would be the worse mistake.
 */
export function detectFlagEmojiSupport(doc: Document = document): boolean {
  const context = doc.createElement('canvas').getContext('2d')
  if (!context) return true
  context.font = '32px sans-serif'
  const flagWidth = context.measureText('\u{1F1EB}\u{1F1F7}').width
  const letterWidth = context.measureText('\u{1F1EB}').width
  if (!flagWidth || !letterWidth) return true
  return flagWidth < letterWidth * 1.5
}
 
function flagsSupported(): boolean {
  if (flagsSupportedCache === undefined) {
    flagsSupportedCache = typeof document === 'undefined' ? true : detectFlagEmojiSupport()
  }
  return flagsSupportedCache
}
 
/** Forget the detected support, so tests can detect again with another canvas */
export function resetFlagSupportDetection(): void {
  flagsSupportedCache = undefined
}
 
/**
 * The user's choice of showing language flags, kept on this device like the
 * theme, since whether flags can be drawn depends on the device too.
 */
export function useLanguageFlags() {
  const canShowFlags = flagsSupported()
  const showFlags = computed(() => canShowFlags && flagPreference.value === 'flags')
 
  function setFlagPreference(preference: LanguageFlagPreference): void {
    flagPreference.value = preference
    Eif (typeof window !== 'undefined') {
      window.localStorage.setItem(FLAG_STORAGE_KEY, preference)
    }
  }
 
  /** The flag of a language, or an empty string when flags are not shown */
  function flagFor(code: string): string {
    if (!showFlags.value) return ''
    return getLanguageInfo(code)?.flag ?? ''
  }
 
  return {
    canShowFlags,
    flagFor,
    flagPreference: readonly(flagPreference),
    setFlagPreference,
    showFlags
  }
}