forked from Shinonome/dots-hyprland
translation tools: add -y/--yes flag
This commit is contained in:
@@ -23,6 +23,7 @@ show_help() {
|
|||||||
echo " -l, --lang LANG Specify language (e.g.: zh_CN)"
|
echo " -l, --lang LANG Specify language (e.g.: zh_CN)"
|
||||||
echo " -t, --trans-dir DIR Translation files directory (default: $TRANSLATIONS_DIR)"
|
echo " -t, --trans-dir DIR Translation files directory (default: $TRANSLATIONS_DIR)"
|
||||||
echo " -s, --source-dir DIR Source code directory (default: $SOURCE_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 " -h, --help Show this help message"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Examples:"
|
echo "Examples:"
|
||||||
@@ -63,6 +64,7 @@ show_status() {
|
|||||||
# Parse command line arguments
|
# Parse command line arguments
|
||||||
LANG_CODE=""
|
LANG_CODE=""
|
||||||
COMMAND=""
|
COMMAND=""
|
||||||
|
YES_FLAG=""
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case $1 in
|
case $1 in
|
||||||
@@ -78,6 +80,10 @@ while [[ $# -gt 0 ]]; do
|
|||||||
SOURCE_DIR="$2"
|
SOURCE_DIR="$2"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
|
-y|--yes)
|
||||||
|
YES_FLAG="-y"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
-h|--help)
|
-h|--help)
|
||||||
show_help
|
show_help
|
||||||
exit 0
|
exit 0
|
||||||
@@ -120,23 +126,23 @@ BASE_ARGS="--translations-dir $TRANSLATIONS_DIR --source-dir $SOURCE_DIR"
|
|||||||
case $COMMAND in
|
case $COMMAND in
|
||||||
extract)
|
extract)
|
||||||
echo "Extracting translatable texts..."
|
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)
|
update)
|
||||||
echo "Updating translation files..."
|
echo "Updating translation files..."
|
||||||
if [ -n "$LANG_CODE" ]; then
|
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
|
else
|
||||||
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS
|
python3 "$SCRIPT_DIR/translation-manager.py" $BASE_ARGS $YES_FLAG
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
clean)
|
clean)
|
||||||
echo "Cleaning unused translation keys..."
|
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)
|
sync)
|
||||||
echo "Syncing translation keys..."
|
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)
|
status)
|
||||||
show_status
|
show_status
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ translation_manager = importlib.util.module_from_spec(spec)
|
|||||||
spec.loader.exec_module(translation_manager)
|
spec.loader.exec_module(translation_manager)
|
||||||
TranslationManager = translation_manager.TranslationManager
|
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"""
|
"""Clean translation files by removing unused keys"""
|
||||||
print("Starting translation file cleanup...")
|
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:
|
if len(unused_keys) > 10:
|
||||||
print(f" ... and {len(unused_keys) - 10} more keys")
|
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 response.lower().strip() in ['y', 'yes']:
|
||||||
if backup:
|
if backup:
|
||||||
# Create backup only when user confirms deletion
|
# 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.")
|
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"""
|
"""Sync translation keys to ensure all language files have the same keys"""
|
||||||
print(f"Starting translation key sync using {source_lang} as reference...")
|
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
|
# Ask whether to delete extra keys
|
||||||
if 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']:
|
if response.lower().strip() in ['y', 'yes']:
|
||||||
for key in extra_keys:
|
for key in extra_keys:
|
||||||
del target_translations[key]
|
del target_translations[key]
|
||||||
@@ -180,6 +188,8 @@ def main():
|
|||||||
help="Source language for syncing (default: en_US)")
|
help="Source language for syncing (default: en_US)")
|
||||||
parser.add_argument("--no-backup", action="store_true",
|
parser.add_argument("--no-backup", action="store_true",
|
||||||
help="Do not create backup files when cleaning")
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -188,9 +198,9 @@ def main():
|
|||||||
source_dir = os.path.abspath(args.source_dir)
|
source_dir = os.path.abspath(args.source_dir)
|
||||||
|
|
||||||
if args.clean:
|
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:
|
elif args.sync:
|
||||||
sync_translations(translations_dir, args.source_lang)
|
sync_translations(translations_dir, args.source_lang, yes_mode=args.yes)
|
||||||
else:
|
else:
|
||||||
print("Please specify an operation:")
|
print("Please specify an operation:")
|
||||||
print(" --clean: Clean unused translation keys")
|
print(" --clean: Clean unused translation keys")
|
||||||
|
|||||||
@@ -16,10 +16,11 @@ import tempfile
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
class TranslationManager:
|
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.translations_dir = Path(translations_dir)
|
||||||
self.source_dir = Path(source_dir)
|
self.source_dir = Path(source_dir)
|
||||||
self.temp_extracted_file = None
|
self.temp_extracted_file = None
|
||||||
|
self.yes_mode = yes_mode
|
||||||
|
|
||||||
# Ensure translation directory exists
|
# Ensure translation directory exists
|
||||||
self.translations_dir.mkdir(parents=True, exist_ok=True)
|
self.translations_dir.mkdir(parents=True, exist_ok=True)
|
||||||
@@ -191,7 +192,10 @@ class TranslationManager:
|
|||||||
print("No changes made")
|
print("No changes made")
|
||||||
|
|
||||||
def ask_yes_no(self, question: str) -> bool:
|
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:
|
while True:
|
||||||
response = input(f"{question} (y/n): ").lower().strip()
|
response = input(f"{question} (y/n): ").lower().strip()
|
||||||
if response in ['y', 'yes']:
|
if response in ['y', 'yes']:
|
||||||
@@ -220,6 +224,8 @@ def main():
|
|||||||
help="Only extract translatable texts to temporary file")
|
help="Only extract translatable texts to temporary file")
|
||||||
parser.add_argument("--show-temp", action="store_true",
|
parser.add_argument("--show-temp", action="store_true",
|
||||||
help="Show temporary extracted file content")
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -236,7 +242,7 @@ def main():
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Create manager
|
# Create manager
|
||||||
manager = TranslationManager(translations_dir, source_dir)
|
manager = TranslationManager(translations_dir, source_dir, yes_mode=args.yes)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Extract translatable texts
|
# Extract translatable texts
|
||||||
@@ -264,9 +270,10 @@ def main():
|
|||||||
target_languages = [args.language]
|
target_languages = [args.language]
|
||||||
else:
|
else:
|
||||||
print(f"\nAvailable languages: {', '.join(available_languages) if available_languages else 'None'}")
|
print(f"\nAvailable languages: {', '.join(available_languages) if available_languages else 'None'}")
|
||||||
|
|
||||||
if not available_languages:
|
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()
|
lang_input = input("Enter language code to create (e.g.: zh_CN): ").strip()
|
||||||
if lang_input:
|
if lang_input:
|
||||||
target_languages = [lang_input]
|
target_languages = [lang_input]
|
||||||
@@ -278,8 +285,11 @@ def main():
|
|||||||
for i, lang in enumerate(available_languages, 1):
|
for i, lang in enumerate(available_languages, 1):
|
||||||
print(f"{i}. {lang}")
|
print(f"{i}. {lang}")
|
||||||
print("a. Process all languages")
|
print("a. Process all languages")
|
||||||
|
if manager.yes_mode:
|
||||||
choice = input("Please choose (enter number, language code, or 'a'): ").strip()
|
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':
|
if choice.lower() == 'a':
|
||||||
target_languages = available_languages
|
target_languages = available_languages
|
||||||
|
|||||||
Reference in New Issue
Block a user