[Intergration] [I18n] Expose the mapped locale list from Nuxt I18n

Aperta
#7,373 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
nuxt, nuxtjs, typescript

Direzione di ricerca

Read the locale mapping added in PR #7152 and its integration point for Nuxt I18n. Trace how the mapped current locale is exposed, then identify how to expose the configured mapped locale list for ULocaleSelect. Done means consumers can use that list without importing every Nuxt UI locale, while retaining the mapped locale types.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

triage
Package

v4.x

Description

NuxtUI has made it very easy to integrate with Nuxt I18n. Still, there was one pain point that require user to handle: Mapping between NuxtUI's locales and Nuxt I18n's locales

Both packages define different types to represent locale object:

So there need to be a mapping step to set NuxtUI's locale to Nuxt I18n's. Typically there are 2 places:

  • <UApp>: to set the app's locale itself, only the current Nuxt I18n locale need to be mapped
  • Language Switcher component, which NuxtUI very conveniently provided <ULocaleSelect>: this need a list of all configured locales

For a long while. NuxtUI recommend the following pattern:

import * as locales from '@nuxt/ui/locale'

For <UApp>:

<script setup lang="ts">
import * as locales from '@nuxt/ui/locale'

const { locale } = useI18n()
</script>

<template>
  <UApp :locale="locales[locale]">
    <!-- ... -->
  </UApp>
</template>

For <ULocaleSelect>:

<script setup lang="ts">
import * as locales from '@nuxt/ui/locale'

const { locale, setLocale } = useI18n()
</script>

<template>
  <ULocaleSelect
    v-model="locale"
    :locales="Object.values(locales)"
    @update:model-value="setLocale($event)"
  />
</template>

Perhaps people will make a nice little composable for this:

import * as nuxtUiLocales from '@nuxt/ui/locale'

export const useNuxtUiLocale = () => {
    const { 
        locale: nuxtI18nLocaleCode,
        locales: nuxtI18nLocaleObjects,
    } = useI18n()

    return {
        nuxtUiLocale: computed(() => nuxtUiLocales[i18nLocaleCode.value]),
        nuxtUiLocales: computed(() => nuxtI18nLocaleObjects.map((localeObj) => nuxtUiLocales[localeObj.code])),
    }
}

However, import * cannot be tree-shaken, the build would contains all of NuxtUI's locale file even if we only use 2 or 3, and thus born https://github.com/nuxt/ui/pull/7152. In a typical fashion of Nuxt ecosystem, improve the DX by doing the mapping at build time, thereby eliminating the need for hand-mapping AND only bundle the locale that is in-use

And yet, https://github.com/nuxt/ui/pull/7152 only address 1 of the 2 places where we need to map, <UApp>. The <ULocaleSelect> part is not addressed, we have 3 options:

  • Bundle everything via import *, which defeat the purpose of https://github.com/nuxt/ui/pull/7152, and also was discouraged as of said PR.
  • Maintain the in-use language list by hand: import { en, ja, de }, which go against the spirit of Nuxt (There are 2 places you need to maintain and remember to update, bad architecture), and of https://github.com/nuxt/ui/pull/7152 (the PR was made to improve that DX)
  • Turn off Typescript, put Nuxt I18n's LocaleObject<T> in (which expect Locale<M>) and hope that a future version dont break, as it only work because coincidentally both types is having code as a property.

None of these options are good. And all can be solved by just exposing the mapped locale list that was made in https://github.com/nuxt/ui/pull/7152

Additional context
Image
Lingua principale
TypeScript
Stelle
7.1k
Fork
1.2k
Merge medio
1g 2h
PR unite (30g)
147

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nuxt/ui

Tutte le issue di nuxt/ui

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.