translation tools: add -y/--yes flag

This commit is contained in:
end-4
2025-10-14 12:22:16 +02:00
parent 57a5c0f743
commit c9f5397821
3 changed files with 44 additions and 18 deletions
@@ -23,6 +23,7 @@ show_help() {
echo " -l, --lang LANG Specify language (e.g.: zh_CN)"
echo " -t, --trans-dir DIR Translation files directory (default: $TRANSLATIONS_DIR)"
echo " -s, --source-dir DIR Source code directory (default: $SOURCE_DIR)"
echo " -y, --yes Skip all confirmation prompts (auto-confirm)"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
@@ -63,6 +64,7 @@ show_status() {
# Parse command line arguments
LANG_CODE=""
COMMAND=""
YES_FLAG=""
while [[ $# -gt 0 ]]; do
case $1 in
@@ -78,6 +80,10 @@ while [[ $# -gt 0 ]]; do
SOURCE_DIR="$2"
shift 2
;;
-y|--yes)
YES_FLAG="-y"
shift
;;
-h|--help)
show_help
exit 0
@@ -120,23 +126,23 @@ BASE_ARGS="--translations-dir $TRANSLATIONS_DIR --source-dir $SOURCE_DIR"
case $COMMAND in
extract)
echo "Extracting translatable texts..."
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS --extract-only --show-temp
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS $YES_FLAG --extract-only --show-temp
;;
update)
echo "Updating translation files..."
if [ -n "$LANG_CODE" ]; then
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS --language "$LANG_CODE"
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS $YES_FLAG --language "$LANG_CODE"
else
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS $YES_FLAG
fi
;;
clean)
echo "Cleaning unused translation keys..."
python3 "$SCRIPT_DIR/translation-cleaner.py" $BASE_ARGS --clean
python3 "$SCRIPT_DIR/translation-cleaner.py" $BASE_ARGS $YES_FLAG --clean
;;
sync)
echo "Syncing translation keys..."
python3 "$SCRIPT_DIR/translation-cleaner.py" $BASE_ARGS --sync
python3 "$SCRIPT_DIR/translation-cleaner.py" $BASE_ARGS $YES_FLAG --sync
;;
status)
show_status
@@ -21,7 +21,7 @@ translation_manager = importlib.util.module_from_spec(spec)
spec.loader.exec_module(translation_manager)
TranslationManager = translation_manager.TranslationManager
def clean_translation_files(translations_dir: str, source_dir: str, backup: bool = True):
def clean_translation_files(translations_dir: str, source_dir: str, backup: bool = True, yes_mode: bool = False):
"""Clean translation files by removing unused keys"""
print("Starting translation file cleanup...")
@@ -66,7 +66,11 @@ def clean_translation_files(translations_dir: str, source_dir: str, backup: bool
if len(unused_keys) > 10:
print(f" ... and {len(unused_keys) - 10} more keys")
response = input(f"Delete these {len(unused_keys)} unused keys? (y/n): ")
if yes_mode:
response = 'y'
print(f"Delete these {len(unused_keys)} unused keys? (auto-confirmed by --yes)")
else:
response = input(f"Delete these {len(unused_keys)} unused keys? (y/n): ")
if response.lower().strip() in ['y', 'yes']:
if backup:
# Create backup only when user confirms deletion
@@ -93,7 +97,7 @@ def clean_translation_files(translations_dir: str, source_dir: str, backup: bool
print(f"\nCleanup completed! Total deleted {total_removed} unused keys.")
def sync_translations(translations_dir: str, source_lang: str = "en_US", target_langs: List[str] = None):
def sync_translations(translations_dir: str, source_lang: str = "en_US", target_langs: List[str] = None, yes_mode: bool = False):
"""Sync translation keys to ensure all language files have the same keys"""
print(f"Starting translation key sync using {source_lang} as reference...")
@@ -153,7 +157,11 @@ def sync_translations(translations_dir: str, source_lang: str = "en_US", target_
# Ask whether to delete extra keys
if extra_keys:
response = input(f" Delete {len(extra_keys)} extra keys? (y/n): ")
if yes_mode:
response = 'y'
print(f" Delete {len(extra_keys)} extra keys? (auto-confirmed by --yes)")
else:
response = input(f" Delete {len(extra_keys)} extra keys? (y/n): ")
if response.lower().strip() in ['y', 'yes']:
for key in extra_keys:
del target_translations[key]
@@ -180,6 +188,8 @@ def main():
help="Source language for syncing (default: en_US)")
parser.add_argument("--no-backup", action="store_true",
help="Do not create backup files when cleaning")
parser.add_argument("-y", "--yes", action="store_true",
help="Skip all confirmation prompts (auto-confirm)")
args = parser.parse_args()
@@ -188,9 +198,9 @@ def main():
source_dir = os.path.abspath(args.source_dir)
if args.clean:
clean_translation_files(translations_dir, source_dir, backup=not args.no_backup)
clean_translation_files(translations_dir, source_dir, backup=not args.no_backup, yes_mode=args.yes)
elif args.sync:
sync_translations(translations_dir, args.source_lang)
sync_translations(translations_dir, args.source_lang, yes_mode=args.yes)
else:
print("Please specify an operation:")
print(" --clean: Clean unused translation keys")
@@ -16,10 +16,11 @@ import tempfile
import subprocess
class TranslationManager:
def __init__(self, translations_dir: str, source_dir: str):
def __init__(self, translations_dir: str, source_dir: str, yes_mode: bool = False):
self.translations_dir = Path(translations_dir)
self.source_dir = Path(source_dir)
self.temp_extracted_file = None
self.yes_mode = yes_mode
# Ensure translation directory exists
self.translations_dir.mkdir(parents=True, exist_ok=True)
@@ -191,7 +192,10 @@ class TranslationManager:
print("No changes made")
def ask_yes_no(self, question: str) -> bool:
"""Ask user for confirmation"""
"""Ask user for confirmation, auto-confirm if yes_mode is True"""
if getattr(self, "yes_mode", False):
print(f"{question} (auto-confirmed by --yes)")
return True
while True:
response = input(f"{question} (y/n): ").lower().strip()
if response in ['y', 'yes']:
@@ -220,6 +224,8 @@ def main():
help="Only extract translatable texts to temporary file")
parser.add_argument("--show-temp", action="store_true",
help="Show temporary extracted file content")
parser.add_argument("-y", "--yes", action="store_true",
help="Skip all confirmation prompts (auto-confirm)")
args = parser.parse_args()
@@ -236,7 +242,7 @@ def main():
sys.exit(1)
# Create manager
manager = TranslationManager(translations_dir, source_dir)
manager = TranslationManager(translations_dir, source_dir, yes_mode=args.yes)
try:
# Extract translatable texts
@@ -264,9 +270,10 @@ def main():
target_languages = [args.language]
else:
print(f"\nAvailable languages: {', '.join(available_languages) if available_languages else 'None'}")
if not available_languages:
print("No existing translation files found")
if manager.yes_mode:
print("No existing translation files found, auto-skipping language creation due to --yes")
return
lang_input = input("Enter language code to create (e.g.: zh_CN): ").strip()
if lang_input:
target_languages = [lang_input]
@@ -278,8 +285,11 @@ def main():
for i, lang in enumerate(available_languages, 1):
print(f"{i}. {lang}")
print("a. Process all languages")
choice = input("Please choose (enter number, language code, or 'a'): ").strip()
if manager.yes_mode:
choice = 'a'
print("Auto-selecting all languages due to --yes")
else:
choice = input("Please choose (enter number, language code, or 'a'): ").strip()
if choice.lower() == 'a':
target_languages = available_languages