@mylukin/easy-i18n-js
v0.1.2
Published
Framework-agnostic i18n extraction tool with plugin support for Svelte, Vue, and more
Maintainers
Readme
@mylukin/easy-i18n-js
A framework-agnostic i18n extraction and management tool with plugin support for Svelte, Vue, and more.
一个与框架无关的 i18n 提取和管理工具,支持 Svelte、Vue 等框架的插件系统。
Features
- Framework-agnostic: Works with any JavaScript/TypeScript project
- Plugin architecture: Built-in plugins for Svelte and Vue
- AST-based extraction: Accurate extraction using Babel parser
- Locale management: Update, merge, and sync locale files
- CLI and API: Use as a command-line tool or programmatic API
Installation
npm install -D @mylukin/easy-i18n-js
# For Svelte support
npm install -D svelte
# For Vue support
npm install -D @vue/compiler-sfcCLI Usage
Extract i18n strings
# Basic extraction
npx easyi18n extract -s ./src -o ./locales/en.json
# With custom patterns
npx easyi18n extract --include "**/*.{ts,tsx,vue}" --exclude "tests/**"
# Dry run (preview without writing)
npx easyi18n extract --dry-runUpdate locale files
# Sync target locales with source
npx easyi18n update ./locales/en.json ./locales/zh.json ./locales/ja.json
# Remove unused keys
npx easyi18n update -f ./locales/en.json ./locales/zh.jsonCheck translation status
# Show coverage report
npx easyi18n status ./locales/en.json ./locales/zh.json ./locales/ja.json
# Find missing translations
npx easyi18n missing ./locales/en.json ./locales/zh.json
# Find unused keys
npx easyi18n unused ./locales/en.json ./locales/zh.jsonProgrammatic API
import {
extract,
extractFromDirectory,
mergeResults,
updateLocale,
readLocaleFile,
writeLocaleFile
} from '@mylukin/easy-i18n-js';
// Extract from code string
const items = extract(`$t('Hello, {name}')`, 'app.js');
console.log(items);
// [{ key: 'Hello, {name}', file: 'app.js', line: 1, column: 1, hasParams: true, params: ['name'] }]
// Extract from directory
const allItems = await extractFromDirectory('./src', {
include: '**/*.{js,ts,vue,svelte}',
exclude: ['node_modules/**']
});
// Merge and deduplicate
const merged = mergeResults(allItems);
// Update locale files
const source = await readLocaleFile('./locales/en.json');
const target = await readLocaleFile('./locales/zh.json');
const updated = updateLocale(source, target, { flush: true });
await writeLocaleFile('./locales/zh.json', updated);Plugin System
Using built-in plugins
import { registerPlugin, sveltePlugin, vuePlugin } from '@mylukin/easy-i18n-js';
// Register plugins
registerPlugin(sveltePlugin);
registerPlugin(vuePlugin);Creating custom plugins
import type { FrameworkPlugin, ExtractionItem } from '@mylukin/easy-i18n-js';
const myPlugin: FrameworkPlugin = {
name: 'my-framework',
extensions: ['.myext'],
extract(code: string, file: string): ExtractionItem[] {
// Your extraction logic
return [];
},
isAvailable(): boolean {
// Check if dependencies are installed
return true;
}
};
registerPlugin(myPlugin);Supported i18n Patterns
The tool detects these common i18n function patterns:
// Function calls
$t('Hello')
t('Hello')
i18n.t('Hello')
this.$t('Hello')
// With parameters
$t('Hello, {name}', { values: { name } })
t('{count} items', { count: 5 })
// Vue templates
{{ $t('Hello') }}
{{ t('Hello') }}
v-t="'Hello'"
// Svelte templates
{$t('Hello')}
{t('Hello')}API Reference
Extraction
extract(code, file, options?)- Extract from code stringextractFromTypescript(code, file, options?)- Extract from TypeScriptextractFromFile(filePath, options?)- Extract from fileextractFromDirectory(dir, options?)- Extract from directorymergeResults(results)- Merge and deduplicate results
Locale Management
readLocaleFile(path)- Read JSON locale filewriteLocaleFile(path, data, options?)- Write locale fileupdateLocale(source, target, options?)- Update locale datasortKeys(obj)- Sort keys alphabeticallyfindMissing(source, target)- Find missing translationsfindUnused(source, target)- Find unused keysgetCoverage(source, target)- Get translation coverage stats
Plugin Management
registerPlugin(plugin)- Register a framework pluginunregisterPlugin(name)- Remove a plugingetRegisteredPlugins()- List registered plugins
License
MIT
