The clipboard translator is much closer to working now. Tests with BK2 went reasonably well.

This commit is contained in:
Storm Dragon 2022-10-04 16:42:32 -04:00
parent 46fda47279
commit 4c440646f0

View File

@ -1,18 +1,23 @@
#!/usr/bin/env bash #!/usr/bin/env bash
cleanup() {
sort -uo "$dictionaryFile" "$dictionaryFile"
}
trap cleanup EXIT
# Clear the clipboard so you don't accidently send personal info to the translator. # Clear the clipboard so you don't accidently send personal info to the translator.
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
export dictionaryFile="${XDG_CONFIG_HOME:-$HOME/.config}/storm-games/audiogame-manager/translations.txt" export dictionaryFile="${XDG_CONFIG_HOME:-$HOME/.config}/storm-games/audiogame-manager/translations.txt"
if [[ ! -r "$dictionaryFile" ]]; then if [[ ! -r "$dictionaryFile" ]]; then
touch "$dictionaryFile" touch "$dictionaryFile"
fi fi
newText=""
oldText=""
oldTranslatedText=""
while : ; do while : ; do
newText="$(xclip -d "${DISPLAY:-:0}" -selection clipboard -o)" newText=""
translatedText=""
newText="$(xclip -d "${DISPLAY:-:0}" -selection clipboard -o 2> /dev/null)"
sleep 0.01 sleep 0.01
if [[ "$oldText" == "$newText" ]] || [[ "$newText" == "" ]]; then if [[ "${#newText}" -lt 1 ]]; then
continue continue
fi fi
export newText export newText
@ -20,15 +25,14 @@ while : ; do
$1~originalText { print $2; exitCode = 0; exit} $1~originalText { print $2; exitCode = 0; exit}
END { exit exitCode }' "$dictionaryFile")" END { exit exitCode }' "$dictionaryFile")"
if [[ "${#translatedText}" -ge 1 ]]; then if [[ "${#translatedText}" -ge 1 ]]; then
if [[ "${translatedText}" != "${oldTranslatedText}" ]]; then spd-say "$translatedText"
spd-say "$translatedText" echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
fi
oldTranslatedText="$translatedText"
continue continue
fi fi
translatedText="$(trans -b -- "$newText")" translatedText="$(trans -b -- "$newText")"
echo "${newText})==:[${translatedText}" >> "$dictionaryFile" if [[ "${newText}" != "${translatedText}" ]]; then
sort -uo "$dictionaryFile" "$dictionaryFile" echo "${newText})==:[${translatedText}" >> "$dictionaryFile"
fi
spd-say "$translatedText" spd-say "$translatedText"
oldText="$newText" echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
done done