npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dvcol/vite-plugin-i18n

v1.1.3

Published

Vite plugin to generate merged i18n locales

Downloads

22

Readme

vite-plugin-i18n

vite-plugin-i18n is a vite plugin to generate static json locale files, either in a virtual object or in a dist folder.

It parses all json i18n files in a given folder and merge them by language scope.

Files need to conform to the following structure to be properly parsed:

src
├── ...
├── root
│   ├── locale.en.json
│   ├── locale.es.json
│   ├── locale.fr.json
│   └── ...
│   └── models
│       ├── models.en.json
│       ├── models.es.json
│       ├── models.fr.json
│       └── ...
└── ...

Where src/root is the given root folder containing files to be parsed.

Usage

Install dev dependency :

yarn add vite-plugin-i18n -D
npm install vite-plugin-i18n --save-dev
pnpm install vite-plugin-i18n --save-dev

Add the plugin to your vite.config.ts:

// vite.config.ts
import { viteI18nPlugin } from "./vite-plugin-i18n";

export default {
  plugins: [
    viteI18nPlugin({
      path: "src/i18n",
      out: "dist/locale",
    }),
  ],
};

Use virtual module :

import { locales, watchLocales } from 'virtual:vite-plugin-i18n';

console.info('My locales at runtime', locales);

watchLocales((data: Locale) => console.info('My locales on hot reload', data));

Use hot module reload :

if (import.meta.hot) {
  import.meta.hot.on('virtual:vite-plugin-i18n', ({ data }) => {
    console.info(data) // new locale
  })
}

API

options

  • Type: Object

options.path

  • Type: string
  • Default: undefined

The path where the plugin will attempt to parse json formatted files into unified locales.

options.out

  • Type: boolean | string | { dir: string; name?: string } | (locale: string, messages: RecursiveRecord<string>) => string
  • Default: undefined

An string, option object or resolver function to compute the filepath and filename of generated locales.

By default, the plugin will not write files and only generate a virtual module (see vite virtual modules)

If output is enabled the files will be generated with the following structure:

dist
├── ...
├── locales
│   ├── en.json
│   ├── es.json
│   ├── fr.json
│   └── ...
└── ...

You can provide a custom folder path, file name, or file name resolver like this:

viteI18nPlugin({
  path: "src/i18n",
  out: "dist/locales",
});
 viteI18nPlugin({
  path: "src/i18n",
  out: {
    dir: 'dist/_locales'
    name: 'my-custom-name'
  },
})
viteI18nPlugin({
  path: "src/i18n",
  out: (locale, messages) => `prefix-${locale}-${messages["suffixe"]}.json`,
});

License

MIT