it-blaster/translation-bundle

Symfony2 扩展包,用于处理网站的语言版本。包括高级 Propel 行为 sluggable。

安装: 365

依赖: 3

建议者: 0

安全: 0

星标: 0

关注者: 5

分支: 0

开放问题: 2

类型:symfony-bundle

1.0.4 2016-01-12 16:56 UTC

This package is not auto-updated.

Last update: 2024-09-25 14:41:55 UTC


README

Build Status Scrutinizer Code Quality

用于处理网站语言版本的辅助包

安装

composer.json 中添加 ItBlasterTranslationBundle

{
    "require": {
        "it-blaster/translation-bundle": "dev-master"
	},
}

现在运行 composer 命令以下载扩展包

$ php composer.phar update it-blaster/translation-bundle

composer 将扩展包安装到项目的 vendor/it-blaster/translation-bundle 文件夹中。

然后,在 AppKernel.php 核心中连接扩展包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new ItBlaster\TranslationBundle\ItBlasterTranslationBundle(),
    );
}

app/config/config.yml 中,重新定义 propel 行为 sluggable 的路径

 propel:
     ...
     behaviors:
         ...
         sluggable: ItBlaster\TranslationBundle\Behavior\ExtendedSluggableBehavior
         it_blaster_translation_model: ItBlaster\TranslationBundle\Behavior\TranslationModelBehavior

schema.yml 文件中,为表指定行为,并指定参数 primary_string,基于它将形成 slug。例如

    <behavior name="sluggable">
        <parameter name="primary_string" value="title" />
    </behavior>

如果您不需要为表创建 slug,只需按正确的顺序显示语言版本字段,则只需指定行为 it_blaster_i18n

        <behavior name="it_blaster_i18n">
            <parameter name="primary_string" value="question" />
        </behavior>

并在 config.yml 中指定其连接

 propel:
     ...
     behaviors:
         ...
         it_blaster_i18n: ItBlaster\TranslationBundle\Behavior\ExtendedI18nBehavior

行为 ExtendedSluggableBehavior 已经包括了 ExtendedI18nBehavior 中的方法,因此不需要在方案中同时指定它们。

如果您使用语言版本(i18n),则需要 在 config.yml 文件中指定参数 it_blaster_translation.localesit_blaster_translation.slug_locales

it_blaster_translation:
    locales: ['ru', 'en','uk','cs']
    slug_locales: ['en','ru']

参数 it_blaster_translation.locales 负责在 CMS 编辑表单中字段输出的顺序。参数 it_blaster_translation.slug_locales 负责形成 slug 的语言顺序。如果第一个语言的值未填写,则系统将根据下一个语言版本的值生成 slug。

翻译表

需要在项目的 src 文件夹中创建翻译文件 messages.locale.phpvalidators.locale.php,并包含以下内容

<?php
return \ItBlaster\TranslationBundle\Model\TranslationPeer::getListForLocale("LOCALE");

例如,对于英语,需要创建以下文件

  1. src\App\MainBundle\Resources\translations\messages.en.php
  2. src\App\MainBundle\Resources\translations\validators.en.php

内容如下

<?php
return \ItBlaster\TranslationBundle\Model\TranslationPeer::getListForLocale("en");

为了填充翻译表,需要在控制台中运行索引任务

'php app/console translation:extract en --dir=./src/ --output-dir=./src/App/MainBundle/Resources/translations/ --output-format="php"'

如果您只需要模板文件(twig)的翻译,则取消选中 php 文件

'php app/console translation:extract en --dir=./src/ --output-dir=./src/App/MainBundle/Resources/translations/ --exclude-name="*.php" --output-format="php"'

连接特点

  1. 如果项目中未使用 JMSTranslationBundle,则需要将其添加到 composer.json 中 http://jmsyst.com/bundles/JMSTranslationBundle/master/installation
  2. 在某些情况下,服务 jms_translation.dumper.php_dumper.class 不会被重新定义,如果发生这种情况,则需要 在 app/config.yml 中指定
parameters:
    ...
    jms_translation.dumper.php_dumper.class: ItBlaster\TranslationBundle\Translation\Dumper\PhpDumper
    ...

致谢

It-Blaster it-blaster@yandex.ru