中文文档 | English
A complete React demonstration project showcasing how to use a custom $LS() function for internationalization and how i18n-scanner-toolkit detects and manages these translations.
- $LS() Function - Custom internationalization function using text values directly
- Redux Integration - Language state management with Redux Toolkit
- HOC Pattern - Higher-Order Component wrapper pattern (HOCDemo)
- Hook Pattern - Custom Hook pattern (HookDemo)
- Zero i18n Dependencies - No dependency on react-i18next, completely custom implementation
- Language Switching - Real-time switching between Chinese and English
- Multiple Usage Patterns - Demonstrates different i18n usage approaches
- Missing Translations - Intentionally added missing translations to demonstrate scanner functionality
src/
├── components/ # React components
│ ├── LanguageSwitcher.jsx # Language switcher
│ ├── HOCDemo.jsx # HOC pattern demo
│ └── HookDemo.jsx # Hook pattern demo
├── pages/
│ └── HomePage.jsx # Main page
├── redux/ # Redux state management
│ ├── store.js
│ └── languageSlice.js
├── utils/
│ └── LS.js # Custom $LS function
├── hooks/
│ └── useTranslationI18n.js # Custom Hook
├── localized/ # Internationalization related
│ ├── hoc.js # Higher-order component
│ ├── util.js # Utility functions
│ └── strings/ # Language files
│ ├── zh_hans.js # Chinese
│ └── en.js # English
└── App.jsx # Main application
# Install dependencies
pnpm install
# Start development server
pnpm dev
# Build project
pnpm build
# Lint code
pnpm lint- react & react-dom - React core
- react-redux & @reduxjs/toolkit - State management
- lucide-react - Icon library
- vite & @vitejs/plugin-react - Build tools
- eslint & eslint-plugin-react - Code linting
- i18n-scanner-toolkit - Internationalization scanner
- ❌ react-i18next - Replaced with custom $LS system
- ❌ i18next - No longer needed
- ❌ react-router-dom - Simplified to single-page demo
// Using the scanner programmatically
const { I18nScanner } = require('i18n-scanner-toolkit');
const config = {
framework: 'custom',
sourceDir: 'src',
localeDir: 'src/localized/strings',
extractPattern: /\$LS\s*\(\s*["'`]([^"'`]+)["'`]\s*\)/g,
ignoreKeyPatterns: ['src', 'components']
};
const scanner = new I18nScanner(config);
// 1. Get missing translations
const missing = await scanner.scanAll();
console.log(missing);
// 2. Export to CSV
const csvPath = await scanner.export();
// 3. Import from CSV (after translation)
await scanner.import('./translations.csv');The project includes an i18n-scanner.config.json configuration file:
{
"framework": "custom",
"extractPattern": "\\$LS\\s*\\(\\s*['\"`]([^'\"`]+)['\"`]\\s*\\)",
"localeDir": "src/localized/strings",
"defaultLocale": "zh_hans.js"
}Unlike traditional i18n libraries, this project's $LS() function uses text values directly instead of keys:
// Traditional approach: using keys
t('navigation.home') // Requires defining "navigation.home": "Home" in language files
// $LS approach: direct text usage
$LS('首页') // Uses Chinese text directly, language file defines "首页": "Home"- Developer Friendly - No need to think of key names
- Self-Documenting - The code shows actual text content
- Fallback Ready - If translation is missing, original text is displayed
- Scanner Friendly - Easy to detect and extract with regex patterns
import LS from "../utils/LS.js";
const HOCDemo = ({$LS}) => {
return (
<div>
<h2>{$LS("React 国际化演示")}</h2>
<p>{$LS("首页")}</p>
</div>
)
}
export default LS(HOCDemo)import {useTranslationI18n} from "../hooks/useTranslationI18n.js";
const HookDemo = () => {
const $LS = useTranslationI18n()
return (
<div>
<h2>{$LS("演示页面")}</h2>
<p>{$LS("切换语言")}</p>
</div>
)
}// zh_hans.js (Chinese)
export const message = {
"首页": "首页",
"关于": "关于",
"登录": "登录"
}
// en.js (English)
export const message = {
"首页": "Home",
"关于": "About",
"登录": "Login"
}The scanner detects the following content:
$LS("首页")- Exists in all language files$LS("登录")- Complete translation$LS("成功!")- Status messages
$LS("这个文本在英文翻译中缺失")- Only exists in Chinese file$LS("另一个缺失的文本")- Completely missing"Hardcoded text"- Hardcoded text that should use $LS()
- Number of files scanned
- Number of texts extracted
- Number of missing translations
- Translation coverage percentage
- Automatic Detection - No manual configuration needed, automatically recognizes $LS() patterns
- Detailed Reports - Generates CSV reports with file locations and suggested translation keys
- Multiple Formats - Supports CSV, JSON output
- Zero Configuration - Works out of the box, intelligently detects project structure
- Custom Patterns - Supports various custom i18n function patterns
- Start Project - See Chinese-English switching effects
- Run Scanner - View detected missing translations
- Generate Reports - Get detailed CSV reports
- Fix Translations - Add missing translations based on reports