New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vite-plugin-i18n-ally

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-i18n-ally

vite plugin load i18n resources lazily. 懒加载国际化资源,适配vscode-i18n-ally

  • 5.2.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Vite logo i18n logo


vite-plugin-i18n-ally

npm package

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
  • Support vite hmr out of the box

Install

pnpm add vite-plugin-i18n-ally -D

Online Demo

Demo

Plugin Options

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

OptionTypeDefaultDescription
localesPathsstring[]i18n-ally.localesPathsThe 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[]Built-in pluginsResource file parsing plugin
useVscodeI18nAllyConfigboolean | { stopAt: string }trueWhether to automatically use 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 { i18nAlly } from 'vite-plugin-i18n-ally'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [i18nAlly()],
})

Client Options

OptionTypeDescription
languagestringThe current language
namespacesstring[]Initial namespaces
onInitedFunctionCallback after initialization
onResourceLoadedFunctionCallback after resource loaded
fallbackLngstringFallback language
detectionArrayLanguage detection and cache, like i18next-browser-languagedetector

Use with i18next

main.tsx

import React from 'react'
import { initReactI18next } from 'react-i18next'
import i18next from 'i18next'
import ReactDOM from 'react-dom/client'
import { i18nAlly } from 'vite-plugin-i18n-ally/client'
import App from './App'

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

const { asyncLoadResource } = i18nAlly({
  onInit() {
    i18next.use(initReactI18next).init({
      resources: {}, // !!! important: No resources are added at initialization, otherwise what's lazy loading :)
      nsSeparator: '.',
      keySeparator: '.',
      fallbackLng: 'en',
    })
  },
  onInited() {
    root.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>,
    )
  },
  onResourceLoaded: (resource, currentLang) => {
    // Once the resource is loaded, add it to i18next
    Object.keys(resources).forEach((ns) => {
      i18next.addResourceBundle(language, ns, resources[ns])
    })
  },
  fallbackLng: 'en',
  detection: [
    {
      detect: 'querystring',
      lookup: 'lang',
    },
    {
      detect: 'cookie',
      lookup: 'cookie-name',
    },
    {
      detect: 'htmlTag',
    },
  ],
})

const i18nextChangeLanguage = i18next.changeLanguage
i18next.changeLanguage = async (lang: string, ...args) => {
  // Load resources before language change
  await asyncLoadResource(lang)
  return i18nextChangeLanguage(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.pathMatcher": "{locale}/{namespaces}.{ext}",
  "i18n-ally.namespace": true // If you use `namespace` above, you need to configure
}

⚠️ Warm Tips

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

Thanks

License

MIT

Keywords

FAQs

Package last updated on 31 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc