|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to translate a keyword into all CTFd available languages and add to a plugin's translations.json |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python translate_keyword.py <keyword> <plugin_name> |
| 7 | +
|
| 8 | +Examples: |
| 9 | + python translate_keyword.py "Challenge Created" MyPlugin |
| 10 | + python translate_keyword.py "Challenge Created" /path/to/plugin # Full path also works |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import json |
| 15 | +import os |
| 16 | +import sys |
| 17 | + |
| 18 | +# Try to import translation library |
| 19 | +try: |
| 20 | + from deep_translator import GoogleTranslator |
| 21 | + HAS_TRANSLATOR = True |
| 22 | +except ImportError: |
| 23 | + HAS_TRANSLATOR = False |
| 24 | + |
| 25 | +# Import CTFd's language constants |
| 26 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..')) |
| 27 | + |
| 28 | + |
| 29 | +Languages = { |
| 30 | + "en": "English", |
| 31 | + "de": "Deutsch", |
| 32 | + "pl": "Polski", |
| 33 | + "es": "Español", |
| 34 | + "ar": "اَلْعَرَبِيَّةُ", |
| 35 | + "zh_CN": "简体中文", |
| 36 | + "zh_TW": "繁體中文", |
| 37 | + "fr": "Français", |
| 38 | + "ko": "한국어", |
| 39 | + "ru": "русский язык", |
| 40 | + "pt_BR": "Português do Brasil", |
| 41 | + "sk": "Slovenský jazyk", |
| 42 | + "ja": "日本語", |
| 43 | + "it": "Italiano", |
| 44 | + "vi": "tiếng Việt", |
| 45 | + "ca": "Català", |
| 46 | + "el": "Ελληνικά", |
| 47 | + "fi": "Suomi", |
| 48 | + "ro": "Română", |
| 49 | + "sl": "Slovenščina", |
| 50 | + "sv": "Svenska", |
| 51 | + "he": "עברית", |
| 52 | + "uz": "oʻzbekcha", |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +def translate_keyword(keyword, target_lang): |
| 57 | + """Translate keyword to target language using available service.""" |
| 58 | + try: |
| 59 | + translator = GoogleTranslator(source='auto', target=target_lang) |
| 60 | + result = translator.translate(keyword) |
| 61 | + return result |
| 62 | + except Exception as e: |
| 63 | + print(f"Warning: Translation failed for {target_lang}: {e}") |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +def load_translations_file(plugin_dir): |
| 69 | + """Load existing translations from plugin's translations.json""" |
| 70 | + translations_path = os.path.join(plugin_dir, 'translations.json') |
| 71 | + if os.path.exists(translations_path): |
| 72 | + try: |
| 73 | + with open(translations_path, 'r', encoding='utf-8') as f: |
| 74 | + return json.load(f) |
| 75 | + except json.JSONDecodeError: |
| 76 | + return {} |
| 77 | + return {} |
| 78 | + |
| 79 | + |
| 80 | +def resolve_plugin_dir(plugin_input): |
| 81 | + """Resolve plugin name or path to absolute plugin directory. |
| 82 | + |
| 83 | + Args: |
| 84 | + plugin_input: Either a plugin name (e.g., 'MyPlugin') or full path |
| 85 | + |
| 86 | + Returns: |
| 87 | + Absolute path to plugin directory |
| 88 | + """ |
| 89 | + # If it's an absolute path or looks like a path (contains / or \), use it as-is |
| 90 | + if os.path.isabs(plugin_input) or '/' in plugin_input or '\\' in plugin_input: |
| 91 | + return os.path.abspath(plugin_input) |
| 92 | + |
| 93 | + # Otherwise, treat it as a plugin name and resolve relative to CTFd/plugins/ |
| 94 | + luautils_dir = os.path.dirname(__file__) |
| 95 | + plugins_dir = os.path.dirname(luautils_dir) |
| 96 | + plugin_dir = os.path.join(plugins_dir, plugin_input) |
| 97 | + return os.path.abspath(plugin_dir) |
| 98 | + |
| 99 | + |
| 100 | +def save_translations_file(plugin_dir, translations): |
| 101 | + """Save translations to plugin's translations.json""" |
| 102 | + translations_path = os.path.join(plugin_dir, 'translations.json') |
| 103 | + os.makedirs(plugin_dir, exist_ok=True) |
| 104 | + with open(translations_path, 'w', encoding='utf-8') as f: |
| 105 | + json.dump(translations, f, ensure_ascii=False, indent=2) |
| 106 | + |
| 107 | + |
| 108 | +def main(): |
| 109 | + parser = argparse.ArgumentParser( |
| 110 | + description='Translate a keyword into all CTFd available languages' |
| 111 | + ) |
| 112 | + parser.add_argument('keyword', help='The keyword to translate') |
| 113 | + parser.add_argument('plugin_name', help='Plugin name (e.g., MyPlugin) or full path') |
| 114 | + parser.add_argument( |
| 115 | + '--key', |
| 116 | + help='Translation key (defaults to lowercase keyword with underscores)' |
| 117 | + ) |
| 118 | + parser.add_argument( |
| 119 | + '--no-save', |
| 120 | + action='store_true', |
| 121 | + help='Print translations without saving' |
| 122 | + ) |
| 123 | + |
| 124 | + args = parser.parse_args() |
| 125 | + |
| 126 | + keyword = args.keyword |
| 127 | + plugin_input = args.plugin_name |
| 128 | + translation_key = args.key or keyword.lower().replace(' ', '_') |
| 129 | + |
| 130 | + # Resolve plugin directory |
| 131 | + plugin_dir = resolve_plugin_dir(plugin_input) |
| 132 | + |
| 133 | + # Validate plugin directory |
| 134 | + if not os.path.isdir(plugin_dir): |
| 135 | + print(f"Error: Plugin directory not found: {plugin_dir}") |
| 136 | + sys.exit(1) |
| 137 | + |
| 138 | + # Check if translator is available |
| 139 | + if not HAS_TRANSLATOR: |
| 140 | + print("Error: deep-translator is required but not installed.") |
| 141 | + print("Install with: pip install deep-translator") |
| 142 | + sys.exit(1) |
| 143 | + |
| 144 | + translations = {} |
| 145 | + failed_langs = [] |
| 146 | + |
| 147 | + # Translate to each language |
| 148 | + for lang_code, lang_name in Languages.items(): |
| 149 | + if lang_code == 'en': |
| 150 | + # English is the source, use the keyword as-is |
| 151 | + translation = keyword |
| 152 | + else: |
| 153 | + translation = translate_keyword(keyword, lang_code) |
| 154 | + if translation is None: |
| 155 | + failed_langs.append(lang_code) |
| 156 | + continue |
| 157 | + |
| 158 | + if lang_code not in translations: |
| 159 | + translations[lang_code] = {} |
| 160 | + translations[lang_code][translation_key] = translation |
| 161 | + |
| 162 | + if failed_langs: |
| 163 | + print(f"Warning: Failed to translate to: {', '.join(failed_langs)}") |
| 164 | + |
| 165 | + # Save if not --no-save |
| 166 | + if not args.no_save: |
| 167 | + # Load existing translations and merge |
| 168 | + existing = load_translations_file(plugin_dir) |
| 169 | + |
| 170 | + # Merge new translations with existing |
| 171 | + for lang_code, new_terms in translations.items(): |
| 172 | + if lang_code not in existing: |
| 173 | + existing[lang_code] = {} |
| 174 | + existing[lang_code].update(new_terms) |
| 175 | + |
| 176 | + save_translations_file(plugin_dir, existing) |
| 177 | + print(f"Saved to {os.path.join(plugin_dir, 'translations.json')}") |
| 178 | + |
| 179 | +if __name__ == '__main__': |
| 180 | + main() |
0 commit comments