Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic localization abilities #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions accidentalbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ var webSocket = require('ws');
var channel = '#atp';
var webAddress = 'http://www.caseyliss.com/showbot';
var TITLE_LIMIT = 75;
var BOT_LANG = 'en';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of hardcoding the language here, you get it from Accept-Language? That way you could serve up different languages to different users from the same running instance depending on their language settings for their browser.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tried yet, but does the Accept-Language propagate through IRC?
This little hack of mine only applies to the bot part, not to the web interface.


var user_string = require('./lang/' + BOT_LANG + '.json');

var titles = [];
var connections = [];
Expand Down Expand Up @@ -35,8 +38,8 @@ function handleNewSuggestion(from, message) {
}

if (title.length > TITLE_LIMIT) {
client.say(from, 'That title is too long (over ' + TITLE_LIMIT +
' characters); please try again.');
client.say(from, user_string['titletoolong'] + TITLE_LIMIT +
user_string['characterstryagain']);
title = '';
}
if (title.length > 0) {
Expand All @@ -59,7 +62,7 @@ function handleNewSuggestion(from, message) {
sendToAll({operation: 'NEW', title: title});
} else {
//client.say(channel, 'Sorry, ' + from + ', your title is a duplicate. Please try another!');
client.say(from, 'Sorry, your title is a duplicate. Please try another!');
client.say(from, user_string['duplicatetitle']);
}
}
}
Expand All @@ -77,10 +80,10 @@ function handleSendVotes(from, message) {
return t.votes;
}, true).to(3);

client.say(from, 'Three most popular titles:');
client.say(from, user_string['threemostpopular']);
for (var i = 0; i < titlesByVote.length; ++i) {
var votes = titlesByVote[i]['votes'];
client.say(from, titlesByVote[i]['votes'] + ' vote' + (votes != 1 ? 's' : '') + ': " ' + titlesByVote[i].title + '"');
client.say(from, titlesByVote[i]['votes'] + ' ' + (votes != 1 ? user_string['votes'] : user_string['vote']) + ': " ' + titlesByVote[i].title + '"');
}
}

Expand All @@ -102,17 +105,17 @@ function handleNewLink(from, message) {

sendToAll({operation: 'NEWLINK', link: link});
} else {
client.say(from, "That doesn't look like a link to me.");
client.say(from, user_string['notalink']);
}
}

function handleHelp(from) {
client.say(from, 'Options:');
client.say(from, '!s {title} - suggest a title.');
client.say(from, '!votes - get the three most highly voted titles.');
client.say(from, '!link {URL} - suggest a link.');
client.say(from, '!help - see this message.');
client.say(from, 'To see titles/links, go to: ' + webAddress);
client.say(from, user_string['options']);
client.say(from, user_string['helpsuggest']);
client.say(from, user_string['helpvotes']);
client.say(from, user_string['helplink']);
client.say(from, user_string['helphelp']);
client.say(from, user_string['helpviewtitles'] + webAddress);
}

var client = new irc.Client('irc.freenode.net', 'accidentalbot', {
Expand Down
15 changes: 15 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"titletoolong": "That title is too long (over ",
"characterstryagain": " characters); please try again.",
"duplicatetitle": "Sorry, your title is a duplicate. Please try another!",
"threemostpopular": "Three most popular titles:",
"vote": "vote",
"votes": "votes",
"notalink": "That doesn't look like a link to me.",
"options": "Options:",
"helpsuggest": "!s {title} - suggest a title.",
"helpvotes": "!votes - get the three most highly voted titles.",
"helplink": "!link {URL} - suggest a link.",
"helphelp": "!help - see this message.",
"helpviewtitles": "To see titles/links, go to: "
}
15 changes: 15 additions & 0 deletions lang/it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"titletoolong": "Quel titolo è troppo lungo (oltre ",
"characterstryagain": " caratteri); riprova.",
"duplicatetitle": "Mi spiace, il tuo titolo è un duplicato. Provane un altro!",
"threemostpopular": "I tre titoli più popolari:",
"vote": "voto",
"votes": "voti",
"notalink": "Quello non mi sembra un link.",
"options": "Opzioni:",
"helpsuggest": "!s {titolo} - suggerisci un titolo.",
"helpvotes": "!votes - ottieni i tre titoli più votati.",
"helplink": "!link {URL} - suggerisci un link.",
"helphelp": "!help - guarda questo messaggio.",
"helpviewtitles": "Per vedere i titoli/link, vai su: "
}