Use a translator in the CEF cottage and a translator in the CEF cottage
Cause
In some cases, there is a need to translate a language from another type into another type. For example, when generating an object, you may need to convert the Chinese name to English. So we used CEFSharp to copy a translator. As follows:
CEF Introduction
CEF is called Chromium Emmbed Framework and is an open-source project. Embedded Web browser controls based on Google Chromium projects.
Application of CEF in. NET
The CEF is written by C ++ and cannot be directly applied to. NET. It needs to be packaged in some form. Common examples include ium. CefGlue and CEFSharp. I am personally familiar with CEFSharp.
Among the many functions of CEFSharp, I like CEF to execute JS Code and register local objects. Combining these two functions, I can make a lot of interesting results.
Since the WPF version of CEFSharp cannot input Chinese Characters in Win10, I generally use the WinForm version of CEFSharp. in WPF, Windows formshost is used for packaging.
Principles of shanzhai Translator
The cottage translator adds a CEFSharp browser on the interface to access Bing translation. When entering Chinese characters on the interface, use JS Code to set the sentence to be translated on the Bing translation page to Chinese on the interface, and then use JS Code to simulate and click the translation button, when Bing translation gets the result, use JS to call local code to set the interface result to bing translation result.
At this time, some people will raise questions. Where is the CEFSharp browser? Why didn't I see it on the interface? In fact, a small Trick is used here to set the width and height of the browser to 1 pixel, of course, no more.
Translation triggered due to delay in some details
When the input of the Chinese to be translated is fast, if each input changes, the translation process described above is called, which is less efficient. There is also a problem of interaction. If the user's input is not blocked, the previous Translation results may not arrive until the network is unstable, so that the translation results are inaccurate. If the network is congested, the network is always better. If the network is slow, the experience is extremely poor.
Therefore, I have created ThrottleInvokeCommandAction Based on Blend Interactivity, which can call the command at some time after no operation.
Notification of translation completion
Because we do not know when the translation is complete, we can listen to the DOMNodeInserted event to receive a notification. When an event occurs, the method of the registered local object is automatically called. However, the method name in js is different from that in C #. The method name must be in lowercase.
Enable Developer Tools
In CEF, developers can be enabled to facilitate debugging. In my code, I listened to the CEF keyboard event. When I press F12, the developer tool is displayed.
Advanced
The above is just a simple translation, and there are other translation options on the Bing translation page, which can be integrated into the cottage translator. For example, you can select multiple candidate items or integrate the translation process into Entity generation.
Code download
Blog: Translator
Notes
- The referenced DLL is missing from the downloaded source code. For more information about the DLL, see
You can use nuget to obtain cefsharp39. The command is as follows:Install-Package CefSharp.WinForms -Version 39.0.0
You can use nuget to obtain the Prism. The command is as follows:Install-Package Prism -Version 4.1.0
- Copy the reference files of cefsharp39 to the generated directory before running the command.
- On the project property generation tab, set the target platform to x86.