Updated the clipboard translater because it could crash on some translations.
This commit is contained in:
parent
a38fc607a8
commit
ef43dca94f
@ -7,7 +7,6 @@
|
|||||||
# set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/#:~:text=set%20%2Du,is%20often%20highly%20desirable%20behavior.
|
# set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/#:~:text=set%20%2Du,is%20often%20highly%20desirable%20behavior.
|
||||||
shopt -s expand_aliases
|
shopt -s expand_aliases
|
||||||
|
|
||||||
|
|
||||||
if [[ $# -ne 2 ]]; then
|
if [[ $# -ne 2 ]]; then
|
||||||
echo "Usage: $0 \"application name\" \"file name\"."
|
echo "Usage: $0 \"application name\" \"file name\"."
|
||||||
exit 1
|
exit 1
|
||||||
@ -22,6 +21,40 @@ fileName="${2,,}"
|
|||||||
fileName="${fileName//[[:space:]]/-}.sqlite"
|
fileName="${fileName//[[:space:]]/-}.sqlite"
|
||||||
translationFile="${XDG_CACHE_HOME:-$HOME/.cache}/audiogame-manager/${fileName}"
|
translationFile="${XDG_CACHE_HOME:-$HOME/.cache}/audiogame-manager/${fileName}"
|
||||||
|
|
||||||
|
# Make sure the directory exists
|
||||||
|
mkdir -p "$(dirname "$translationFile")"
|
||||||
|
|
||||||
|
# Initialize database if it doesn't exist
|
||||||
|
if [[ ! -s "$translationFile" ]]; then
|
||||||
|
rm -f "$translationFile"
|
||||||
|
sqlite3 "$translationFile" <<EOF
|
||||||
|
CREATE TABLE translations(
|
||||||
|
text TEXT NOT NULL,
|
||||||
|
translation TEXT NOT NULL,
|
||||||
|
PRIMARY KEY (text)
|
||||||
|
);
|
||||||
|
CREATE INDEX translations_text_idx ON translations (text);
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Define a function to safely query the database
|
||||||
|
query_database() {
|
||||||
|
local db_file="$1"
|
||||||
|
local sql_query="$2"
|
||||||
|
sqlite3 -line "$db_file" "$sql_query"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Define a function to safely insert into the database
|
||||||
|
insert_database() {
|
||||||
|
local db_file="$1"
|
||||||
|
local text="$2"
|
||||||
|
local translation="$3"
|
||||||
|
|
||||||
|
# Use sqlite3 .import feature which is more robust for special characters
|
||||||
|
echo "$text|$translation" | sqlite3 -separator "|" "$db_file" ".import /dev/stdin temp_import"
|
||||||
|
sqlite3 "$db_file" "INSERT OR IGNORE INTO translations SELECT * FROM temp_import; DROP TABLE IF EXISTS temp_import;"
|
||||||
|
}
|
||||||
|
|
||||||
# Read so long as the application is running
|
# Read so long as the application is running
|
||||||
while pgrep -u "$USER" ^$1 &> /dev/null ; do
|
while pgrep -u "$USER" ^$1 &> /dev/null ; do
|
||||||
sleep 0.05
|
sleep 0.05
|
||||||
@ -37,24 +70,6 @@ while pgrep -u "$USER" ^$1 &> /dev/null ; do
|
|||||||
if [[ -z "$text" ]]; then
|
if [[ -z "$text" ]]; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
q() { sqlite3 -column -init "" "$translationFile" "$@" 2> /dev/null ; }
|
|
||||||
|
|
||||||
|
|
||||||
if [[ ! -s "$translationFile" ]]; then
|
|
||||||
rm -f "$translationFile"
|
|
||||||
cat << EOF | sqlite3 -init "" "$translationFile" 2> /dev/null
|
|
||||||
.bail on
|
|
||||||
|
|
||||||
CREATE TABLE translations(
|
|
||||||
text TEXT NOT NULL,
|
|
||||||
translation TEXT NOT NULL,
|
|
||||||
|
|
||||||
PRIMARY KEY (text)
|
|
||||||
);
|
|
||||||
CREATE INDEX translations_text_idx ON translations (text);
|
|
||||||
EOF
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# https://en.wikipedia.org/wiki/Unicode_equivalence#Combining_and_precomposed_characters
|
# https://en.wikipedia.org/wiki/Unicode_equivalence#Combining_and_precomposed_characters
|
||||||
# https://www.effectiveperlprogramming.com/2011/09/normalize-your-perl-source/
|
# https://www.effectiveperlprogramming.com/2011/09/normalize-your-perl-source/
|
||||||
@ -65,18 +80,32 @@ EOF
|
|||||||
alias normalize_spaces="perl -CSDA -plE 's/[^\\S\\t]/ /g'"
|
alias normalize_spaces="perl -CSDA -plE 's/[^\\S\\t]/ /g'"
|
||||||
alias normalize_unicode="normalize_spaces | nfc"
|
alias normalize_unicode="normalize_spaces | nfc"
|
||||||
|
|
||||||
textEscaped="$(echo "$text" | sed "s/'/''/g" | normalize_unicode)" # escape single quotes for sqlite
|
# Normalize text
|
||||||
translated="$(q "SELECT translation FROM translations WHERE text = '$textEscaped' LIMIT 1" | sed 's/\s*$//')"
|
normalized_text="$(echo "$text" | normalize_unicode)"
|
||||||
|
|
||||||
|
# Create a temporary database for import
|
||||||
|
sqlite3 "$translationFile" "CREATE TABLE IF NOT EXISTS temp_import(text TEXT, translation TEXT);"
|
||||||
|
|
||||||
|
# Check if we already have a translation
|
||||||
|
translated=$(sqlite3 "$translationFile" "SELECT translation FROM translations WHERE text = '$normalized_text' LIMIT 1;" 2>/dev/null)
|
||||||
|
|
||||||
if [[ -z "$translated" ]]; then
|
if [[ -z "$translated" ]]; then
|
||||||
translated="$(trans -no-autocorrect -no-warn -brief "$text" | head -1 | sed 's/\s*$//' | normalize_unicode)"
|
# Get translation from the trans utility
|
||||||
|
translated="$(trans -no-autocorrect -no-warn -brief "$normalized_text" | head -1 | sed 's/\s*$//' | normalize_unicode)"
|
||||||
|
|
||||||
if [[ -n "$translated" ]]; then
|
if [[ -n "$translated" ]]; then
|
||||||
translatedEscaped="$(echo "$translated" | sed "s/'/''/g")"
|
# Insert using echo piping to avoid escaping issues
|
||||||
q "INSERT OR IGNORE INTO translations (text, translation) VALUES ('$textEscaped', '$translatedEscaped')"
|
echo "$normalized_text|$translated" | sqlite3 -separator "|" "$translationFile" ".import /dev/stdin temp_import"
|
||||||
|
sqlite3 "$translationFile" "INSERT OR IGNORE INTO translations SELECT * FROM temp_import; DELETE FROM temp_import;"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
spd-say -- "$translated"
|
# If we got a translation, speak it
|
||||||
|
if [[ -n "$translated" ]]; then
|
||||||
|
spd-say -- "$translated"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Clear clipboard
|
||||||
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
|
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
|
||||||
done
|
done
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user