Development > Module Requests

Translation module

(1/1)

lulifuz:
Hi there,

with transfer coming along, and for people from other countrys I thougt it may be a good idea to have a translation modul.

Basicaly you would write:
!translate somewordinnativelanguage
and the bot would tell you what that means translated in english.
I did this quite a while ago, but this is not working anymore.
And I am not up to date with the new module programming stuff.
But I did a little research, maybe this could be used for such a module?

Regards,

 lulifuz

lulifuz:
Hi,

as no one seems to care  ;D , I did it myself.
Had to migrate some other modules to 0.6 anyway.

This one translates from german to english.
But you can change this easyly. Look at the file languages.ini here.

From there you also need GTranslate.php (from the svn).
Name it _GTranslate.php and save to your custom modul folder.
Ther might be another place to store .php files which are no modules, but I didnt figure it out.
The languages.ini need to be also stored in the custom modules folder.

The help discription is in german, sorry, but you can translate it with google translate  :P


--- Code: ---<?php
/*
* <translate.php> - Übersetzt Text von englisch nach deutsch und anderst herum.
*
* BeBot - An Anarchy Online & Age of Conan Chat Automaton
* Copyright (C) 2004 Jonas Jax
* Copyright (C) 2005-2010 Thomas Juberg, ShadowRealm Creations and the BeBot development team.
*
* Developed by:
* - Alreadythere (RK2)
* - Blondengy (RK1)
* - Blueeagl3 (RK1)
* - Glarawyn (RK1)
* - Khalem (RK1)
* - Naturalistic (RK1)
* - Temar (RK1)
*
* See Credits file for all aknowledgements.
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; version 2 of the License only.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
*  USA
*
* File last changed at $LastChangedDate: 2008-11-30 23:09:06 +0100 (Sun, 30 Nov 2008) $
* Revision: $Id: _ExampleModule.php 1833 2008-11-30 22:09:06Z alreadythere $
*/

/*
Add a "_" at the beginning of the file (_ClassName.php) if you do not want it to be loaded.
*/

require("_GTranslate.php");

$translate = new Translate($bot);

/*
The Class itself...
*/
class Translate extends BaseActiveModule
{
/*
Constructor:
Hands over a referance to the "Bot" class.
Defines access control for the commands
Creates settings for the module
Defines help for the commands
*/
function __construct (&$bot)
{
//Initialize the base module
parent::__construct(&$bot, get_class($this));

/*
Register commands with the bot.
Possible values for $channel:
tell (incomeing tell)
pgmsg (message in privategroup)
gc (message in guildchat)
all (tell, pgmsg, gc at once)
extpgmsg (external private group, this is not covered by the command_handler() on default)

$command should be a string with the command the module should react to

$access_level is the default access level for the command

$sub_access_levels is an array of entries in the format "subcommand" => "level" to define the default access level for subcommands
*/
$this->register_command('all', 'de2en', 'MEMBER');
$this->register_command('all', 'en2de', 'MEMBER');

/*
Create help for this module
'description' is a brief description of what the module does
'command' is an array that holds
'command1' is one of the commands handled by this module
'command1 <keyword>' is what the command does when given the <keyword>
'notes' are notes that are useful to know.
*/
$this -> help['description'] = 'Übersetzt englischen Text ind Deutsche und zurück.';
$this -> help['command']['de2en <Text>'] = "Übersetzt den deutschen <Text> ins Englische.";
$this -> help['command']['en2de <Text>'] = "Übersetzt den englischen <Text> ins Deutsche.";
}


/*
Unified message handler
$source: The originating player
$msg: The actual message, including command prefix and all
$type: The channel the message arrived from. This can be either "tell", "pgmsg" or "gc"
*/
function command_handler($source, $msg, $origin)
{
//ALWAYS reset the error handler before parsing the commands to prevent stale errors from giving false reports
$this->error->reset();

//The default is to split the command to com, sub and args. If you want to split it some other way change the pattern for it
//parse_com() returns an array where the pattern is the keys and the values are split out from $msg
$com = $this->parse_com($msg, array('com', 'text'));

$command = $vars[0];

switch($com['com'])
{
case 'de2en':
return($this -> transde2en($com['text']));
break;
case 'en2de':
return($this -> transen2de($com['text']));
break;
default:
// Just a safety net to allow you to catch errors where a module has registered  a command, but fails to actually do anything about it
$this -> error -> set("Broken plugin, recieved unhandled command: $command");
return($this->error->message());
}
}


function transde2en($in)
{

try{
$gt = new Gtranslate;

$out = $gt->de_to_en($in);
} catch (GTranslateException $ge)
{
       $out = $ge->getMessage();
}

return $out;

}

function transen2de($in)
{

try{
$gt = new Gtranslate;

$out = $gt->en_to_de($in);
} catch (GTranslateException $ge)
{
       $out = $ge->getMessage();
}

return $out;

}

}
?>

--- End code ---

lulifuz:
Ok, works fine so far, but I got one problem.
Sending special characters (like german umlauts äöü) to google works fine.
But when google returns them they are not shown as such.
Any idea how to convert them?

Thanks,

 lulifuz

Oolwe:
for special chars:

--- Code: --- function transen2de($in)
{

try{
$gt = new Gtranslate;

$out = $gt->en_to_de($in);
} catch (GTranslateException $ge)
{
       $out = $ge->getMessage();
}

return utf8_decode($out);

}

}
?>

--- End code ---

Navigation

[0] Message Index

Go to full version