diff --git a/scripts/extract-to-translations.ts b/scripts/extract-to-translations.ts new file mode 100644 index 0000000..103fcec --- /dev/null +++ b/scripts/extract-to-translations.ts @@ -0,0 +1,87 @@ +#!/usr/bin/env node +/** + * Translation Extraction Helper + * Helps identify and extract hardcoded strings from Svelte files + */ + +import { readFileSync, writeFileSync } from 'fs'; +import { join } from 'path'; + +interface StringMatch { + line: number; + original: string; + suggested Key: string; + context: string; +} + +/** + * Extract hardcoded strings from a file and suggest translation keys + */ +function extractStrings(filePath: string): StringMatch[] { + const content = readFileSync(filePath, 'utf-8'); + const lines = content.split('\n'); + const matches: StringMatch[] = []; + + const patterns = [ + // String in quotes + /'([^']{5,})'/g, + /"([^"]{5,})"/g, + // JSX text content + />([A-Z][^<>{}]{4,})'); + process.exit(1); +} + +const filePath = args[0]; +const matches = extractStrings(filePath); + +console.log(`\nFound ${matches.length} potential strings in ${filePath}\n`); + +for (const match of matches.slice(0, 20)) { + console.log(`Line ${match.line}: "${match.original}"`); + console.log(` Suggested key: ${match.suggestedKey}`); + console.log(` Context: ${match.context}\n`); +} + +if (matches.length > 20) { + console.log(`... and ${matches.length - 20} more`); +} diff --git a/scripts/find-hardcoded-text.ts b/scripts/find-hardcoded-text.ts index 9c30abb..3e1dd6e 100644 --- a/scripts/find-hardcoded-text.ts +++ b/scripts/find-hardcoded-text.ts @@ -153,6 +153,11 @@ function scanDirectory(dirPath: string): HardcodedText[] { continue; } + // Validate safeName to prevent path traversal and unsafe characters + if (safeName.includes('..') || /[^a-zA-Z0-9._-]/.test(safeName)) { + console.error(`Unsafe file or directory name blocked: ${safeName}`); + continue; + } // Construct path and verify it stays within PROJECT_ROOT const fullPath = join(dirPath, safeName); if (!fullPath.startsWith(PROJECT_ROOT)) {