Assume: a WinForms project where:
1. Controls
Button: btnTranslate: click to translate English to Arabic: uses rtbEnglish, rtbArabic
Button: btnSearchButton: click to search for an English word: uses tbSearchEnglish
RichTextBox: rtbEnglish: displays user-entered English text
RichTextBox: rtbArabic: displays English text in rtbEnglish translated into Arabic
TextBox: tbSearchEnglish: displays user-entered English text, to used to search
2. Run-time behavior: Translate
a. When the btnTranslate is clicked: text in rtbEnglish is checked for a valid entry, and if valid:
b. is processed into an Array or generic List of strings; and:
c. the rtbArabic RichTextBox is cleared, and: a word-by-word look-up of each English word in the database is performed which returns a corresponding Arabic word(s) which is then appended to the rtbArabic RichTextBox Text.
3. Run-time behavior: Search
a. tbSearch is parsed for a valid entry (can it have more than one word ?), and, if valid:
b. occurrence of any of the word(s) in the rtbEnglish is highlighted, and their corresponding words in rtbArabic are highlighted.
4. Run-time behavior: user edits rtbEnglish
a. rtbArabic is cleared, any highlights in rtbEnglish are removed ?
5. Strategy:
a. since no run-time editing is allowed without re-translation, then, when the translation is performed word-by-word, you can know the position (start, and end) of each English word, and you can know the position of each Arabic word/phrase inserted.
b. we can use Dictionaries: one to map English words to the Arabic translation, and two of type <string, List<int>> to hold a "mapping" of each English word to
all its occurrences in both rtbEnglish, and rtbArabic RichTextBoxes.
Dictionary<string, string> dctEnglishToArabic = new Dictionary<string, string>();
Dictionary<string, List<int>> dctEnglishWordToLocations = new Dictionary<string, List<int>>();
Dictionary<string, List<int>> dctArabicWordToLocations = new Dictionary<string, List<int>>();
6. Code: ... note
this is an outline of code, not complete working code
private char[] charsToSplit = new char[] {' ', '\t', '\n'};
private void btnTranslate_Click(object sender, EventArgs e)
{
rtbArabic.Clear();
dctEnglishToArabic.Clear();
dctEnglishWordToLocations.Clear();
dctArabicWordToLocations.Clear();
List<string> englishWords = rtbEnglish.Text.Split(charsToSplit, StringSplitOptions.RemoveEmptyEntries).ToList();
int englishNdx = 0;
int arabicNdx = 0;
string theArabic = "";
foreach (string s in englishWords)
{
if (dctEnglishWordToLocations.Contains(s))
{
}
else
{
int eLen = s.Length;
theArabic = YourDataBaseLookUp(s);
int aLen = theArabic.Length;
dctEnglishToArabic.Add(s, theArabic);
dctEnglishWordToLocations.Add(s, new List<int>());
dctArabicWordToLocations.Add(s, new List<int>());
}
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
foreach(string wrd in searchWords
{
List<int> eLocations = dctEnglishWordToLocations[wrd];
List<int> aLocations = dctArabicWordToLocations[wrd];
}
}
Note: A big problem
not addressed here is that you will want to get the positions of the words in the English text by considering exactly where they occur (and that includes the white-space before them) in the rtbEnglish; I am not familiar with punctuation in Arabic, and do not know what issues you will face there.
Note: this "outline" has been done quickly; I'll revise it as I have time.
Note: A problem you will have to deal with here is when you have English text like this:
"An apple a day, will keep the doctor away, but, eating too many apples, like, for example, golden delicious apples, could make you sick."
What do you do to handle translating "apple" "apples" "apples," ?