N years agoArticleCome on, take it out for drying, haha...
I. Overview
At present, there are a variety of translation software on the market, which is relatively common in China, such as Kingsoft, Which is internationalized.ProgramDuring development, it is often necessary to translate multiple languages, such as English into Russian and Chinese into Spanish. However, there are very few professional translation software on the market, who has machines installed so many large software has certain economic and machine affordability limitations. Later, I found that there are many online translation websites, such as Yahoo's "translation fish", which can achieve translation in N multiple languages and play a very helpful role in related work. However, due to the inherent defects of the browser, it is inconvenient to use it. According to my habits, I decided to develop a client program and embed the function of the translation fish into the program, it will be very convenient.
Ii. Development
With the above ideas, I successfully realized my "dream" in two hours and had a complete set of personalized multilingual translation software.
Development Environment: vs2003
Development language: C #
Database: None
1. system initialization
For simplicity, the program has only one main interface, and there is no configuration interface or configuration file. Speaking of this, you may ask, if there is no configuration file, how do you set the language that the system can translate? This is very simple. I directly captured the translation fish webpage, analyzed the text, found the list of languages it can process, and put it directly on my interface, in this way, no matter what language it adds, I can automatically adapt to it.
The WebClient class is used to capture webpages. The usage is as follows:
Byte [] bytes = client. downloaddata ("http://fanyi.cn.yahoo.com/translate_txt? More = 1 ");
String content = system. Text. encoding. utf8.getstring (bytes );
Int Pos = content. indexof ("<Div class = \" tctrl \ "> ");
Int POS _ = Pos;
While (Pos> 0 & Pos _> 0)
{
Pos = content. indexof ("<option", POS _);
If (Pos> 0)
{
Pos _ = content. indexof ("</option>", POS );
If (Pos _> 0)
{
String option = content. substring (Pos, POS _-pos + 9 );
System. Diagnostics. Debug. writeline (option );
Fillsingleoption (option );
}
}
}
The above function references a function named fillsingleoption. It is used to extract each line of language and fill it in the language selection box on the interface,CodeThe main parts are as follows:
Int Pos = option. indexof ("value = \"");
Int POS _ = option. indexof ("\" ", POS + 1 );
String value = option. substring (Pos + 8, POS _-pos-2 );
Pos = option. indexof ("> ");
Pos _ = option. indexof ("</");
String text = option. substring (Pos + 1, POS _-pos-1 );
If (hash [value] = NULL)
{
Hash [value] = text;
Lang = new Lang ();
Lang. value = value;
Lang. Text = text;
Coblang. Items. Add (Lang, system. Windows. Forms. checkstate. Unchecked );
}
Ah, sorry, it's really a loop. In the small function above, a new class, called Lang, appeared again. What did it do? It is used to fill information in the checklist. Why add a class to items? Because only one object can be added, the information I need to record includes Code such as ZH-en and text such as "Chinese-English", so I have to worry about it and write a class. There are only a few lines of code for this class, that is, two attributes, and two values are recorded. However, you must rewrite the tostring function. Otherwise, the words "object" will be displayed in the checklist.
The class code is as follows:
Public class Lang
{
Private string value _;
Private string text;
Public String Value
{
Get {return value _;} set {value _ = value ;}
}
Public String text
{
Get {return text;} set {text = value ;}
}
Public override string tostring ()
{
Return text;
}
}
Wow, it's the smallest class in history, but it's also the end of its history.
Initialization has ended so far!
2. Translation
After the program starts, the real work is about to begin. I have set two text boxes, one for the input source character and the other for the output, which is different from the translation fish, I can select multiple translation conditions at the same time here, and it is very convenient to combine them during output.
How to achieve translation, after careful analysis of the translation fish web page, found that can be used to get the results, as follows: http://fanyi.cn.yahoo.com/translate_txt? More = 1 & trtext = China & Lp = en_ru
In the preceding URL, three parameters are described as follows:
Ø more = 1 this seems to have to be written. It is defined by the system.
Ø trtext = China indicates the source content to be translated.
Ø Lp = en_ru refers to the rules used for translation. At present, it refers to translating English into Russian.
In this case, what form will the translation result return? We all want this page to return a result directly, but I failed to do so. In fact, it still returns a page, and the Translation results are embedded in it. No way, we can only use a program to dig deep into the translation results. Fortunately, with the previous web page capture experience, we can directly look at the relevant code:
String result = "";
Foreach (Object o in coblang. checkeditems)
{
Lang = (Lang) O;
String src = txtsrc. Text. Trim ();
If (SRC! = "")
{
String url = "http://fanyi.cn.yahoo.com/translate_txt? More = 1 & trtext = "+ SRC +" & Lp = "+ Lang. value;
Byte [] bytes = client. downloaddata (URL );
String content = system. Text. encoding. utf8.getstring (bytes );
Int Pos = content. indexof ("<Div id = \" Pd \ "class = \" Pd \ "> ");
If (Pos> 0)
{
Int POS _ = content. indexof ("</div>", POS + 1 );
If (Pos _> 0)
{
Result + = Lang. text + "\ r \ n" + content. substring (Pos + 24, POS _-pos-24 ). trim () + "\ r \ n ";
}
}
}
Txtresult. Text = result;
}
Wow, it looks messy. In fact, it's not messy at all. You just need to retrieve the content of the webpage, find the corresponding location, find the ending symbol, and retrieve the corresponding content. Right, do not forget to trim the result.
Take a closer look at the above Code and find that multiple translations have been implemented at the same time, and the results are spliced.
Iii. Conclusion
This is too abstract. For more information, see the interface:
By the way, do not forget to change the font of the output result box to Arial. If the default is used, some texts may not be easy to display, but if the texts in some countries cannot be correctly displayed, this is not a problem with the program. You need to install the corresponding language pack on your own.
The interface and functions are very simple, but they are the most useful.