From 46fda472791e4e303677351b2118870c7dded010 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Tue, 4 Oct 2022 02:57:19 -0400 Subject: [PATCH] First pass at a script to translate the text in the clipboard. Not ready for prime time, unless you are working on the script, do not use yet. --- speech/clipboard_translator.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 speech/clipboard_translator.sh diff --git a/speech/clipboard_translator.sh b/speech/clipboard_translator.sh new file mode 100755 index 0000000..4c04c85 --- /dev/null +++ b/speech/clipboard_translator.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Clear the clipboard so you don't accidently send personal info to the translator. +echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard +export dictionaryFile="${XDG_CONFIG_HOME:-$HOME/.config}/storm-games/audiogame-manager/translations.txt" +if [[ ! -r "$dictionaryFile" ]]; then + touch "$dictionaryFile" +fi +newText="" +oldText="" +oldTranslatedText="" +while : ; do + newText="$(xclip -d "${DISPLAY:-:0}" -selection clipboard -o)" + sleep 0.01 + if [[ "$oldText" == "$newText" ]] || [[ "$newText" == "" ]]; then + continue + fi + export newText + translatedText="$(awk -v originalText="${newText}" 'BEGIN { exitCode = 1; FS = "\\)==:\\[" } + $1~originalText { print $2; exitCode = 0; exit} + END { exit exitCode }' "$dictionaryFile")" + if [[ "${#translatedText}" -ge 1 ]]; then + if [[ "${translatedText}" != "${oldTranslatedText}" ]]; then + spd-say "$translatedText" + fi + oldTranslatedText="$translatedText" + continue + fi + translatedText="$(trans -b -- "$newText")" + echo "${newText})==:[${translatedText}" >> "$dictionaryFile" + sort -uo "$dictionaryFile" "$dictionaryFile" + spd-say "$translatedText" + oldText="$newText" +done