kunstmaan/translator-bundle

一个插件,允许在不编辑翻译文件的情况下,在管理界面编辑翻译。翻译将存储在(默认)数据库中,并以最有效的方式检索。

安装次数: 114,407

依赖项: 3

建议者: 1

安全: 0

星级: 50

关注者: 16

分支: 10

类型:symfony-bundle

7.1.1 2024-04-01 19:36 UTC

This package is auto-updated.

Last update: 2024-08-28 20:17:40 UTC


README

Build Status Total Downloads Latest Stable Version Analytics

一个插件,允许在不编辑翻译文件的情况下,在管理界面编辑翻译。翻译将存储在(默认)数据库中,并以最有效的方式检索。

Symfony Profiler Example

安装要求

在您安装 KunstmaanTranslatorBundle 之前,您应该能够安装并运行 Symfony >=2.3。

安装说明

假设您已安装 composer.phar 或 composer 二进制文件

$ composer require kunstmaan/translator-bundle 2.3.*@dev
$ composer require doctrine/migrations dev-master
$ composer require doctrine/doctrine-migrations-bundle dev-master

将 KunstmaanTranslatorBundle 添加到您的 AppKernel.php 文件

new Kunstmaan\TranslatorBundle\KunstmaanTranslatorBundle(),
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),

将 KunstmaanTranslatorBundle 添加到您的 routing.yml。考虑到您自己的路由配置,您可能需要在您自己的路由配置之前添加以下代码

KunstmaanTranslatorBundle:
    resource: "@KunstmaanTranslatorBundle/Resources/config/routing.yml"
    prefix:   /{_locale}/
    requirements:
        _locale: "%requiredlocales%"

配置

根据需要覆盖 config.yml 中的 KunstmaanTranslatorBundle 配置,以下为默认值

kunstmaan_translator:
    enabled:         true
    default_bundle:  own
    bundles:         []
    cache_dir:       %kernel.cache_dir%/translations
    managed_locales: []
    file_formats:    ['yml', 'yaml', 'xliff']
    debug:           defaults to the kernel.debug parameter (boolean)
  • enabled : 启用或禁用 KunstmaanTranslatorBundle
  • default_bundle : 在后端使用导入功能所用的哪些束。可能的值:'own', 'all', 'custom'。
    • own : 您 src 目录中的所有束
    • all : 您 src 目录中的所有束 + 您 vendor 目录中的所有束
    • custom : 您在 bundles 中指定的束
  • bundles : 将用于后端导入功能的束列表。仅在 default_bundle 设置为 'custom' 时使用。
  • cache_dir : 缓存翻译目录
  • managed_locales : 应导入哪些区域设置翻译文件
  • file_formats : 应导入哪种类型的翻译文件
  • debug : 当启用调试时,禁用翻译缓存

示例配置

kunstmaan_translator:
    managed_locales: ['en', 'fr', 'es']
kunstmaan_translator:
    default_bundle: own
    managed_locales: ['en', 'fr', 'es']
    debug: false
kunstmaan_translator:
    enabled: true
    default_bundle: custom
    bundles: ['MyCompanyCoolBundle', 'MyCompanyAwesomeBundle']
    managed_locales: ['en', 'fr', 'es']

数据库模式

使用 doctrine 更新您的数据库模式

bin/console doctrine:schema:update --force

Database schema updated successfully! "1" queries were executed

将开发翻译迁移到生产

使用以下命令生成 doctrine 迁移,其中包含从您当前环境的所有新翻译和更新的翻译。

bin/console kuma:translator:migrations:diff

当您想将迁移的这些翻译包含到您的(其他)环境时,使用正常的 doctrine migrate 命令。

bin/console doctrine:migrations:migrate

导入现有翻译文件

当迁移您的当前项目时,您可以轻松导入现有翻译文件。

如果没有参数,则包括所有翻译,从当前 main 项目中的所有区域

bin/console kuma:translator:import

要强制覆盖存储器中的现有翻译

bin/console kuma:translator:import --force

要导入特定束的翻译

bin/console kuma:translator:import --bundle=superCoolNewApplicationBundle

要导入特定区域

bin/console kuma:translator:import --locales=nl,fr,de

要从全局资源(app/Resources/translations)导入翻译

bin/console kuma:translator:import --globals

缓存是如何工作的

翻译存储在数据库中,但在非调试模式下缓存(如 Symfony 通常所做)。

$kernel = new AppKernel('prod', false); // translations are cached an read from this cache
$kernel = new AppKernel('dev', true); // translations are always loaded from the stash (slower, more queries)

当在后台编辑翻译时,更改不会立即在您的网站上可见。当未加载较新或更新的翻译到缓存时,后端将显示警告消息。单击 刷新实时 按钮以重建缓存。

清除缓存和请求状态

清除翻译缓存文件,这将触发在访问页面时重建翻译缓存

bin/console kuma:translator:cache --flush

请求当前缓存的状态

bin/console kuma:translator:cache --status

重置翻译标志

当所有翻译都更新到最新状态时,例如将所有开发翻译迁移到生产环境中,您需要重置所有标记翻译为新或更新的标志。否则,已迁移的翻译将再次添加到后续迁移中(这可能会导致插入和唯一键错误)

bin/console kuma:translator:flag --reset

查找翻译的关键字/域

您可能并不总是记得特定页面上的翻译使用的是哪个关键字和/或域。为了解决这个问题,您可以在请求中添加一个额外的GET参数。将?transSource=1添加到您的URL中,以查看所有翻译标签的来源。

您可能会看到“Hello world”而不是“Hello world”,而是header.hello_world (messages)。这意味着

  • 关键字是header.hello_world
  • 域是messages

Symfony Profiler集成

Symfony Profiler显示了当前请求中使用的翻译数量

Symfony Profiler Example

当您点击此项目时,您可以看到当前请求中使用的所有翻译以及添加或编辑它们的Kunstmaan Admin后端链接。

Symfony Profiler Example Table

工作流程示例(新项目)

  1. 在您的模板文件中添加翻译(带关键字)(开发环境)
  2. 通过“添加翻译”将(1)的翻译添加到您的后端(开发环境)
  3. 重复1和2
  4. 创建迁移差异bin/console kuma:translator:migrations:diff(开发环境)
  5. 重置翻译标志bin/console kuma:translator:flag --reset(开发环境)
  6. 部署您的应用程序
  7. 执行Doctrine迁移bin/console doctrine:migrations:migrate(生产环境)
  8. 编辑/添加翻译(生产环境)
  9. 编辑/添加完成后,单击“刷新实时”或bin/console kuma:translator:cache --flush(生产环境)
  10. 在prod中编辑/添加翻译时,重复7和8

工作流程示例(现有项目)

  1. 导入当前翻译,点击“导入 -> 导入”或bin/console kuma:translator:import(生产/开发环境)
  2. 如果您在开发环境中进行了1的操作,请转到“工作流程示例(新项目)”中的4,否则转到7“工作流程示例(新项目)”

功能

  • 从任何类型的翻译文件导入bundle/global翻译
  • 仅导入特定翻译(仅从控制台命令中导入)
  • 强制导入以覆盖具有相同域/关键字/语言环境的现有翻译
  • 从后端界面编辑存储的翻译
  • 从后端界面添加新翻译
  • 翻译被缓存(如果禁用调试)
  • 当缓存的翻译与存储的翻译不一致时发出警告
  • 清除翻译缓存以从存储的翻译重建翻译
  • 较新或更新的翻译被标记
  • 创建包含所有标记翻译的Doctrine迁移文件
  • 重置所有标记翻译(从控制台命令中)
  • 从控制台命令清除和检查翻译缓存
  • 检查所有翻译的关键字和域的页面

开发说明

运行单元测试

./vendor/bin/phpunit -c phpunit.xml.dist

运行PHP CS Fixer,在安装php-cs-fixer系统全局

php-cs-fixer fix .

如何创建您自己的文件导出器

  • 将您的导出器标记为`translation.exporter`
  • 实现\Kunstmaan\TranslatorBundle\Service\Exporter\FileExporterInterface

注意:导出尚未稳定(目前)