MysteryGuest
Registered Member
- Mar 7, 2013
- 64
- 18
for this we will be needing 2 combo-box, 3 text-boxes; set it on multi line, and we also be needing a button.
you also will be needing WebResourceProvider.dll and StringParser.dll
lets start with the class! i named it translator.
open up the code cave of your main form and just copy this into there. you also will be able to see the names of the controls! just simply change them.
and your done!
you also will be needing WebResourceProvider.dll and StringParser.dll
lets start with the class! i named it translator.
Code:
/// <summary>
/// Translates text using Google's online language tools.
/// </summary>
public class Translator : WebResourceProvider
{
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="Translator"/> class.
/// </summary>
public Translator(string reverseTranslation)
{
ReverseTranslation = reverseTranslation;
SourceLanguage = "English";
TargetLanguage = "French";
Referer = "http://www.google.com";
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the source "
/// </summary>
/// <value>The source "</value>
public string SourceLanguage {
get;
set;
}
/// <summary>
/// Gets or sets the target "
/// </summary>
/// <value>The target "</value>
public string TargetLanguage {
get;
set;
}
/// <summary>
/// Gets or sets the source text.
/// </summary>
/// <value>The source text.</value>
public string SourceText {
get;
set;
}
/// <summary>
/// Gets the translation.
/// </summary>
/// <value>The translated text.</value>
public string Translation {
get;
private set;
}
/// <summary>
/// Gets the reverse translation.
/// </summary>
/// <value>The reverse translated text.</value>
public string ReverseTranslation {
get;
private set;
}
/// <summary>
/// Gets the supported languages.
/// </summary>
public static IEnumerable<string> Languages {
get {
EnsureInitialized();
return _languageModeMap.Keys.OrderBy (p => p);
}
}
#endregion
#region Public methods
/// <summary>
/// Attempts to translate the text.
/// </summary>
public void Translate()
{
if (string.IsNullOrEmpty (SourceLanguage) ||
string.IsNullOrEmpty (TargetLanguage) ||
SourceLanguage.Trim().Equals (TargetLanguage.Trim())) {
throw new Exception ("An invalid source or target language was specified.");
}
fetchResource();
}
#endregion
#region WebResourceProvider implementation
/// <summary>
/// Returns the url to be fetched.
/// </summary>
/// <returns>The url to be fetched.</returns>
protected override string getFetchUrl()
{
return "http://translate.google.com/translate_t";
}
protected override string getPostData()
{
var strPostData = string.Format ("hl=en&ie=UTF8&oe=UTF8submit=Translate&langpair={0}|{1}",
LanguageEnumToIdentifier (SourceLanguage),
LanguageEnumToIdentifier (TargetLanguage));
strPostData += "&text=" + HttpUtility.UrlEncode (SourceText);
return strPostData;
}
/// <summary>
/// Parses the fetched content.
/// </summary>
protected override void parseContent()
{
Translation = string.Empty;
var strContent = Content;
var parser = new StringParser (strContent);
var strTranslation = string.Empty;
if (parser.skipToEndOf ("<span id=result_box")) {
while (parser.skipToEndOf ("onmouseout=\"this.style.backgroundColor='#fff'\">")) {
var translatedSegment = string.Empty;
if (parser.extractTo("</span>", ref translatedSegment)) {
strTranslation += " " + StringParser.removeHtml (translatedSegment);
}
}
}
#region Fix up the translation
var startClean = 0;
var endClean = 0;
var i=0;
while (i < strTranslation.Length) {
if (Char.IsLetterOrDigit (strTranslation[i])) {
startClean = i;
break;
}
i++;
}
i = strTranslation.Length - 1;
while (i > 0) {
var ch = strTranslation[i];
if (Char.IsLetterOrDigit (ch) ||
(Char.IsPunctuation (ch) && (ch != '\"'))) {
endClean = i;
break;
}
i--;
}
Translation = strTranslation.Substring (startClean, endClean - startClean + 1).Replace ("\"", "");
#endregion
}
#endregion
#region Private methods
/// <summary>
/// Converts a language to its identifier.
/// </summary>
/// <param name="language">The language."</param>
/// <returns>The identifier or <see cref="string.Empty"/> if none.</returns>
private static string LanguageEnumToIdentifier
(string language)
{
string mode;
EnsureInitialized();
_languageModeMap.TryGetValue (language, out mode);
return mode;
}
/// <summary>
/// Ensures the translator has been initialized.
/// </summary>
private static void EnsureInitialized()
{
if (_languageModeMap != null) return;
_languageModeMap = new Dictionary<string,string>
{
{"Afrikaans", "af"},
{"Albanian", "sq"},
{"Arabic", "ar"},
{"Armenian", "hy"},
{"Azerbaijani", "az"},
{"Basque", "eu"},
{"Belarusian", "be"},
{"Bengali", "bn"},
{"Bulgarian", "bg"},
{"Catalan", "ca"},
{"Chinese", "zh-CN"},
{"Croatian", "hr"},
{"Czech", "cs"},
{"Danish", "da"},
{"Dutch", "nl"},
{"English", "en"},
{"Esperanto", "eo"},
{"Estonian", "et"},
{"Filipino", "tl"},
{"Finnish", "fi"},
{"French", "fr"},
{"Galician", "gl"},
{"German", "de"},
{"Georgian", "ka"},
{"Greek", "el"},
{"Haitian Creole", "ht"},
{"Hebrew", "iw"},
{"Hindi", "hi"},
{"Hungarian", "hu"},
{"Icelandic", "is"},
{"Indonesian", "id"},
{"Irish", "ga"},
{"Italian", "it"},
{"Japanese", "ja"},
{"Korean", "ko"},
{"Lao", "lo"},
{"Latin", "la"},
{"Latvian", "lv"},
{"Lithuanian", "lt"},
{"Macedonian", "mk"},
{"Malay", "ms"},
{"Maltese", "mt"},
{"Norwegian", "no"},
{"Persian", "fa"},
{"Polish", "pl"},
{"Portuguese", "pt"},
{"Romanian", "ro"},
{"Russian", "ru"},
{"Serbian", "sr"},
{"Slovak", "sk"},
{"Slovenian", "sl"},
{"Spanish", "es"},
{"Swahili", "sw"},
{"Swedish", "sv"},
{"Tamil", "ta"},
{"Telugu", "te"},
{"Thai", "th"},
{"Turkish", "tr"},
{"Ukrainian", "uk"},
{"Urdu", "ur"},
{"Vietnamese", "vi"},
{"Welsh", "cy"},
{"Yiddish", "yi"}
};
}
#endregion
#region Fields
/// <summary>
/// The language to translation mode map.
/// </summary>
private static Dictionary<string, string> _languageModeMap;
public Translator()
{
}
#endregion
open up the code cave of your main form and just copy this into there. you also will be able to see the names of the controls! just simply change them.
Code:
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="GoogleTranslatorFrm"/> class.
/// </summary>
public GoogleTranslatorFrm()
{
InitializeComponent();
}
#endregion
#region Form event handlers
/// <summary>
/// Handles the Load event of the GoogleTranslatorFrm control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void GoogleTranslatorFrm_Load
(object sender,
EventArgs e)
{
_comboFrom.Items.AddRange (Translator.Languages.ToArray());
_comboTo.Items.AddRange (Translator.Languages.ToArray());
_comboFrom.SelectedItem = "English";
_comboTo.SelectedItem = "French";
}
#endregion
#region Button handlers
/// <summary>
/// Handles the Click event of the _btnTranslate control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void _btnTranslate_Click
(object sender,
EventArgs e)
{
var translator = new Translator
{
SourceLanguage = (string) _comboFrom.SelectedItem,
TargetLanguage = (string) _comboTo.SelectedItem,
SourceText = _editSourceText.Text
};
_editTarget.Text = string.Empty;
_editTarget.Update();
_editReverseTranslation.Text = string.Empty;
_editReverseTranslation.Update();
try {
Cursor = Cursors.WaitCursor;
_lblStatus.Text = "Translating...";
_lblStatus.Update();
translator.Translate();
_editTarget.Text = translator.Translation;
_editTarget.Update();
_lblStatus.Text = "Reverse translating...";
_lblStatus.Update();
Thread.Sleep (500);
translator.SourceLanguage = (string) _comboTo.SelectedItem;
translator.TargetLanguage = (string) _comboFrom.SelectedItem;
translator.SourceText = _editTarget.Text;
translator.Translate();
_editReverseTranslation.Text = translator.Translation;
}
catch (Exception ex) {
MessageBox.Show (ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally {
_lblStatus.Text = string.Empty;
Cursor = Cursors.Default;
}
}
#endregion
and your done!