From 96c08e6563b8be2f0151dd77c6cf80ae1eaf4e94 Mon Sep 17 00:00:00 2001 From: MacRimi Date: Sat, 5 Jul 2025 11:52:20 +0200 Subject: [PATCH] Update utils.sh --- scripts/utils.sh | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/scripts/utils.sh b/scripts/utils.sh index 386235f..7611ada 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -298,28 +298,25 @@ 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 " @@ -328,12 +325,12 @@ import sys, json, re def translate_text(text, dest_lang): translator = Translator() - # Context is not strictly necessary, but if you want it: - context = '###CONTEXT### ' + # El contexto solo se añade aquí + context = 'Context: sysadmin script translation. ' try: result = translator.translate(context + text, dest=dest_lang).text - # Remove context marker if present - translated = re.sub(r'^.*###CONTEXT### ?', '', result).strip() + # 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)}) @@ -348,8 +345,6 @@ print(translate_text('$text', '$dest_lang')) 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) @@ -358,7 +353,7 @@ print(translate_text('$text', '$dest_lang')) .[$text][$lang] = $translated ' "$CACHE_FILE" > "$temp_cache" && mv "$temp_cache" "$CACHE_FILE" fi - + echo "$translated" else echo "$text" @@ -367,6 +362,7 @@ print(translate_text('$text', '$dest_lang')) + ########################################################