Vue i18n Translation Extractor
vue-i18n-extract is a development tool designed for managing `vue-i18n` localization within Vue.js projects through static code analysis. It scans Vue Single File Components (SFCs), JavaScript, and TypeScript files to identify used i18n keys and compares them against existing translation files (e.g., JSON, YAML). This helps developers find missing translations, identify unused keys that can be safely removed, and detect duplicate keys. The current stable version is 2.0.7, with an active release cadence, frequently incorporating new features and bug fixes as evidenced by recent patch releases. Its primary differentiation lies in its comprehensive static analysis approach for Vue-specific i18n usage, supporting various patterns like `$t`, `v-t`, and `Translation` components.
Common errors
-
[vue-i18n-extract] Error: vueFiles glob has no files.
cause The glob pattern provided for `--vueFiles` (or in the config file) does not match any existing `.vue`, `.js`, or `.ts` files in your project. This often happens due to incorrect paths or typos in the pattern.fixVerify the `vueFiles` glob pattern in your command or configuration file. Use a tool like `ls -l './src/**/*.vue'` to confirm that files are matched by the pattern. Adjust the path or pattern as needed. -
[vue-i18n-extract] Error: languageFiles glob has no files.
cause Similar to `vueFiles` not being found, this error indicates that the glob pattern for `--languageFiles` does not match any translation files (e.g., `.json`, `.yml`, `.yaml`) at the specified location.fixCheck the `languageFiles` glob pattern. Ensure the path is correct and the file extensions (`.json`, `.yml`, `.yaml`) are included and properly escaped if necessary (e.g., `*.?(json|yml|yaml)`). -
TypeError: Cannot read properties of undefined (reading 'extractI18n')
cause This typically occurs when attempting to use programmatic features (like `extractI18n`) in a CommonJS module context when the package is primarily designed for ES Modules, or when the named export is incorrect.fixFor Node.js projects with `'type': 'module'`, use ES module imports: `import { extractI18n } from 'vue-i18n-extract';`. If sticking to CommonJS, check if a default export or different named exports are available, or use `await import('vue-i18n-extract')` for dynamic imports.
Warnings
- breaking Upgrading from `v1.x` to `v2.x` introduces significant breaking changes in configuration options. Several CLI arguments and config file properties have been renamed or removed.
- gotcha Incorrect glob patterns for `--vueFiles` or `--languageFiles` are a common cause of issues, leading to missing translations not being detected or files being ignored.
- gotcha When using `vue-i18n-extract` programmatically in an ES module (`'type': 'module'` in `package.json`) Node.js project, direct `require` statements for the library will fail with `SyntaxError: Unexpected token 'export'`.
- deprecated The underlying `vue-i18n` library has deprecated certain features like `$tc` and `tc` functions, especially for Composition API mode, and the `<i18n>` component in favor of `<i18n-t>`. While `vue-i18n-extract` aims to support various syntaxes, relying on deprecated `vue-i18n` features can lead to future compatibility issues.
Install
-
npm install vue-i18n-extract -
yarn add vue-i18n-extract -
pnpm add vue-i18n-extract
Imports
- extractI18n
const { extractI18n } = require('vue-i18n-extract');import { extractI18n } from 'vue-i18n-extract'; - readVueFiles
import { readVueFiles } from 'vue-i18n-extract'; - writeLocaleMessages
import { writeLocaleMessages } from 'vue-i18n-extract';
Quickstart
{
"name": "my-vue-i18n-app",
"version": "1.0.0",
"description": "My Vue.js i18n project",
"main": "index.js",
"scripts": {
"i18n:report": "vue-i18n-extract report --vueFiles './src/**/*.?(js|ts|vue)' --languageFiles './src/locales/*.?(json|yml|yaml)' --ci",
"i18n:add": "vue-i18n-extract report --vueFiles './src/**/*.?(js|ts|vue)' --languageFiles './src/locales/*.?(json|yml|yaml)' --add",
"i18n:remove": "vue-i18n-extract report --vueFiles './src/**/*.?(js|ts|vue)' --languageFiles './src/locales/*.?(json|yml|yaml)' --remove"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"vue-i18n-extract": "^2.0.0"
}
}