Skip to content

Instantly share code, notes, and snippets.

@mh-firouzjah
Last active May 9, 2024 15:42
Show Gist options
  • Save mh-firouzjah/9aec6745dc0ec4a3f204e65839bf2b38 to your computer and use it in GitHub Desktop.
Save mh-firouzjah/9aec6745dc0ec4a3f204e65839bf2b38 to your computer and use it in GitHub Desktop.
Translate selected text on Linux with ease! Assign a keyboard shortcut to instantly translate and pronounce selected texts. πŸŒπŸ“

Text Translator and Pronunciation Tool

This script provides a convenient way to check spelling, pronunciation, and translation of words or text using a dictionary (such as Google Translate) directly from your Linux desktop. It can be easily integrated with a keyboard shortcut, making it globally accessible for quick translation and pronunciation of selected text.

Background

As a non-native English speaker, I often find myself needing to verify the spelling, pronunciation, or translation of words, phrases, or paragraphs using an online dictionary like Google Translate. To streamline this process, I developed a script that allows me to quickly translate and hear the pronunciation of selected text with a simple keyboard shortcut.

How It Works

This script utilizes command-line tools (trans or translate-shell, xclip, and notify-send) to achieve the following functionalities:

  • Translation: Translates the selected text using an online translator (Google Translate in this case).
  • Pronunciation: Retrieves the pronunciation of the translated text.

Requirements

Ensure the following packages are installed on your Linux system:

  • trans or translate-shell: Command-line translator supporting Google Translate, Bing Translator, Yandex.Translate, etc.
  • xclip: Command-Line interface to X selections (clipboard).
  • notify-send or libnotify-bin: Program to send desktop notifications.

You can install these packages using the package manager of your Linux distribution (e.g., apt for Debian/Ubuntu).

Installation and Usage

  • Download the Script: Save the script (translator.sh) to a directory of your choice (e.g., ~/.local/bin/):

    mkdir -p ~/.local/bin/  # Create the directory if it doesn't exist
    curl -o ~/.local/bin/translator.sh https://gist.githubusercontent.com/mh-firouzjah/9aec6745dc0ec4a3f204e65839bf2b38/raw/ee5d27ed892d6a13283855fc2284d5a984e55d89/translator.sh
  • Make the Script Executable: Ensure the script is executable:

    chmod +x ~/.local/bin/translator.sh
  • Test the Script: Run the script from the command line to ensure it works:

    ~/.local/bin/translator.sh
  • Assign a Keyboard Shortcut: Set up a keyboard shortcut in your Linux Desktop/WM to execute the script (translator.sh) when triggered. This process may vary depending on your desktop environment.

Usage

After setting up the script and keyboard shortcut:

  1. Highlight/Select Text: Use your mouse to highlight or select any text within any window.
  2. Invoke Translation and Pronunciation: Press the assigned keyboard shortcut to initiate the translation and pronunciation of the selected text.
  3. View Translation and Pronunciation: A pop-up notification will display the translation and pronunciation of the selected text.

Customization

Feel free to customize the script according to your preferences and needs. You can adjust the translation settings, modify the keyboard shortcut, or enhance the functionality based on different dictionaries or services.

  • Note: For detailed configuration and usage of the translate-shell package, refer to its official GitHub repository. This repository provides comprehensive documentation and examples to help you customize the translation settings according to your preferences and linguistic needs.
#!/bin/bash
# Function to translate text using 'trans'
translate() {
# Use 'trans' to translate the provided text (:fa means into Persian)
res=$(trans :fa -no-auto -no-bidi -sp -b -j "$1")
# Display translation in a desktop notification
notify-send --expire-time=5000 --icon=crow-translate-tray "Translator" "$res"
}
# Function to get selected text from the primary clipboard and translate it
select_and_translate() {
# Use 'xclip' to retrieve the selected text from the primary clipboard
selected_text=$(xclip -out -selection primary)
# Call the translate function with the selected text
translate "$selected_text"
}
# Call the select_and_translate function to perform translation on selected text
select_and_translate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment