Angular v15 Monorepo中Transloco Keys Manager提取库翻译键报错求助
解决Transloco Keys Manager提取库翻译键的路径错误问题
可能的原因及解决方案
1. 使用Angular项目标识(--project)替代手动指定路径
在Angular Monorepo中,Transloco Keys Manager支持通过--project参数直接指定库项目,无需手动输入-i和-o路径,能避免路径配置错误。执行以下命令:
./node_modules/.bin/transloco-keys-manager extract --project ui-lib
该命令会自动读取库的Angular配置,扫描对应目录的代码文件,并将翻译键输出到库的assets/i18n目录下(匹配你配置中的translationsPath)。
2. 修正命令参数与配置的冲突
你的全局transloco.config.js中keysManager.input设置为'app',这可能与命令中手动指定的-i路径产生冲突。可以在命令中显式覆盖input参数,同时确保路径正确:
./node_modules/.bin/transloco-keys-manager extract -i projects/ui-lib/src/lib/ -o projects/ui-lib/src/assets/i18n/ --input projects/ui-lib/src/lib/
3. 明确指定扫描的文件类型
如果命令未识别到目录中的模板或TS文件,可以添加--include参数指定要扫描的文件扩展名:
./node_modules/.bin/transloco-keys-manager extract -i projects/ui-lib/src/lib/ -o projects/ui-lib/src/assets/i18n/ --include "*.ts,*.html"
这确保Keys Manager能正确识别包含翻译键的组件模板和代码文件。
4. 检查工作目录
确保你是在仓库根目录(<project-dir>)下执行命令,否则相对路径会失效。可以通过pwd命令确认当前目录是否正确。
备选:为库配置单独的Transloco文件
如果上述方法无效,可以为库创建单独的Transloco配置文件,在projects/ui-lib下新建transloco.config.js:
module.exports = { langs: ['en'], keysManager: { input: 'src/lib', translationsPath: 'src/assets/i18n', sort: true, addMissingKeys: true, replace: false, emitErrorOnExtraKeys: true, defaultValue: 'MISSING:{{key}}' } };
然后执行命令时指定该配置文件:
./node_modules/.bin/transloco-keys-manager extract --config projects/ui-lib/transloco.config.js
内容的提问来源于stack exchange,提问作者Kees de Bruin
相关产品推荐
相关产品推荐

