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

vite-plugin-auto-i18n

v0.0.9

Published

vite automatic translation plugin based on Google translation api.

Downloads

86

Readme

vite-plugin-auto-i18n

Introduction

vite auto-translation plugin based on Google api, not intrusive to the source code.

Support

Vue2、Vue3、React

support language:langFile

demo project addressdemo

Specificities

  • Not intrusive to the source code(No more need to replace i18n in the source code).
  • Fully automatic translation.
  • Support js, ts, jsx, tsx and other types of files

Usage

Install

npm i vite-plugin-auto-i18n -D # yarn add vite-plugin-auto-i18n -D

Option

| 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/assetsUsed 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 indexUsed 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.

config

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'
            }
        }),
      ]
});

main.js

import './lang' // The first line of the entry file introduces the automatic translation configuration file

lang file

Example

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')

Explanation

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 }})