Comments (18)
Thanks for report, I got it. Now is fixed.
from bot-brother.
I update to v1.0.31 and i still have the issue.
bot.texts(texts.en, { locale: 'en' });
change my string into a function.
So when I do something like return ctx.sendMessage('start.language_selected');
. It doesn't work because it's a function
from bot-brother.
Iarfadin, Please, try again in v1.0.32
from bot-brother.
thanks it works !
from bot-brother.
Sorry, I have a question. How to use translation in keyboard? because when I do return ctx.sendMessage('start.language_selected');
it send 'start.language_selected' instead of 'English is selected.', so I have to use: ctx.sendMessage(texts[ctx.session.locale].start.language_selected);
and then it works. But I can't use it in keyboard. how do I do something like this?
.keyboard([
[{ texts[ctx.session.locale].country.england: 'en' }],
[{ texts[ctx.session.locale].country.england: 'es' }]
])
from bot-brother.
You should set locale via ctx.setLocale(ctx.session.locale)
and then use localization keys:
bot.use('before', function (ctx) {
ctx.setLocale(ctx.session.locale || 'en');
})
.texts({
country: {
espanol: 'Espanol 1',
england: 'England 1'
}
}, {locale: 'en'})
.texts({
country: {
espanol: 'Espanol 2',
england: 'England 2'
}
}, {locale: 'es'});
bot.command('mycommand').keyboard([
'country.england': 'en'
'country.espanol': 'es'
])
.invoke(function (ctx) {...})
.answer(function (ctx) {...})
from bot-brother.
.use('before', function(ctx) {
console.log(ctx.session); // {}
ctx.setLocale(ctx.session.locale || 'en');
console.log(ctx.session); // {}
})
doesn't work
.use('before', function(ctx) {
console.log(ctx.session); // {}
ctx.session.locale = ctx.session.locale || 'en';
console.log(ctx.session); // { locale: 'en' }
})
from bot-brother.
could you please provide more detailed sources
from bot-brother.
var token = 'token';
var bb = require('bot-brother');
var texts = [];
texts.es = require('./locales/es.js');
texts.en = require('./locales/en.js');
var bot = bb({
key: token,
redis: {
port: 6379,
host: '127.0.0.1'
}
})
.texts(texts.en, { locale: 'en' })
.texts(texts.es, { locale: 'es' })
.use('before', function(ctx) {
console.log(ctx.session);
ctx.setLocale(ctx.session.locale || 'en'); // doesn't work
ctx.session.locale = ctx.session.locale || 'en'; // work
console.log(ctx.session);
});
bot.command('start')
.invoke(function(ctx){
return ctx.sendMessage('Please select your language');
})
.keyboard([
[{ ':es: Spanish': 'es' }],
[{ ':us: English': 'en' }]
])
.answer(function(ctx){
ctx.session.locale = ctx.answer;
var text = texts[ctx.session.locale].start.language_selected; // I did this because because it wasn't working with 'start.language_selected'
return ctx.sendMessage(text).then(function(){
ctx.go('set_country');
});
});
bot.command('set_country')
.invoke(function(ctx) {
var text = texts[ctx.session.locale].start.choose_your_country;
return ctx.sendMessage(text);
})
.keyboard([
[{ 'keyboard.choose_country.england': 'en' }], // doesn't work I try to replace by ''texts[ctx.session.locale].country.england: 'en' "
[{ 'keyboard.choose_country.spain': 'es' }]
])
.answer(function(ctx) {
console.log(ctx.answer);
return ctx.sendMessage('hi');
});
from bot-brother.
module.exports = {
start: {
"language_selected": "English is selected.",
"choose_your_country": "Now choose your country"
},
keyboard: {
choose_country: {
england: ":en: england",
spain: ":es: Spain"
}
}
};
My /locales/en.js file
from bot-brother.
Why texts
variable is an array?
from bot-brother.
it does not work if I do just
texts.fr = require('./locales/fr.js');
texts.en = require('./locales/en.js');
from bot-brother.
I just tried your code and everything works fine.
Try to force set locale to en
and inspect variable ctx.session.locale, maybe you have bad values inside.
ctx.session.locale = 'en'
ctx.setLocale(ctx.session.locale)
from bot-brother.
can you choose country with my code?
from bot-brother.
Yes, I can. Here is some modified peace of your code:
var bb = require('bot-brother');
var texts = [];
texts.es = require('./locales/es.js');
texts.en = require('./locales/en.js');
var bot = bb({
key: token,
redis: {
port: 6379,
host: '127.0.0.1'
}
})
.texts(texts.en, { locale: 'en' })
.texts(texts.es, { locale: 'es' })
.use('before', function(ctx) {
ctx.setLocale(ctx.session.locale || 'en'); // doesn't work
});
bot.command('start')
.invoke(function(ctx){
return ctx.sendMessage('Please select your language');
})
.keyboard([
[{ 'keyboard.choose_country.england': 'en' }],
[{ 'keyboard.choose_country.spain': 'es' }]
])
.answer(function(ctx){
console.log(ctx.answer)
ctx.session.locale = ctx.answer;
ctx.setLocale(ctx.answer);
return ctx.sendMessage('start.language_selected');
});
locales/es.js
module.exports = {
start: {
"language_selected": "English is selected. SPAIN",
"choose_your_country": "Now choose your country. SPAIN"
},
keyboard: {
choose_country: {
england: ":us: england SPAIN",
spain: ":es: Spain SPAIN"
}
}
};
locales/en.js
module.exports = {
start: {
"language_selected": "English is selected.",
"choose_your_country": "Now choose your country"
},
keyboard: {
choose_country: {
england: ":us: england",
spain: ":es: Spain"
}
}
};
Also I saw some stranges in your code
.keyboard([
[{ 'keyboard.choose_country.england': 'en' }], // doesn't work I try to replace by ''texts[ctx.session.locale].country.england: 'en' "
[{ 'keyboard.choose_country.spain': 'es' }]
])
Why england is 'es' and spain is 'en'? :)
from bot-brother.
Thanks a lot for your help. I rewrote everything based on delorean_bot example and using yaml instead of json. Everything work fine but I have still stranged issue with ctx.setLocale();
(but it works !).
I still have some question. Is it possible to add a translation into ctx.data ? something like this:
ctx.data.country = 'country.en'
. Because I need to send something like this 'your country is <%=country%>'
from bot-brother.
You can use following yaml-localization for your purposes:
en.yaml
message: 'your country <%=render(country)%>'
country:
en: 'England'
es: 'Spain'
Where ctx.data.country
can be 'country.en'
or 'country.es'
from bot-brother.
Thanks !
from bot-brother.
Related Issues (20)
- How to handle all rejections? HOT 1
- Inline keyboard doesn't work HOT 2
- Add support for non-command matches? HOT 1
- How do I get Photo or Document send by User?
- webhook res: undefined
- add support for inline_query HOT 2
- Multiple Answer HOT 1
- Use template-literal instead of EJS
- A couple typos that could be corrected. HOT 1
- How to request phone number? HOT 1
- Custom redis options HOT 1
- Use without commands? Retrieve ctx on bot.api.on('message')
- Dependance "botanio" outdated and service down
- something wrong with timeout
- Typescript support ?
- 2.0 API HOT 2
- How to reset a state? HOT 1
- BotBrother and asynchronous db HOT 1
- Why do we use ctx.data for message variables? HOT 1
- Do I _have to_ send a message before reading for user input?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bot-brother.