multiple_language
Version:
Multiple language by Tùng Duy
98 lines (67 loc) • 2.98 kB
Markdown
# Multi Language
[](https://badge.fury.io/js/multiple_language) [](https://badge.fury.io/nu/multiple_language)
<p align="center">
<img width="460" height="300" src="https://dev.fpt.work/images/FTI/15c12a77-2b45-4057-bb0a-aa07fb1d8a29.svg">
</p>
---
## Installation
Install with [npm](https://www.npmjs.com/package/multiple_language)
```sh
# Install package
npm i multiple_language
```
## Import module
```js
const express = require('express');
const app = express();
...
const { multiLanguageBe } = require("multiple_language");
multiLanguageBe(app);
...
```
## Add translation files
```sh
cd folder_root
mkdir locales
cd locales
mkdir vi && mkdir en
touch vi/file_name.json && touch en/file_name.json
```
##### Note
- The directory name must be `locales`.
- Locales's subdirectory is abbreviated name
---
#### API Documentation
`Multi Language` exposes the following api.
`multiLanguageBe(app, optional)`
The exported multiLanguageBe function takes two parameters. The first parameter is the app object of Express. The second parameter is an optional and It is not required.
`optional` is the root path to the directory containing the compiled files.
By default if option is empty the package will find a directory called `locales`. This directory is on par with the `index.js` or `server.js`.
If you want to change the path you can change the new path for this parameter.
If you use the `Content-Language` variable to store the language value in the request's header. You can use `setLanguageBe` function as a middleware to automatically set the language for your other request.
The exported `setLanguageBe` function gets its value fro the default variable `Content-Language`.
If `setLanguageBe` function is used, then `Content-Language` variable is indispensable in each request.
<img src="https://dev.fpt.work/images/5f462dc2c93e380007768cef/5f462dc291324d0007016f36/content-language.png">
##### Example
```js
const express = require('express');
const app = express();
...
const { multiLanguageBe, setLanguageBe } = require("multiple_language");
const pathName = __dirname + '/folder'
multiLanguageBe(app, pathName);
app.use(setLanguageBe); // Get the value from variable "Content-Language" in the request's header
...
```
## Authors and acknowledgment
- Project manager: `tungdd5@fpt.com.vn`
## Support
Reach out to me at one of the following places!
- Website at [Tùng Duy](https://www.facebook.com/tungduy.dev)
- Email: tungdd5@fpt.com.vn
- Phone: 0358001357
## License
Copyright © 2020 FPT VPN. Crafted with :heart: by [Tùng Duy](https://www.facebook.com/tungduy.dev) - tungdd5@fpt.com.vn.
Released under the [MIT License](LICENSE).
---
This file was generated by [Tùng Duy](https://github.com/tung-duy/fvpn-module/blob/master/README.md), v1.1.0, on May 10, 2020.