Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement word-marking Translation tool on Linux platform

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to implement the word-marking translation tool on the Linux platform. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The implemented word translation tool mainly has the following characteristics:

Support the translation of English words and phrases into Chinese

Word translation, terminal display

Automatically filter special characters such as line breaks in selected text

Rely on only a few Linux command tools

The following is a motion picture for demonstration.

The environment I use is Ubuntu 18.04.3 LTS, a Linux distribution running under a VMware virtual machine, so the steps described here may be slightly different from those in other Linux distributions. Let's implement it step by step.

one。 Install the necessary commands

1.xclip

$sudo apt install xclip

The xclip command establishes a channel between the terminal and the clipboard, which can save the terminal output or the contents of the file to the clipboard, or output the contents of the clipboard to the terminal or file. For detailed usage, you can use man xclip, see its manual. Here are some common uses.

$xclip file_name # file content saved to X window clipboard $xclip-selection c file_name # file content saved to external clipboard $xclip-o # X window clipboard content output to terminal display $xclip-selection c-o # external clipboard content output to terminal display

It is worth emphasizing that the X window clipboard mentioned here simply means that the text you select with the mouse will be stored on the clipboard in real time and can be pasted using the middle mouse button. The external clipboard holds the text you copied with ctrl+c, and ctrl+v can paste it. These two places are different.

2.translate-shell

$sudo apt install translate-shell

This is the command-line version of the Google translation tool, formerly known as Google Translate CLI, a command-line translator that translates with Google translation (default), Bing translation, etc. It allows you to access these translation engines on the terminal. Translate-shell is available in most Linux distributions. The common methods are as follows:

$trans en:zh [word] # English-to-Chinese word translation $trans en:zh-b [text] # brief output for text translation

It is important to note that using this translation tool requires you to be able to access the external network, or by modifying the default translation engine of translate-shell. The specific methods are not described here.

two。 Programming realization

The overall idea of this tool is that the C program detects the dynamics of the mouse button in real time. When it is detected that the user uses the mouse to select a piece of text, the shell script is called to obtain the contents of the X window clipboard and then translated and output to the terminal display.

1. Locate the mouse device file

The mouse acts as the input device. Its information can be viewed in the file / proc/bus/input/devices using the following command:

$sudo cat / proc/bus/input/devices I: Bus=0011 Vendor=0002 Product=0013 Version=0006 N: Name= "VirtualPS/2 VMware VMMouse" P: Phys=isa0060/serio1/input1 S: Sysfs=/devices/platform/i8042/serio1/input/input4 U: Uniq= H: Handlers=mouse0 event2 B: PROP=0 B: EV=b B: KEY=70000 0000 B: ABS=3

The value event2 of Handlers indicates that the status of the mouse can be read under the / dev/input/event2 file. It is important to note that the file that reads the status of the mouse may be different for different devices, such as / dev/input/event3. We can use the following command to find out which event your mouse corresponds to.

$sudo cat / dev/input/event2 | hexdump # you can change the number during the test

For example, when I run the above command, I move the mouse and press the left / middle / right mouse button, the terminal will output some values, which means that the event2 file corresponds to my mouse. If there is no response to the operation of the mouse, this is not. You can find the corresponding event file for your mouse in this way.

2. Get keystroke response under Linux

In the Linux kernel, input devices are described by input_dev structures. when using the input subsystem to implement the input device driver, the core job of the driver is to report input events such as keys, touch screen, keyboard, mouse and so on to the system (event, described by the input_event structure). You no longer need to care about the file operation interface. Because the input subsystem has completed the file manipulation interface Linux/input.h this file defines the structure of event events API and standard key coding and so on.

/ / for the definition of the structure, see input.h struct input_event {struct timeval time; / / key time _ _ U16 type; / / event type _ _ U16 code; / / what key to simulate as _ _ S32 value; / / whether to press or release} / / for the following macro definition, please see input-event-coses.h / / type # define EV_KEY 0x01 # define EV_REL 0x02 # define EV_ABS 0x03 /. / / code # define BTN_LEFT 0x110 # define BTN_RIGHT 0x111 # define BTN_MIDDLE 0x112 /. / / value # define MSC_SERIAL 0x00 # define MSC_PULSELED 0x01 / /.

Here a little introduction to type, refers to the event type, the common event types are: EV_KEY, key events, such as the keyboard button (which key is pressed), the left and right mouse button (whether to hit) and so on; EV_REL, relative coordinates, mainly refers to the mouse movement events (relative displacement); EV_ABS, absolute coordinates, mainly refers to the touch screen movement events.

3. Write a C program

Now you can write a program to detect the movement of the mouse. First create a folder Translator under your user's directory. Create a ct.c source file in Translator with the following code:

# include # include int main (void) {int keys_fd; struct input_event t; / / Note that the file opened here changes accordingly according to your own device keys_fd = open ("/ dev/input/event2", O_RDONLY); if (keys_fd / dev/null | xargs) if [["$str_new"! = "$str_old" & & $str_new]] Then echo-e "\ n" count=$ (echo "$str_new" | wc-w) if ["$count" = = "1"]; then echo-n-e "$str_new" > > ~ / Translator/words echo "$str_new" | trans: zh-CN | tail-1 | cut-c 5-| sed "s,\ X1b\ [0-9 ] * [a-zA-Z], g "| tee-a ~ / Translator/words else echo" $str_new "| trans: zh-CN-b fi echo" $str_new "> ~ / Translator/lastContent fi

The principle is very simple and readers can understand it by themselves. Here, we also need to create a lastContent.txt file in Translator as a cache, so that we can get the translated text of the last call when calling the script this time. If it is the same as the translated text of this call, it will not be translated this time.

Set ct alias

Here you can run the program with the following command:

$sudo ~ / Translator/ct

But since we output such a long command every time we run, we add the following command to the ~ / .bashrc file.

Alias ct='sudo ~ / Translator/ct'

In this way, every time you read the English literature in the future, you can enter it under the command line:

This is the end of the ct article on "how to implement the word translation tool on the Linux platform". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report