
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
vite-plugin-auto-i18n
Advanced tools
vite automatic translation plugin based on Google translation api.
vite auto-translation plugin based on Google api, not intrusive to the source code.
Vue2、Vue3、React
support language:langFile
demo project address:demo
js, ts, jsx, tsx
and other types of filesnpm i vite-plugin-auto-i18n -D # yarn add vite-plugin-auto-i18n -D
parameter | typology | required field | default value | descriptions |
---|---|---|---|---|
translateKey | string | ❌ | $t | Default function for switching languages after plugin conversion |
excludedCall | string[] | ❌ | ["$i8n", "require", "$$i8n", "console.log", "$t"] | Marking does not translate the calling function |
excludedPattern | RegExp[] | ❌ | [/\.\w+$/] | Marking strings without translation |
excludedPath | RegExp[] | ❌ | [] | Specify a directory of files that do not need to be translated |
includePath | RegExp[] | ❌ | [/src\//] | Specify the directory of the files to be translated |
globalPath | string | ❌ | ./lang | Translation profile generation location |
distPath | string | ✅ | '' | The location of the generated files after packaging e.g. . /dist/assets ( Used to inject translation configurations into packaged files ) |
distKey | string | ✅ | '' | The name of the main file of the generated file after packaging, e.g. index.xxx Default is index ( Used to inject translation configurations into packaged files ) |
namespace | string | ✅ | '' | Distinguish translation configurations between current projects online |
originLang | string | ❌ | 'zh-cn' | source language(Translations into other languages based on that language) |
targetLangList | string[] | ❌ | ['en'] | target language(The type of language that the original language will be translated into, passed into an array to support multiple languages at once) support target language(langFile) |
buildToDist | Boolean | ❌ | false | Whether to package the translation configuration into the main package.() |
why need buildToDist?
After executing the plugin in the vite environment, the translation configuration file is just generated. If you directly build it, the project will generate the translation configuration file. However, the translation configuration file will not be packaged into the main package immediately, and you may need to package it a second time. Therefore, the buildToDist option is provided, and when the translation configuration file is created, it will be actively set to the main package, The flaw is that your packaged file may have two sets of translation configuration files.
How to update translations?
After executing the plugin, two files will be generated under globalPath,
index.js and index.json , index.js generates the relevant translation functions, while index.json is the json file that stores all the translation sources, if you want to update the translation content, you can directly update this json file.
import vitePluginAutoI18n from "../vitePluginAutoI18n/src/index";
import createVuePlugin from '@vitejs/plugin-vue';
const vuePlugin = createVuePlugin({
include: [/\.vue$/],
// Note that this configuration must be added to prevent the plugin from creating static nodes when parsing and not getting the text in the nodes
template: {
compilerOptions: {
hoistStatic: false,
cacheHandlers: false,
}
}
})
export default defineConfig({
plugins: [
vuePlugin,
vitePluginAutoI18n({
option:{
globalPath: './lang',
namespace: 'lang',
distPath: './dist/assets',
distKey: 'index'
}
}),
]
});
import './lang' // The first line of the entry file introduces the automatic translation configuration file
import '../../lang/index'
import langJSON from '../../lang/index.json
const langMap = {
en: window?.lang?.en || _getJSONKey('en', langJSON),
zhcn: window?.lang?.zhcn || _getJSONKey('zhcn', langJSON)
}
const lang = window.localStorage.getItem('lang') || 'zhcn'
window.$t.locale(langMap[lang], 'lang')
import '../../{{ your globalPath }}/index' // Import translation base function
import langJSON from '../../{{ your globalPath }}/index.json' // Import translation target JSON
const langMap = {
{{ your originLangKey }}: window?.{{ your namespace }}?.{{ your originLangKey } || _getJSONKey('zhcn', langJSON)
{{ your targetLangList[0] }}: window?.{{ your namespace }}?.{{ your targetLangList[0] } || _getJSONKey('en', langJSON),
}
// window.localStorage.getItem('lang') Storing the current language type
const lang = window.localStorage.getItem('lang') || {{ your originLangKey }}(defualt lang)
window.{{ your translateKey }}.locale(langMap[lang], {{ your namespace }})
FAQs
vite automatic translation plugin based on Google translation api.
The npm package vite-plugin-auto-i18n receives a total of 229 weekly downloads. As such, vite-plugin-auto-i18n popularity was classified as not popular.
We found that vite-plugin-auto-i18n demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.