I have not found documentation about it:
If you need a BOT that reply in the language of the users you can use:
https://github.com/yagop/node-telegram-bot-api, with
https://github.com/mashpie/i18n-node
Question
But Telegram for some user return language code it and fot other it-IT.
Example:
How can i fix it?
process.env.NTBA_FIX_319 = 1; // fix cancellation of promises https://github.com/yagop/node-telegram-bot-api/issues/319. module.js:652:30
const TelegramBot = require('node-telegram-bot-api');
const token = 'XXXXXXXXX'; // Token from @BotFather
const bot = new TelegramBot(token, {polling: true}); // Create a bot that uses 'polling' to fetch new updates
const botName = "yourBOTName"
const botVer = "1.1.0"
var i18n = require('./node_modules/i18n'),
app;
i18n.configure({
locales: ['en-EN', 'it-IT'],
directory: __dirname + '/locales',
autoReload: true,
fallbacks:{'nl-NL': 'en-EN'},
defaultLocale: 'en-EN'
});
// Test Language
bot.onText(/\/test_i18n/, (msg) => {
i18n.setLocale(msg.from.language_code);
var greeting = i18n.__('Hello {{name}}, how are you today?', { name: msg.from.first_name });
bot.sendMessage(msg.chat.id, greeting, {
parse_mode: "HTML"
}
);
});
// Start Message i18n
bot.onText(/\/start_i18n/, (msg) => {
i18n.setLocale(msg.from.language_code);
var msg_start_1 = i18n.__("Welcome You {{name}}, \nI'm <b>Your BOT</b> and I'm here to help you to find ...", { name: msg.from.first_name });
var msg_start_2 = i18n.__("\n\nPlease type: \n<b>The name of ...</b>, or\n/list - To know the list of... \n/help - To have support\n/site - To visit our website");
bot.sendMessage(msg.chat.id, msg_start_1 + msg_start_2, {
parse_mode: "HTML"
}
);
});
If you have a problem only with it and it-IT, you can use simple if/else statement
var lang;
if(msg.from.language_code.length == 2) {
lang = msg.from.language_code + '-' + msg.from.language_code.toUpperCase();
} else {
lang = msg.from.language_code;
}
i18n.setLocale(lang);
This way lang will be always it-IT when you will receive it or it-IT
Thanks @jejopl,
I was trying a reverse solution :)
var str = msg.from.language_code.toLowerCase();
var code = str.substring(0, 2);
i18n.setLocale(code);
this always returns it (and i've changed the cfg of i18n)
No problem. Also, you should check at the beginning if msg.from.language_code is not undefined.
Consider that with custom client not always language_code is valid or exists.
@jejopl, @sidelux thank you for the tip!
i18n in configuration allows you to set a default language, so in case of an empty field or undefined it should show the language set.
But it is better to avoid errors in the logic and insert a check on the presence nad of the value of msg.from.language_code:
i18n.configure({
locales: ['en', 'it'],
directory: __dirname + '/locales',
autoReload: true,
fallbacks:{'nl': 'en'},
defaultLocale: 'en'
});
// Test Language
bot.onText(/\/test_i18n/, (msg) => {
if (msg.from.hasOwnProperty('language_code') && msg.from.language_code!=('undefined')) {
var str = msg.from.language_code.toLowerCase();
var code = str.substring(0, 2);
i18n.setLocale(code);
}
var greeting = i18n.__('Hello {{name}}, how are you today?', { name: msg.from.first_name });
bot.sendMessage(msg.chat.id, greeting, {
parse_mode: "HTML"
}
);
});
Question seems to have been resolved, closing it for now.
hello. How does telegram determine language_code ?
i set spanish language (sp) on windows version telegram app, but language_code = 'en'.
how it work?
Do language codes exist for all clients? Noticing from 520k users that only about 52k have codes set in my tracking. Anyone else seeing the same?
Yes, is not in all clients, and in some cases it's "en" also I language is different.
I think the best method should be that the user select his language, not auto detecting it.
Most helpful comment
@jejopl, @sidelux thank you for the tip!
i18n in configuration allows you to set a default language, so in case of an empty field or
undefinedit should show the language set.But it is better to avoid errors in the logic and insert a check on the presence nad of the value of
msg.from.language_code:New code: