jprud67 / translator-bundle
本套件为您提供了一种非常简单的方式,将内容翻译添加到您的symfony网站。
v0.1.1
2019-10-15 09:22 UTC
Requires
- php: >=7.0
- doctrine/doctrine-bundle: *
- symfony/framework-bundle: >= 4.0
- symfony/twig-bundle: 4.*.*
This package is not auto-updated.
Last update: 2024-09-25 20:38:04 UTC
README
本套件为您提供了一种非常简单的方式,将内容翻译添加到您的symfony网站。
TranslatorBundle 安装
1: 在composer中添加依赖
composer require jprud67/translator-bundle
2: 在Symfony内核中注册TranslatorBundle
<?php
// config/bundle.php
return [
//...
Jprud67\TranslatorBundle\Jprud67TranslatorBundle::class => ['all' => true],
];
3: 添加路由
# config/routes.yaml
jprud67_translation_routing:
resource: "@Jprud67TranslatorBundle/Resources/config/routing.yaml"
4: 包配置
# config/packages/jprud67_translator.yaml
doctrine:
orm:
mappings:
Jprud67_translator:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/vendor/Jprud67/translator-bundle/Entity'
prefix: 'Jprud67\TranslatorBundle\Entity'
alias: Jprud67_translator
jprud67_translator:
locales: ['en','es']
5: 更新数据库
php bin/console doctrine:schema:update --force
6: 安装样式
php bin/console asset:install
使用TranslatorBundle
1: 在实体中添加注解
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Jprud67\TranslatorBundle\Annotation\Translatable;
use Jprud67\TranslatorBundle\Annotation\TranslatableField;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
* @Translatable()
*/
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=190)
* @TranslatableField()
*/
private $title;
// ...
}
2: 访问网址mysite.xyz/translation/webui来编辑您的翻译
3: 在twig中展示您的翻译
{% for post in posts %}
<li>{{ jp_trans(post,'title') }}</li>
{% endfor %}