2.2.2 • Published 4 months ago

vite-plugin-i18n-detector v2.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

English | 中文

A vite plugin for lazy loading i18n resources

NOTE: This plugin is independent of the language framework. Whether you use React or Vue (or any other language), as long as it is vite, you can implement lazy loading of internationalization resources based on this plugin

Features

  • Seamless development experience, no need to manually import resource files
  • Lazy loading language resource files to reduce the size of the first screen resource
  • Read the configuration items of i18n-ally by default, no additional configuration is required

Install

pnpm add vite-plugin-i18n-detector -D

Online Demo

Demo

Options

If i18n.ally is configured, the plugin will read the configuration by default

OptionTypeDefaultDescription
localesPathsstring[]i18n-ally.localesPaths \|\| ['./src/locales', './locales']The directory of language resources, relative to root
rootstringprocess.cwd()The project root path
namespacebooleani18n-ally.namespace \|\| falseEnable namespace
pathMatcherstringauto detected by structureResource file matching rule
parserPluginsParserPlugin[][jsonParser, json5Parser, yamlParser]Resource file parsing plugin
autoDetectI18nConfigboolean \| { stopAt: string }trueWhether to automatically detect i18n-ally configuration, if stopAt is passed in, it will stop detecting in the specified directory

Config Reference

vite.config.ts

import path from 'node:path'
import { defineConfig } from 'vite'
import { i18nDetector } from 'vite-plugin-i18n-detector'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    i18nDetector({
      localesPaths: ['./src/locales'],
    }),
  ],
})

Use with React-i18next

main.tsx

import i18next from 'i18next'
import LanguageDetector from 'i18next-browser-languagedetector'
import React from 'react'
import ReactDOM from 'react-dom/client'
import { initReactI18next } from 'react-i18next'
import { setupI18n } from 'vite-plugin-i18n-detector/client'
import App from './App'

const root = ReactDOM.createRoot(document.querySelector('#root') as HTMLElement)

const lookupTarget = 'lang'
const fallbackLng = 'en'

i18next
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    resources: {}, // !!! important: No resources are added at initialization, otherwise what's lazy loading :)
    nsSeparator: '.',
    fallbackLng,
    detection: {
      order: ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator'],
      caches: ['localStorage', 'sessionStorage', 'cookie'],
      lookupQuerystring: lookupTarget,
      // ... For more configurations, please refer to `i18next-browser-languagedetector`
    },
  })


const { loadResourceByLang } = setupI18n({
  language: i18next.language,
  onInited() {
    root.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>,
    )
  },
  onResourceLoaded: (langs, currentLang) => { // Once the resource is loaded, add it to i18next
    Object.keys(langs).forEach((ns) => {
      i18next.addResourceBundle(currentLang, ns, langs[ns])
    })
  },
  fallbackLng,
  cache: {
    querystring: lookupTarget,
  },
})

const _changeLanguage = i18next.changeLanguage
i18next.changeLanguage = async (lang: string, ...args) => {
  // Load resources before language change
  await loadResourceByLang(lang)
  return _changeLanguage(lang, ...args)
}

Full Example

Please refer to i18next example

vscode i18n-ally configuration reference

.vscode => settings.json

{
  "i18n-ally.localesPaths": ["src/locales"],
  "i18n-ally.keystyle": "nested",
  "i18n-ally.enabledParsers": ["json"],
  "i18n-ally.enabledFrameworks": ["react", "i18next"],
  "i18n-ally.namespace": true,
  "i18n-ally.pathMatcher": "{locale}/{namespaces}.{ext}",
}

⚠️ Warm Tips

Built-in support for json / json5 / yaml / yml resource files, customizable plugin parsing language

Thanks

License

MIT

2.2.2

4 months ago

2.2.1

4 months ago

2.2.0

5 months ago

2.1.2

5 months ago

2.1.1

5 months ago

2.1.0

5 months ago

1.0.3

7 months ago

2.0.1

7 months ago

2.0.0

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago

0.0.10

10 months ago

0.0.9

10 months ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago