Universal Translator




Notes:

  1. Pretty simple lab. This will translate or restore text into semi-sudo pig latin
  2. I used an internal class, which will stop other assemblies from accessing it



Front end code

    9             <asp:Label ID="lblTranslatorText" runat="server" Text="Universal Translator" style="font-weight:bold;font-size:large;" />

   10             <hr />

   11             <asp:TextBox ID="txtSource" runat="server" TextMode="MultiLine" />

   12             <br />

   13             <asp:Button ID="btnTranslate" runat="server" Text="Translate" OnClick="btnTranslate_Click" />



Source Code.

    1 using System;

    2 using System.Data;

    3 using System.Configuration;

    4 using System.Collections;

    5 using System.Web;

    6 using System.Web.Security;

    7 using System.Web.UI;

    8 using System.Web.UI.WebControls;

    9 using System.Web.UI.WebControls.WebParts;

   10 using System.Web.UI.HtmlControls;

   11 namespace BicNet.Projects.mcp.chapter3

   12 {   

   13     public partial class Translator : System.Web.UI.Page

   14     {

   15         TranslatorClass objTranslator;

   16 

   17         void Page_Load(object sender, EventArgs e)

   18         {

   19             //the first time this page is displayed

   20             if (!IsPostBack)

   21             {

   22                 //Create a new translator class

   23                 objTranslator = new TranslatorClass();

   24                 //store the object in a session state variable

   25                 Session["Translator"] = objTranslator;

   26             }

   27             else

   28             {

   29                 //Get the session Transclass variable

   30                 objTranslator = (TranslatorClass)Session["Translator"];

   31             }

   32 

   33         }

   34         protected void btnTranslate_Click(object sender, EventArgs e)

   35         {

   36             //Declare a boolean switch

   37             bool bolSwitch;

   38             //Check if ViewState variable exists

   39             if (ViewState["Switch"] != null)

   40                 bolSwitch = !(bool)ViewState["Switch"];

   41             else //set the switch

   42                 bolSwitch = true;

   43 

   44             //Save the new value in the ViewState

   45             ViewState["Switch"] = bolSwitch;

   46             //User the switch to either translate to restore

   47             //the text in txtSource

   48             if (bolSwitch)

   49             {

   50                 //get the text

   51                 objTranslator.Text = txtSource.Text;

   52                 //Translate it

   53                 objTranslator.Translate();

   54                 //display the text               

   55                 txtSource.Text = objTranslator.Text;

   56                 //change the button text

   57                 btnTranslate.Text = "Restore";

   58             }

   59             else

   60             {

   61                 //Restore the original text

   62                 objTranslator.Restore();

   63                 //dispalay the text

   64                 txtSource.Text = objTranslator.Text;

   65                 //Change the button text

   66                 btnTranslate.Text = "Translate";

   67             }

   68 

   69         }

   70 

   71 }

   72 

   73     internal class TranslatorClass

   74     {

   75         string mstrText, mstrOriginal;

   76 

   77         //Controls access to class-level variables

   78         public string Text

   79         {

   80             get { return mstrText; }

   81             set

   82             {

   83                 mstrText = value;               

   84                 mstrOriginal = value;

   85             }

   86         }

   87 

   88         /// <summary>

   89         /// Restores the Translated text back to the original

   90         /// </summary>

   91         public void Restore()

   92         {

   93             mstrText = mstrOriginal;

   94         }

   95 

   96         /// <summary>

   97         /// Translate the value in the Text Property

   98         /// </summary>

   99         public void Translate()

  100         {

  101             string strWord;

  102             string[] astrWords;

  103             bool bolCaps = false;

  104             //Convert the string into an array using System.String

  105             astrWords = mstrText.Split(' ');

  106             for (int i = 0; i < astrWords.GetUpperBound(0); i++)

  107             {

  108                 //Change to lowercase

  109                 strWord = astrWords[i].ToLower();

  110                 //check if word is capitalized

  111                 if (!astrWords[i].Equals(strWord))

  112                     bolCaps = true;

  113                 //Do Translation

  114                 if (strWord != "")

  115                 {

  116                     strWord = strWord.Substring(1, strWord.Length - 1) + strWord.Substring(0, 1) + "ay";

  117                     //Recapitalize if necessary.

  118                     if (bolCaps)

  119                         strWord = strWord.Substring(0, 1).ToUpper() + strWord.Substring(1, strWord.Length - 1);

  120                 }

  121                 //store the word back in the array

  122                 astrWords[i] = strWord;

  123                 //Reset the caps flag.

  124                 bolCaps = false;

  125             }

  126             // Rebuild the string from the array

  127             mstrText = String.Join(" ", astrWords);

  128         }

  129 

  130 

  131     }

  132 

  133 

  134 }