RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Translation book using Google

alambik

Sorceror

Imagine you can understand what other players says, even if they dont speak your language...
You are english, the player in front of you speaks in swedish, but when he speaks, what he says is autromatically translated into english in your UO console... And when you speak english, the swedish understand what you says, as well as the spanish player just behind you, as your words are translated into their language...

That is the aim of the script I want to submit. It works: as a book, you define your language, and the other players does the same. When your book is in your backpack, what other says is tranbslated to you (if its not the same language of course). If you let the book on the ground, the translation appears to everyone around, on the top of the book.

However, there are 3 things I want you to help me before the submission:
- the script is RunUO 1.0 : someone will have to submit an up-to-date version
- the script cannot handle well languages like russians, chinese, greek: I want somebody to test and fix this issue for the whole community. It's about encoding, and I don't know how it works.
- the script decrease the performance as it is not a thread-managed system: if a thread that translate the words could be created to not disturb the main server thread, that would solve performance issue. I don't know how it works in C#.

Would you be able to do this?
 

milva

Sorceror
Wow this is really needed for so many servers- I can't count the amount of players who only speak spanish or are turkish, where some know the basic english language others no nothing. Hopefully a few will help out with this script :)
 

alambik

Sorceror
Here is the script to fix.
The Item shall be configured by double-click and entering the language of the player using the standard language 2 letters.
Example:
en for english
fr for french
etc...

The Item shall be place in the backpack of the player. Another use is to put it on the ground.

Code:
using System;
using System.Collections;
using System.Net;
using System.Text;
using System.Net.Sockets;
using Server;
using Server.Network;
using Server.Targeting;
using Server.Mobiles;
using Server.Prompts;
 
namespace Server.Items
{
  public class Translator : Item
  {
 
      private bool m_Active;
      private string m_Language;
 
      [CommandProperty( AccessLevel.GameMaster )]
      public bool Active
      {
        get{ return m_Active; }
        set{ m_Active = value ; }
      }
 
      [CommandProperty( AccessLevel.GameMaster )]
      public string Language
      {
        get{ return m_Language; }
        set{ m_Language = value ; }
      }
 
      [Constructable]
      public Translator() : base( 4079 )
      {
        Name="Translator";
      }
 
      public Translator( Serial serial ) : base( serial )
      {}
 
      public override void OnDoubleClick( Mobile from )
      {
        from.SendMessage("Language:");
        from.Prompt = new LanguagePrompt( this );
      }
 
 
      public override bool HandlesOnSpeech
      {
        get{ return Active && ( RootParent == null || RootParent is Mobile ); }
      }
 
      public override void OnSpeech( SpeechEventArgs e )
      {
 
        Mobile from = e.Mobile;
 
        if ( !Active || ( RootParent != null && !(RootParent is Mobile) ))
            return;
 
        if (RootParent is Mobile)
            if ( from == ((Mobile)RootParent) )
              return;
 
        if ( e.Type == MessageType.Emote )
            return;
           
       
        string speech = e.Speech;
        if (from.Backpack != null)
        {
            Translator translator=(Translator)(from.Backpack.FindItemByType(typeof(Translator)));
            if (translator != null)
            if (translator.Language != m_Language)
            try {
              string translation=TranslateText(speech,translator.Language+"|"+m_Language);
              if (RootParent == null)
                  PublicOverheadMessage( MessageType.Regular, 0x2B2, false, translation );
              else
                  if (RootParent is Mobile) ((Mobile)RootParent).SendMessage(translation);
            }
            catch {}
        }
      }
 
      public override void Serialize( GenericWriter writer )
      {
        base.Serialize( writer );
        writer.Write( 0 ); // version
        writer.Write( m_Active );
        writer.Write( m_Language );
      }
 
      public override void Deserialize( GenericReader reader )
      {
        base.Deserialize( reader );
        int version = reader.ReadInt();
        m_Active = reader.ReadBool();
        m_Language = reader.ReadString();
      }
     
      public static string TranslateText(
      string input,
      string languagePair)
      {
        string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
        WebClient webClient = new WebClient();
       
        byte[] pageData = webClient.DownloadData(url);
            string result = System.Text.Encoding.UTF8.GetString(pageData);
 
        int iStart = result.LastIndexOf("TRANSLATED_TEXT='"); 
              int iEnd = result.IndexOf("';", iStart+"TRANSLATED_TEXT='".Length);
            result = result.Substring(iStart+"TRANSLATED_TEXT='".Length, iEnd-iStart-"TRANSLATED_TEXT='".Length );
              result = result.Trim();       
        return result;
      }
  }
 
  public class LanguagePrompt : Prompt
  {
      private Translator m_Translator;
 
      public LanguagePrompt( Translator translator )
      {
        m_Translator = translator;
      }
 
      public override void OnResponse( Mobile from, string text )
      {
        if (  m_Translator != null )
        if ( !m_Translator.Deleted )
        {
            if (text == "")
              m_Translator.Active = false;
            else
            {
              m_Translator.Active = true;
              m_Translator.Language = text;
            }
        }
      }
  }
}
 

ViWinfii

Sorceror
alambik, what if the overhead speech text from a character is translated to the language of each player's choice without the need for an item?

For example: Let's say there's 3 characters - John, Bill, and Mary. John speaks English, Bill speaks French, and Mary speaks German. When John speaks, Bill will see the character John speaking French. Bill replies in French, and John sees Bill speaking English. Mary watches both John and Bill speaking, but Mary sees both of them speaking German.

I have a script that does this already, but only does it for make-believe languages like Elven, Drow, Orcish, etc. Here it is: http://www.runuo.com/community/resources/multilinguists.93/ I would like to implement your idea of using google translate in Multilinguists. If you would like, I can help you make a version that only deals with google translate with no extra make-believe languages.
 

alambik

Sorceror
ViWinfii,
That's what I wanted to do at the beginning, but I wanted to quickly check the concept. That would be great of course! However... If google is slow or the service is unavailable or the URL/HTML changes, the players will see nothing, or the answer will appear late. That is why keeping the initial words without translation overhead and translate in the client console seems better: the player can still react quickly even if the google service is unavailable/slow. This is also for such reason I would like this translation to be in a dedicated thread, so the server will not "freeze" each time there are too much things to translate. Anyway, thank you for the link of the multilingual implementation, I searched such script a long time :-p Of course you can integrate this idea with some service low-timeout check into your script if you think that is possible. :)
 
Top