https://github.com/node-vk-bot-api/node-vk-bot-api-i18n
🇪🇺 I18n middleware for node-vk-bot-api.
https://github.com/node-vk-bot-api/node-vk-bot-api-i18n
Last synced: 10 months ago
JSON representation
🇪🇺 I18n middleware for node-vk-bot-api.
- Host: GitHub
- URL: https://github.com/node-vk-bot-api/node-vk-bot-api-i18n
- Owner: node-vk-bot-api
- Created: 2018-08-04T18:13:45.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-10T14:07:55.000Z (about 7 years ago)
- Last Synced: 2025-04-15T02:57:37.991Z (10 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/node-vk-bot-api-i18n/
- Size: 23.4 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/node-vk-bot-api-i18n/)
[](http://standardjs.com/)
# node-vk-bot-api-i18n
🇪🇺 I18n middleware for node-vk-bot-api.
## Install
```sh
$ npm i node-vk-bot-api-i18n -S
```
## Tests
Before you must set `TOKEN` and `GROUP_ID` in `process.env`.
```sh
$ npm test
```
## Usage
```js
const VkBot = require('node-vk-bot-api')
const Session = require('node-vk-bot-api/lib/session')
const I18n = require('node-vk-bot-api-i18n')
const path = require('path')
const bot = new VkBot({
token: process.env.TOKEN,
group_id: process.env.GROUP_ID
})
const session = new Session()
const i18n = new I18n({
locales: ['en', 'ru'],
defaultLocale: 'en',
directory: path.join(__dirname, 'locales')
})
bot.use(session.middleware())
bot.use(i18n.middleware())
bot.command('/start', (ctx) => {
ctx.reply(ctx.i18n.__('start', {
name: 'Mikhail'
}))
})
bot.startPolling()
```
## API
### Options
* `locales`: Languages list
* `defaultLocale`: Default language
* `directory`: Absolute path to locales directory
### Methods
* `ctx.i18n.locale=`: Setter for locale
* `ctx.i18n.locale`: Getter for locale
* `ctx.i18n.__(key, [variables])`: Generate message from template
### Locale file example
All files must be store as json.
```js
{
"start": "Hello, %name%!",
"errors": {
"userNotFound": "User not found."
}
}
```
## License
MIT.