|
| 1 | +// utils/i18n.js |
| 2 | +/* global chrome */ |
| 3 | +// Simple runtime internationalisation handler for CodeTweak UI pages. |
| 4 | +// Looks up visible English strings and attributes and swaps them with |
| 5 | +// the user-locale translation using chrome.i18n. |
| 6 | +// Only strings present in the map will be translated. |
| 7 | + |
| 8 | +(function () { |
| 9 | + if (!('i18n' in chrome)) { |
| 10 | + return; // Not running inside extension context |
| 11 | + } |
| 12 | + |
| 13 | + // Map of English UI strings -> i18n message keys. |
| 14 | + const textToKey = { |
| 15 | + 'Save Script': 'save_script', |
| 16 | + 'Unsaved Changes': 'unsaved_changes', |
| 17 | + 'Script Information': 'script_information', |
| 18 | + 'GM API Access': 'gm_api_access', |
| 19 | + 'Required Scripts': 'required_scripts', |
| 20 | + 'Script Resources': 'script_resources', |
| 21 | + 'Execution Settings': 'execution_settings', |
| 22 | + 'Script Name': 'script_name', |
| 23 | + 'Author': 'author', |
| 24 | + 'Version': 'version', |
| 25 | + 'Description': 'description', |
| 26 | + 'License': 'license', |
| 27 | + 'Target URLs': 'target_urls', |
| 28 | + 'Pattern Builder': 'pattern_builder', |
| 29 | + 'Run Timing': 'run_timing', |
| 30 | + 'Generate': 'generate', |
| 31 | + 'Insert': 'insert', |
| 32 | + 'Help & Shortcuts': 'help_shortcuts', |
| 33 | + 'Editor Settings': 'editor_settings', |
| 34 | + 'Dashboard': 'dashboard', |
| 35 | + 'Create': 'create', |
| 36 | + 'Browse Scripts': 'browse_scripts', |
| 37 | + 'New Script': 'new_script', |
| 38 | + 'Scripts': 'scripts_tab', |
| 39 | + 'Settings': 'settings_tab', |
| 40 | + 'About': 'about_tab' |
| 41 | + }; |
| 42 | + |
| 43 | + function translateNodeText(node) { |
| 44 | + const orig = node.textContent.trim(); |
| 45 | + const key = textToKey[orig]; |
| 46 | + if (key) { |
| 47 | + const msg = chrome.i18n.getMessage(key); |
| 48 | + if (msg) node.textContent = msg; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + function translateAttributes(el, attr) { |
| 53 | + const val = el.getAttribute(attr); |
| 54 | + if (!val) return; |
| 55 | + const key = textToKey[val.trim()]; |
| 56 | + if (key) { |
| 57 | + const msg = chrome.i18n.getMessage(key); |
| 58 | + if (msg) el.setAttribute(attr, msg); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + function runTranslation() { |
| 63 | + // Translate text content of all text nodes |
| 64 | + const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null); |
| 65 | + let n; |
| 66 | + while ((n = walker.nextNode())) { |
| 67 | + translateNodeText(n); |
| 68 | + } |
| 69 | + |
| 70 | + // Translate common attributes |
| 71 | + document.querySelectorAll('[title], [placeholder], [aria-label]').forEach((el) => { |
| 72 | + ['title', 'placeholder', 'aria-label'].forEach((attr) => translateAttributes(el, attr)); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + document.addEventListener('DOMContentLoaded', runTranslation); |
| 77 | +})(); |
0 commit comments