mbt_nodejs_i18n v1.0.3
MBT_Nodejs_i18n
A lightweight Nodejs i18n plug-in developed by the MBT_TLT team
Light
- Lightweight, this core of plug-in only about 18 kb
- supports interpolated text i18n
- You can actively reset i18n objects while the project running
- Global CLI command to quickly build the i18n base file
Dev Log
- v1.0.0
- Complete mbt_nodejs_i18n core development
- v1.0.1
- Update related test code
- v1.0.2(deprecate)
- Update CLI related features
- v1.0.3
- Fixed a fatal bug that caused infinite callbacks by calling its own i18n in a path other than the package ontology and where no associated i18n file existed
- New i18n built-in library design, currently only used to supply mbt_nodejs_i18n itself to prevent the occurrence of infinite callback exception use, future version consider to provide some common statement translation
- i18n function added isUseBuildInLibrary Optional argument, which defaults to false, to specify whether the current statement uses the i18n built-in library or the project i18n library
- i18nInit function added the i18nTable optional parameter, which defaults to the project i18n library object and specifies which i18n library object to initialize or reset
- Added the getI18nTable function to get the virtual table object of which i18n library
- The i18nTableKey optional(string) argument is provided. Currently, only 'default' and 'build-in' are provided. The default is 'default', which means to get the virtual table object of the project i18n library
- Forward compatible updates that do not affect original usage
Feature
- Compatible with CommonJS
- Optimize the i18nInit logic to load and parse only the i18n files that make the fix
Install
# use all functions of MBT_Nodejs_i18n
npm install -g mbt_nodejs_i18n
# just use core functions of MBT_Nodejs_i18n
npm install -s mbt_nodejs_i18n
CLI Commands
When you install mbt_nodejs_i18n globally, you will be able to run the given instructions as mbt-i18n for CLI Controller:
1、create - Create i18n basic files
mbt-i18n create [folderPath] [languageArr...]
# folderPath i18n filePath (default: `${ path.resolve('./') }`)
# languageArr An array of target language flags to be translated (default: [`${ osLocaleSync() || 'en-US' }`]) osLocaleSync() is mean get os language
This directive will create an i18n folder and all i18n json files given by languageArr under the given folderPath (default is the current path).
If the folderPath tail path node is i18n, the i18n json file will be created directly under that path.
The command will also run the following child commands:
- npm init -y
Execute this subinstruction only if package.json does not exist in a given path
- npm install -s mbt_nodejs_i18n
Add mbt_nodejs_i18n to the running dependency of the project under the given path
Example:
# 1. Create an i18n folder and related default i18n files under the current path (ignore if they exist)
# 2. Choose to execute 'npm init -y' based on whether there is a package.json file in the current path
# 3. Run 'npm install -s mbt_nodejs_i18n' to introduce the latest package of mbt_nodejs_i18n
mbt-i18n create
# 1. Create an i18n folder and related default i18n files under the 'F:/exampleI18n' folder (ignore if they exist)
# As above...
mbt-i18n create F:/exampleI18n
# 1. Create i18n folders and 'zh-CN.json' and 'en-US.json' i18n files under the 'F:/exampleI18n' folder (ignore if they exist)
# As above...
mbt-i18n create F:/exampleI18n zh-CN en-US
Usage
0. import
You can choose to import and use MBT_Nodejs_i18n using the following methods
1.For ES6
import i18n from 'mbt_nodejs_i18n'
or
import { i18n } from 'mbt_nodejs_i18n'
2.For NodeJS
we're sorry, because MBT_Nodejs_i18n is developed using the ES module specification, and there is no compatibility with CommonJS for the time being, So to use the plug-in, you need to name the JavaScript file suffix that imported the component .ejs, and then import and use it in the same way as ES6。
Or you can change your NodeJS to the ES module specification by adding the following configuration code to the package.json root node of your NodeJS project note: if your project was originally developed according to the CommonJS specification, this will lead to compatibility problems with your project!
"type": "module",
MBT_Nodejs_i18n provides two exposure methods, which are:
i18n
- i18n Corei18nInit
- i18n Initialization function, pass in a path string that stores the i18n file of your project. If the i18n file is stored in the i18n folder under the project root directory, there is no need to call this function actively.
1. Initialization
MBT_Nodejs_i18n provides the i18nInit function, which is used to initialize or reset the i18n data information object for the plug-in, which is documented as:
/**
* i18nInit - Reinitialization is supported, the reinitialization content will overwrite the original i18nObj and i18nLanguageArr
*
* @export
* @const
* @function
* @param { string } [i18nDirPath = path.resolve('./', 'i18n')] - i18n folder path
* */
If the i18n related files of your project are stored in the i18n folder under the root of your project, there is no need to call this function actively. MBT_Nodejs_i18n has done everything for you. 😎
If your i18n-related files are not stored in the i18n folder in the root directory of your project, you need to actively call the i18nInit function before using i18n and pass in the storage path of your project i18n files, like this:
// Suppose the i18n file is stored in the path 'D://i18n'
import { i18nInit } from 'mbt_nodejs_i18n';
// you code ...
i18nInit('D://i18n');
// you code ...
2. Simple text
For simple text, you can simply give the key of the text
Suppose the i18n related files like this:
{
"test": "this is a text"
}
Example:
const a = i18n('test')
// will show "this is a text" in your console
console.log(a);
3. Interpolated text
For some complex scenes that need to be interpolated in the text, you only need to give an additional replacement object replaceObj on the basis of the key of the text, and it should be noted that a given replaceObj must contain the same replaceKey index as text interpolation
Suppose the i18n related files like this:
{
"test": "this is a ${ a }"
}
Example:
const b = i18n('test', {
a: 'text'
});
// will show "this is a text" in your console
console.log(b);
4. Specify i18n language
Sometimes, we need to specify that the text output is in an i18n language, and all we need to do is to give its language identity.
Suppose there are two i18n related files, en-US.json and zh-CN.json like this:
{
"test": "this is a ${ a }"
}
{
"test": "这是一段${ a }"
}
Example:
const c = i18n('test', {
a: '文本'
}, 'zh-CN');
// will show "这是一段文本" in your console
console.log(c);
5. Specifies whether to use the i18n built-in library
Since v1.0.2.1, mbt_nodejs_i18n has added the i18n built-in library design. You can use some of the i18n translations provided by mbt_nodejs_i18n
Assume that mbt_nodejs_i18n The i18n built-in library (that is, mbt_nodejs_i18n/i18n) has two i18N-related files, en-US.json and zh-CN.json:
{
"test": "this is a ${ a }"
}
{
"test": "这是一段${ a }"
}
Example:
const d = i18n('test', {
a: '文本'
}, 'zh-CN', true);
const e = i18n('test', {
a: '文本'
}, 'en-US', true);
// will show "这是一段文本 this is a 文本" in your console
console.log(d, e);
6. Reset i18n object
In addition to the initialization function mentioned above, the i18nInit function provided by MBT_Nodejs_i18n can also be used to reset i18n objects at any time.
Application scenarios:
- The i18n file in your i18n folder will change while the project running
- Your i18n folder will dynamically add or delete some i18n files while the project running
After the above scenario occurs, you can actively call the i18nInit function to reset the i18n object.
If you find any exception in use, please submit the Issues as soon as possible and describe in detail the conditions and usage scenarios of the exception. We will check and fix the relevant exception as soon as possible.
If you think this plug-in is great, please give this project a Star. Everyone's support is the biggest driving force for our progress.😉