Introduction: This code is designed to streamline the management of language semantics and facilitate language switching within a repository of .i18n
files. It achieves this by merging the content of language files such as language/en.json and language/en.json and storing the merged output. When you modify a value in a language file, those changes are automatically reflected. Likewise, if you remove a key from the .i18n
file, it will be automatically deleted from all language outputs. Similarly, if you add a new key, it will be consistently added to all language files. The primary goal is to simplify the process of handling language variations and maintain consistency in language semantics.
- Support for multiple language files.
- Support Template Directory.
- Unplugin Support - Vite, Rollup, Webpack, Nuxt, Esbuild.
- ESM Support.
pnpm i -D unplugin-i18n-merger
Vite
// vite.config.ts
import I18nMerger from 'unplugin-i18n-merger/vite'
export default defineConfig({
plugins: [
I18nMerger({ /* options */ }),
],
})
Example: playground/
Rollup
// rollup.config.js
import I18nMerger from 'unplugin-i18n-merger/rollup'
export default {
plugins: [
I18nMerger({ /* options */ }),
],
}
Webpack
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('unplugin-i18n-merger/webpack')({ /* options */ }),
]
}
Nuxt
// nuxt.config.js
export default defineNuxtConfig({
modules: [
['unplugin-i18n-merger/nuxt', { /* options */ }],
],
i18nMerger: {
/* options */
},
})
This module works for both Nuxt 2 and Nuxt Vite
Vue CLI
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-i18n-merger/webpack')({ /* options */ }),
],
},
}
esbuild
// esbuild.config.js
import { build } from 'esbuild'
import I18nMerger from 'unplugin-i18n-merger/esbuild'
build({
plugins: [I18nMerger()],
})
Name | Type | Default | Description |
---|---|---|---|
exportDir |
string |
language |
The directory where the merged language files are stored. |
templateDir |
string |
.i18n |
The directory where the language files are stored. |
languages |
string[] |
['en', 'tr'] |
The languages to be merged. |
sortObjectKeys |
boolean |
true |
Whether to sort the keys of the merged object. |
- To use this template, click the "Use this template" button above.
- Clone the repository to your local machine.
- Run
pnpm install
to install the dependencies. - Run
pnpm build
to build the bundle. - Run
pnpm start
to start the bundle. - Run
pnpm lint
to lint the code. (You can also runpnpm lint:fix
to fix the linting errors.) - Run
pnpm test
to run the tests. (You can also runpnpm test:watch
to run the tests in watch mode.) - Run
pnpm release
to bump the version. Terminal will ask you to select the version type. And then it will automatically commit and push the changes. GitHub Actions will automatically publish git tags. NPM local registry will automatically publish the package.
MIT License © 2023-PRESENT productdevbook