kfriars / laravel-translations-manager
用于管理 Laravel 中翻译的工具
Requires
- php: ^7.2
- illuminate/console: ^6|^7|^8
- illuminate/contracts: ^6|^7|^8
- illuminate/filesystem: ^6|^7|^8
- illuminate/support: ^6|^7|^8
- illuminate/translation: ^6|^7|^8
- kfriars/php-array-to-file: ^1.1.2
- league/flysystem: ^1.0.34
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
- psalm/plugin-laravel: ^1.2
- vimeo/psalm: ^3.11
This package is auto-updated.
Last update: 2024-09-14 03:02:13 UTC
README
为什么使用这个包?
你有没有在需要支持多个地区语言的项目上工作过,创建了一个新分支,工作了几天后,然后想知道你到底添加或更改了哪些翻译?如果是这样,你知道开发者管理需要添加或更新的翻译是既耗时又容易出错的。
此包的目的是使整个流程变得轻松简单。想知道哪些已经被更新、删除和添加了吗?很简单。想将所有需要翻译的内容导出到一个可以发送给翻译者的文件中?没问题。想自动更新所有翻译过的文件,以确保没有错误?完成。
内容
安装
您可以通过 composer 安装此包
composer require kfriars/laravel-translations-manager
此包无需修改配置即可直接使用。但是,您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Kfriars\TranslationsManager\TranslationsManagerServiceProvider" --tag="config"
这是已发布的配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Laravel Lang Directory |-------------------------------------------------------------------------- | | This value is the path to laravels 'lang' folder. This value is used to | find your applications translations files using __() and Trans | */ 'lang_dir' => resource_path('lang'), /* |-------------------------------------------------------------------------- | Reference Locales |-------------------------------------------------------------------------- | | This is the locale that the development team or a developer works in. | This setting assumes all changes to translations files are being made in | this locale. | */ 'reference_locale' => config('app.locale'), /* |-------------------------------------------------------------------------- | Supported Locales |-------------------------------------------------------------------------- | | These are the locales supported by your application. By default, the | Supported locales are all folders listed in the lang directory. You can | override the setting if you do not want all locales to be validated using | this package. | */ // 'supported_locales' => [], /* |-------------------------------------------------------------------------- | Fix Files |-------------------------------------------------------------------------- | | The fix files are used to fix errors in the translations. | | 'formatter' is the class that will be used to write and parse the fix files. | The default format is a JSON format, since it is easily readable by humans | and reliably parseable. You can implement your own formatter as long as it | implements the FormatterContract, and use it by changing the setting below. | | 'fix_name_format' is the way fix files will be named. The currently | supported formats are 'git' and 'date'. | | 'git' format is 'fixes-{locale}-{git branch name}.txt' | 'date' format is 'fixes-{locale}-{Y-m-d}.txt' | */ 'formatter' => Kfriars\TranslationsManager\TranslationsFixesJSONFormatter::class, 'fix_name_format' => 'git', ];
参考地区
参考地区是您的开发团队用于开发的语言。此包建立在这样一个理念之上:当前参考地区的翻译是项目翻译的正确版本。
例如
您正在为一个法语公司开发一个项目,但该项目也支持英语、德语和西班牙语。原型设计是用法语提供的,因此您将法语设置为默认应用程序地区,并首先创建法语翻译文件。然后在实际翻译完成后将所有内容翻译成英语、德语和西班牙语。法语将是参考地区。
工作流程
假设使用标准 git-flow,其中 master
分支部署到生产环境,而 develop
分支应保持可部署状态。
此包旨在作为项目工作流程的一部分使用。要使分支处于可部署状态,应没有翻译错误。
当 feature
分支完成,并准备好向 develop
分支发送拉取请求时,应采取以下步骤以确保没有翻译错误。
1) 检查错误
此命令将列出项目中所有的翻译错误。
php artisan translations:errors
示例输出
There are 9 error(s) in the translations files: +-------------+-------------------------------+ | de/common | +-------------+-------------------------------+ | Key | Message | +-------------+-------------------------------+ | company | translation_missing | +-------------+-------------------------------+ +-------------+-------------------------------+ | de/contact | +-------------+-------------------------------+ | Key | Message | +-------------+-------------------------------+ | email | translation_missing | +-------------+-------------------------------+ | straße | no_reference_translation | +-------------+-------------------------------+ | phone | reference_translation_updated | +-------------+-------------------------------+ +------------+----------------------+ | de/admin | +------------+----------------------+ | Key | Message | +------------+----------------------+ | FILE_ERROR | file_not_translated | +------------+----------------------+ +-------------+-------------------------------+ | en/contact | +-------------+-------------------------------+ | Key | Message | +-------------+-------------------------------+ | phone | reference_translation_updated | +-------------+-------------------------------+ +------------+----------------------+ | en/admin | +------------+----------------------+ | Key | Message | +------------+----------------------+ | FILE_ERROR | file_not_translated | +------------+----------------------+ +-------------+---------------------+ | es/contact | +-------------+---------------------+ | Key | Message | +-------------+---------------------+ | FILE_ERROR | file_not_translated | +-------------+---------------------+ +------------+----------------------+ | es/admin | +------------+----------------------+ | Key | Message | +------------+----------------------+ | FILE_ERROR | file_not_translated | +------------+----------------------+
错误类型
2) 清理无效翻译
php artisan translations:clean de en es
no_reference_translation
错误可能表明从参考地区删除密钥时产生了无效翻译,并且忘记在支持的地区删除它们。在检查了所有这些错误并确保它们确实是无效翻译后,您可以使用清理命令从支持的地区删除所有这些键。
3) 忽略错误
php artisan translations:ignore locale file key?
如果支持地区中存在不需要维护的翻译,则可以忽略这些错误。在 #1
的示例输出中,管理员界面的翻译可能需要翻译。
因此,可以使用以下命令忽略它们
php artisan translations:ignore de admin php artisan translations:ignore en admin php artisan translations:ignore es admin
4) 生成修复文件
php artisan translations:generate-fixes de en es
由于所有剩余的错误都需要采取一些行动,可以生成包含所有必需翻译的修复文件,以修复每个地区中的错误。此文件旨在发送给翻译者,并以当前格式返回。文件以JSON格式存储,因为它是人类可读的,并且可以可靠地解析。
默认情况下,文件以命名模式 fixes-{locale}-{git-branch-name}.txt
生成。这些文件保存在配置的 fixes_dir
中。默认为 storage/translations/fixes
。
以下将使用法语作为参考地区生成修复文件。
fixes-de-feature-xzy.txt
{ "reference": "fr", "locale": "de", "files": [{ "file": "common", "translations": { "company": "Compagnie" } }, { "file": "contact", "translations": { "email": "Adresse électronique", "phone": "Numéro de téléphone", } }] }
fixes-en-feature-xzy.txt
{ "reference": "fr", "locale": "en", "files": [{ "file": "contact", "translations": { "phone": "Numéro de téléphone" } }], }
fixes-es-feature-xzy.txt
{ "reference": "fr", "locale": "es", "files": [{ "file": "contact", "translations": { "first_name": "Prénom", "last_name": "Nom de famille", "email": "Adresse électronique", "phone": "Numéro de téléphone", } }], }
5) 修复文件
当修复文件完成后并由翻译者返回时,可以将它们放置在配置的 fixed_dir
中。默认为 storage/translations/fixed
。一旦文件已放入目录,可以运行以下命令。
php artisan translations:fix de en es
请注意,此命令将从支持的地区中删除所有带有 no_reference_translation
错误的翻译。
此外,任何包含在修复文件中并具有 reference_translations_updated
的翻译都将更新其锁文件,以包含当前参考地区的翻译。
6) 验证翻译
现在可以通过运行以下命令来测试是否存在任何翻译错误
php artisan translations:validate
如果输出为 Validation Passed
,则可以继续下一步。我强烈建议您添加此命令并确保它在 ci/cd 流中通过以进行生产部署。
7) 锁定翻译
如果由于 reference_translation_updated
错误而验证失败,但您对支持地区翻译的状态满意,则可以通过运行以下命令锁定参考地区翻译的当前状态
php artisan translations:lock
这将消除所有 reference_translation_updated
错误。
8) 创建 PR 并合并
一旦翻译验证通过,就可以合并代码了!
命令
验证
签名:php artisan translations:validate locales?* --no-ignore
确定指定地区中是否存在任何错误。如果没有提供地区,将验证所有支持的地区。
错误
签名:php artisan translations:errors locales?* --no-ignore
列出指定地区中存在的任何错误。
清理
签名:php artisan translations:clean locales?*
从指定地区清除死翻译(带有消息 'no_reference_translation' 的错误)。
忽略
签名:php artisan translations:ignore locale file key?
忽略翻译错误。省略命令中的 key 参数将忽略文件中的所有错误。忽略错误允许验证命令通过,即使存在您不想解决的问题。
取消忽略
签名:php artisan translations:unignore locale file key?
取消忽略翻译错误。省略命令中的 key 参数将取消忽略文件中的所有错误。
生成修复
签名:php artisan translations:generate-fixes locales?*
为指定地区生成修复文件。如果没有提供地区,将生成所有支持地区的修复文件。修复文件将生成到配置的 fixes_dir
。
修复
签名:php artisan translations:fix locales?*
使用修复文件修复指定地区。如果没有提供地区,将修复所有支持地区。该命令在配置的 fixed_dir
中查找修复文件。
状态
签名:php artisan translations:status locales?*
获取翻译管理器的完整状态列表。状态显示了每个指定地区每个翻译文件的错误和忽略信息。如果没有提供地区,将列出所有支持地区的状态。
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件kfriars@gmail.com报告,而不是使用问题跟踪器。
致谢
许可协议
MIT许可证(MIT)。请参阅许可证文件获取更多信息。