ai-i18n-translator
Version:
AI-powered i18n auto-translation tool with pluggable extractors, translators, and organizers
118 lines (102 loc) ⢠3.29 kB
JavaScript
#!/usr/bin/env node
/**
* CLI entry point for ai-i18n-translator
*/
import TranslationManager from './auto-translate.js';
import path from 'path';
import fs from 'fs';
function showHelp() {
console.log(`
š AI i18n Translator
Usage: ai-i18n [options]
Options:
-f, --file <path> Only translate keys from the specified file
-h, --help Show this help message
Examples:
ai-i18n # Translate all files
ai-i18n --file src/circle.ts # Only translate keys from circle.ts
ai-i18n -f "src/components/*.tsx" # Only translate keys from matching files
Configuration:
Create auto-translate.config.js in your project root.
`);
}
async function main() {
console.log('š AI i18n Translator\n');
// Parse command line arguments
const args = process.argv.slice(2);
let targetFile = null;
for (let i = 0; i < args.length; i++) {
if (args[i] === '--file' || args[i] === '-f') {
targetFile = args[i + 1];
if (!targetFile) {
console.error('ā Error: --file/-f option requires a file path');
process.exit(1);
}
i++; // Skip next argument as it's the file path
} else if (args[i] === '--help' || args[i] === '-h') {
showHelp();
process.exit(0);
}
}
if (targetFile) {
console.log(`šÆ Target file: ${targetFile}`);
}
// Check for config file in multiple locations
const configNames = [
'auto-translate.config.mjs',
'auto-translate.config.cjs',
'auto-translate.config.js',
'i18n.config.mjs',
'i18n.config.cjs',
'i18n.config.js'
];
let configFound = false;
for (const configName of configNames) {
const configPath = path.join(process.cwd(), configName);
if (fs.existsSync(configPath)) {
configFound = true;
break;
}
}
if (!configFound) {
console.log('ā ļø No configuration file found in current directory');
console.log('\nš Create one of these config files:');
console.log(' - auto-translate.config.mjs (ES module)');
console.log(' - auto-translate.config.cjs (CommonJS)');
console.log(' - auto-translate.config.js (auto-detected)');
console.log('\nš§ Example configurations:');
console.log('\nES Module (use .mjs or set "type": "module" in package.json):');
console.log(`export default {
extensions: ['.tsx', '.ts', '.jsx', '.js'],
excludedDirs: ['node_modules', '.git', 'dist'],
messagesDir: 'messages',
supportedLocales: ['en', 'vi'],
sourceLocale: 'en'
}`);
console.log('\nCommonJS (use .cjs or default for Node.js):');
console.log(`module.exports = {
extensions: ['.tsx', '.ts', '.jsx', '.js'],
excludedDirs: ['node_modules', '.git', 'dist'],
messagesDir: 'messages',
supportedLocales: ['en', 'vi'],
sourceLocale: 'en'
}`);
process.exit(1);
}
try {
// Create and run translation manager
const manager = new TranslationManager(undefined, { targetFile });
await manager.run();
} catch (error) {
console.error('ā Error:', error.message);
if (error.stack) {
console.error('\nStack trace:', error.stack);
}
process.exit(1);
}
}
// Run the CLI
main().catch(error => {
console.error('ā Unexpected error:', error);
process.exit(1);
});