mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-07-07 15:56:53 +00:00
Update utils.sh
This commit is contained in:
parent
230847dace
commit
0584081c33
@ -212,8 +212,13 @@ load_language() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
|
||||||
# Translation with cache and predefined terms
|
# Translation with cache and predefined terms
|
||||||
translate() {
|
translate_() {
|
||||||
local text="$1"
|
local text="$1"
|
||||||
local dest_lang="$LANGUAGE"
|
local dest_lang="$LANGUAGE"
|
||||||
|
|
||||||
@ -288,6 +293,85 @@ print(translate_text('$text', '$dest_lang'))
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
translate() {
|
||||||
|
local text="$1"
|
||||||
|
local dest_lang="$LANGUAGE"
|
||||||
|
|
||||||
|
# If the language is English, return the original text without translating or caching
|
||||||
|
if [ "$dest_lang" = "en" ]; then
|
||||||
|
echo "$text"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -s "$CACHE_FILE" ] || ! jq -e . "$CACHE_FILE" > /dev/null 2>&1; then
|
||||||
|
echo "{}" > "$CACHE_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local cached_translation=$(jq -r --arg text "$text" --arg lang "$dest_lang" '.[$text][$lang] // .[$text]["notranslate"] // empty' "$CACHE_FILE")
|
||||||
|
if [ -n "$cached_translation" ]; then
|
||||||
|
echo "$cached_translation"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Robust check: skip python if venv or googletrans not ready
|
||||||
|
if [ ! -d "$VENV_PATH" ] || [ ! -f "$VENV_PATH/bin/activate" ] || ! "$VENV_PATH/bin/python3" -c "import googletrans" 2>/dev/null; then
|
||||||
|
echo "$text"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
source "$VENV_PATH/bin/activate"
|
||||||
|
local translated
|
||||||
|
translated=$(python3 -c "
|
||||||
|
from googletrans import Translator
|
||||||
|
import sys, json, re
|
||||||
|
|
||||||
|
def translate_text(text, dest_lang):
|
||||||
|
translator = Translator()
|
||||||
|
# Context is not strictly necessary, but if you want it:
|
||||||
|
context = '###CONTEXT### '
|
||||||
|
try:
|
||||||
|
result = translator.translate(context + text, dest=dest_lang).text
|
||||||
|
# Remove context marker if present
|
||||||
|
translated = re.sub(r'^.*###CONTEXT### ?', '', result).strip()
|
||||||
|
return json.dumps({'success': True, 'text': translated})
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({'success': False, 'error': str(e)})
|
||||||
|
|
||||||
|
print(translate_text('$text', '$dest_lang'))
|
||||||
|
")
|
||||||
|
deactivate
|
||||||
|
|
||||||
|
local translation_result=$(echo "$translated" | jq -r '.')
|
||||||
|
local success=$(echo "$translation_result" | jq -r '.success')
|
||||||
|
|
||||||
|
if [ "$success" = "true" ]; then
|
||||||
|
translated=$(echo "$translation_result" | jq -r '.text')
|
||||||
|
|
||||||
|
# Post-process: Remove context marker again, just in case
|
||||||
|
translated=$(echo "$translated" | sed -E 's/^.*###CONTEXT### ?//g')
|
||||||
|
|
||||||
|
if [ "$dest_lang" != "en" ]; then
|
||||||
|
local temp_cache=$(mktemp)
|
||||||
|
jq --arg text "$text" --arg lang "$dest_lang" --arg translated "$translated" '
|
||||||
|
if .[$text] == null then .[$text] = {} else . end |
|
||||||
|
.[$text][$lang] = $translated
|
||||||
|
' "$CACHE_FILE" > "$temp_cache" && mv "$temp_cache" "$CACHE_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$translated"
|
||||||
|
else
|
||||||
|
echo "$text"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
show_proxmenux_logo() {
|
show_proxmenux_logo() {
|
||||||
clear
|
clear
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user