Skip to content

[QUESTION] How to use import.meta.glob to dynamically import translations #2218

Answered by Red-Asuka
Red-Asuka asked this question in Q&A
Discussion options

You must be logged in to vote

@Guito Thank you for your reply, I finally set it up this way and achieved the same effect.

/lang/en-US.ts

export default defineI18nLocale(() => {
  const importAllTranslations = import.meta.glob(
    '../lang/en-US/*.ts',
    {
      eager: true,
      import: 'default',
    },
  )
  const combinedTranslations = Object.values(importAllTranslations).reduce(
    (acc: any, cur: any) => Object.assign(acc, cur),
    {},
  )
  return combinedTranslations
})

i18n config

lazy: true,
langDir: 'lang/',
defaultLocale: 'en',
locales: [
    {
      code: 'en',
      name: 'English',
      iso: 'en',
      file: 'en-US.ts',
    },
]

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@Guito
Comment options

@Red-Asuka
Comment options

Answer selected by Red-Asuka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2194 on July 09, 2023 05:48.