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

i18n-json-generator

v1.2.0

Published

This is a tool that collects terms from javascript files that need to be internationalized according to specific rules. The collected terms are generated into json files in each language.

Downloads

11

Readme

i18n-json-generator

Specify javascript files (can be multiple, can be all js files in a directory, etc.), from these js files according to the specified internationalization conversion function, extract the need for internationalization in the function, generate json files, and according to the specified need to internationalize the language, generate the corresponding language json template

If you have written internationalization methods in your code, such as

// $t is the internationalization conversion function used to replace "HelloWorld" with each language
const helloWord = $t('HelloWorld')

Using this tool, you can extract the terms that need to be internationalized from all the '$t' methods in your code. The following rules are currently followed:

  • $t('HelloWorld') :The first parameter is extracted as the key and value of the language configuration file
  • $t('HelloWorld{0}', [variable1, variable2, ...]) :The first parameter is extracted as the key and value of the language configuration file
  • $t(['hi', '嗨']) :The first element of the first parameter array is extracted as the key of the language configuration file, and the second element as the corresponding value

The prerequisite for extracting the key above is that it must be a string. If the first parameter is a variable or a method, etc., it will not be extracted.

Based on the above rules, the default generated local term configuration json file is:

{
  "HelloWorld": "HelloWorld",
  "HelloWorld{0}": "HelloWorld{0}",
  "hi": "嗨"
}

If you configure a language that requires internationalization, such as English or German, you will also copy the above json file (one for each language), but the value values are empty and need to be translated and supplemented.

{
  "HelloWorld": "",
  "HelloWorld{0}": "",
  "hi": ""
}

Of course, the tool also provides their own custom configuration, allowing you to freely configure the extraction of the case.

Install

npm i i18n-json-generator -D

or

yarn add i18n-json-generator -D

Usage

Create a js file that contains the following code

// start.js
const { i18nJsonConfig } = require('i18n-json-generator');
i18nJsonConfig();

Then add npm script command to package.json, such as:

"scripts": {
    "createjson": "node start.js"
  }

If you just want to execute the tool's i18nJsonConfig method without any other logic, as in the example above. You can not create a new js file, directly in the package.json to add npm script command, such as:

"scripts": {
"i18n-json": "i18n-json-generator"
}

The command name can be customized, but the command script executes i18n-json-generator, which is built into the tool.

In any case that does not provide configuration items, the default is to find all the 'js' files in the' dist 'folder under the root directory of your project, and then extract the terms from these files to generate json files, the generated directory is in the' lang 'directory under the root directory of the project (if there is no new).

The default name of the local entry json is locale.json, which also generates a jsonn -- en-US.json by default

The default is to find the term to extract according to the $t function.

The example directory in this project is the example demo, which is equivalent to the user's project root directory. We can refer to the use of the situation.

Of course, the above default configuration can be customized

Setting

There are two ways to modify the default configuration

  1. CLI
  2. config file

CLI

Add 'npm script' to package.json, such as:

"scripts": {
    "createjson": "node start.js --lang=en,ja --dir=i18n --entry=dist/**/test2.js --handler='$lang' --localeName=zh-CN"
  }

The specific parameters are explained as follows:

lang : In addition to local json entries, you need to generate json files in which languages, separated by commas (,)
dir : The directory in which the generated json file is stored
entry : What rules are used to match the file that needs to extract the terms, in this case using glob matching rules
handler :  Internationalized conversion function name, i.e., from what function
localeName :  Generated local term json name

config file

Create a file named i18n-generator.config.js in the project root directory. The content is as follows:

module.exports = {
    lang: ['en-US'], // In addition to local json entries, you need to generate json files in which languages
    output: {
        dir: 'lang' // The directory in which the generated json file is stored
    },
    entry: 'dist/**/*.js', // What rules are used to match the file that needs to extract the terms, in this case using glob matching rules
    handler: {
        name: '$t' // Internationalized conversion function name, i.e., from what function
    },
    localeName: 'zh-CN' // Generated local term json name
};