meetfun-i18n-cli
v1.0.25
Published
A powerful CLI tool for automatic internationalization of Vue.js and JavaScript/TypeScript projects
Maintainers
Readme
MeetFun i18n CLI
A powerful CLI tool for automatic internationalization of Vue.js and JavaScript/TypeScript projects.
Features
- 🚀 Automatic i18n conversion: Convert Chinese strings in Vue, JS and TS files to
$t()format - 🔍 Preview mode: Analyze files without making actual changes
- 📦 Support for multiple file types: Vue components, JavaScript and TypeScript files
- ⚙️ Flexible configuration: Customizable options for different needs
- 💻 Command-line interface: Easy to use in scripts or directly from the terminal
- 📚 Programmatic API: Can be integrated into other Node.js applications
- 🛠️ Vite plugin: Use as a Vite plugin during development
Installation
Global installation (for CLI use)
npm install -g meetfun-i18n-cliProject installation (as a dependency)
npm install --save-dev meetfun-i18n-cliUsage
CLI Usage
After global installation, you can use the meetfun-i18n command directly in the terminal.
Basic Commands
# Convert all files in the src directory
meetfun-i18n convert
# Convert specific directories
meetfun-i18n convert src/pages src/components
# Preview which files need conversion
meetfun-i18n preview
# Convert a single file
meetfun-i18n file src/components/Hello.vue
# Clean up backup files
meetfun-i18n cleanOptions
-v, --verbose # Output detailed log information
-b, --backup # Backup original files
-m, --min <length> # Minimum Chinese character length to process
--dev-only # Only run in development environment
--exclude <patterns> # Exclude file patterns, separated by commasProject Integration
Add the following scripts to your package.json:
{
"scripts": {
"i18n:convert": "meetfun-i18n convert",
"i18n:preview": "meetfun-i18n preview",
"i18n:file": "meetfun-i18n file",
"i18n:clean": "meetfun-i18n clean"
}
}Then run:
npm run i18n:convert srcProgrammatic API
You can also use the library programmatically in your Node.js applications:
const { createConverter, convert, preview } = require('meetfun-i18n-cli')
// Create a converter instance with options
const converter = createConverter({
verbose: true,
backup: true,
minChineseLength: 2
})
// Convert files
await converter.convertBatch(['src/pages', 'src/components'])
// Or use the shortcut functions
await convert(['src'], { verbose: true })
await preview(['src'])Vite Plugin
You can use the tool as a Vite plugin during development:
// vite.config.js
const { createPlugin } = require('meetfun-i18n-cli')
module.exports = {
plugins: [
createPlugin({
devOnly: true,
verbose: true,
exclude: [/node_modules/, /test/]
})
]
}How It Works
The tool scans your project files and automatically converts Chinese strings to internationalized format:
- In Vue templates: Convert text and attributes to
{{ $t('key') }}or:attr="$t('key')" - In script blocks: Convert string literals to
$t('key')orthis.$t('key') - Support for template strings with variables: Convert to
$t('key', { variable }) - Support for both Vue 2 Options API and Vue 3 Composition API
The conversion is based on simple text pattern matching. After conversion, you'll need to manually add the corresponding translations to your language files.
Ignore Comments
You can use special comments to prevent certain Chinese strings from being converted:
Using /*i18n-ignore*/ Comment
Place a /*i18n-ignore*/ comment before the code block you want to skip:
/*i18n-ignore*/
const message = '你好世界'
const title = "欢迎使用"This will prevent the tool from converting these Chinese strings to $t() format.
Examples
In Vue Template:
<template>
<div>
<!-- Normal conversion -->
<p>{{ text }}</p>
<!--i18n-ignore-->
<p>这段文字不会被转换</p>
</div>
</template>In JavaScript/TypeScript:
// Normal conversion
const greeting = '你好'
/*i18n-ignore*/
const debugMessage = '调试信息:这不会被转换'
const logMessage = '日志内容'In Vue Script:
<script>
export default {
data() {
return {
title: '这会被转换',
/*i18n-ignore*/
devNote: '开发备注:不转换'
}
}
}
</script>Configuration Options
You can customize the behavior of the tool using these options:
enabled: Enable or disable the plugin (default:true)verbose: Show detailed log information (default:false)devOnly: Run only in development environment (default:false)backup: Create backup files before modifying (default:false)minChineseLength: Minimum length of Chinese strings to process (default:2)exclude: Array of regex patterns to exclude files from processing
Best Practices
- Always commit your code before running the conversion
- Use the preview mode first to see what will be changed
- Review the conversion results carefully
- Add the generated keys to your language files
- Test the internationalized application thoroughly
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
