mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-07-09 17:56:53 +00:00
Update utils.sh
This commit is contained in:
parent
96c08e6563
commit
8139eb607c
@ -217,12 +217,12 @@ load_language() {
|
|||||||
########################################################
|
########################################################
|
||||||
|
|
||||||
|
|
||||||
# Translation with cache and predefined terms
|
|
||||||
translate_() {
|
translate() {
|
||||||
local text="$1"
|
local text="$1"
|
||||||
local dest_lang="$LANGUAGE"
|
local dest_lang="$LANGUAGE"
|
||||||
|
|
||||||
# If the language is English, return the original text without translating or caching
|
|
||||||
if [ "$dest_lang" = "en" ]; then
|
if [ "$dest_lang" = "en" ]; then
|
||||||
echo "$text"
|
echo "$text"
|
||||||
return
|
return
|
||||||
@ -293,76 +293,6 @@ print(translate_text('$text', '$dest_lang'))
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
translate() {
|
|
||||||
local text="$1"
|
|
||||||
local dest_lang="$LANGUAGE"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
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()
|
|
||||||
# El contexto solo se añade aquí
|
|
||||||
context = 'Context: sysadmin script translation. '
|
|
||||||
try:
|
|
||||||
result = translator.translate(context + text, dest=dest_lang).text
|
|
||||||
# Remueve el contexto en el texto traducido
|
|
||||||
translated = re.sub(r'^(Context: sysadmin script translation\. )?', '', 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')
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
########################################################
|
########################################################
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user