GithubHelp home page GithubHelp logo

Comments (18)

SerjoPepper avatar SerjoPepper commented on July 2, 2024

Thanks for report, I got it. Now is fixed.

from bot-brother.

larfadin avatar larfadin commented on July 2, 2024

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.

SerjoPepper avatar SerjoPepper commented on July 2, 2024

Iarfadin, Please, try again in v1.0.32

from bot-brother.

larfadin avatar larfadin commented on July 2, 2024

thanks it works !

from bot-brother.

larfadin avatar larfadin commented on July 2, 2024

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.

SerjoPepper avatar SerjoPepper commented on July 2, 2024

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.

larfadin avatar larfadin commented on July 2, 2024
.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.

SerjoPepper avatar SerjoPepper commented on July 2, 2024

could you please provide more detailed sources

from bot-brother.

larfadin avatar larfadin commented on July 2, 2024
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.

larfadin avatar larfadin commented on July 2, 2024
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.

SerjoPepper avatar SerjoPepper commented on July 2, 2024

Why texts variable is an array?

from bot-brother.

larfadin avatar larfadin commented on July 2, 2024

it does not work if I do just

texts.fr = require('./locales/fr.js');
texts.en = require('./locales/en.js');

from bot-brother.

SerjoPepper avatar SerjoPepper commented on July 2, 2024

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.

larfadin avatar larfadin commented on July 2, 2024

can you choose country with my code?

from bot-brother.

SerjoPepper avatar SerjoPepper commented on July 2, 2024

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.

larfadin avatar larfadin commented on July 2, 2024

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.

SerjoPepper avatar SerjoPepper commented on July 2, 2024

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.

larfadin avatar larfadin commented on July 2, 2024

Thanks !

from bot-brother.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.