c33s / i18n-helper-behavior
此包已被弃用且不再维护。未建议替代包。
默认i18n propel行为的实用补充
v0.2.0
2017-02-27 11:58 UTC
Requires
- php: >=5.3.3
- propel/propel-bundle: ~1.2
This package is not auto-updated.
Last update: 2022-02-01 12:44:32 UTC
README
默认i18n propel行为的实用补充
安装
在您的 composer.json
文件中要求 c33s/i18n-helper-behavior
{ "require": { "c33s/i18n-helper-behavior": "@stable", } }
将 propel 行为添加到您的 propel 配置中
# When using symfony: # app/config/config.yml propel: # ... behaviors: c33s_i18n_helper: vendor.c33s.i18n-helper-behavior.src.C33sPropelBehaviorI18nHelper
用法
将行为添加到您的 propel 模型中 - 要么全局(自动应用于所有包含 i18n 行为的模型)或特定模型
<!-- my/Bundle/Resources/config/schema.xml --> <!-- use globally for all i18n models --> <behavior name="c33s_i18n_helper"> <parameter name="default_locales" value="de, en" /> </behavior> <table name="book"> <behavior name="i18n"> <!-- configure i18n as needed --> <parameter name="i18n_columns" value="title" /> <parameter name="default_locale" value="de" /> </behavior> <!-- use model-specific --> <behavior name="c33s_i18n_helper"> <parameter name="default_locales" value="de, en" /> </behavior> <column name="title" type="varchar" size="255" /> <...> </table>
该行为将为您的模型添加一些便利函数。在上面的示例中,将为 i18n 列 title
生成以下方法
<?php use C33s\Behavior\I18nHelper\I18nModelInterface; class BaseBook extends BaseObject implements Persistent, I18nModelInterface { // ... // c33s_i18n_helper behavior /** * Get all available translations of the "Title" column. * This returns an associative array with locale => value pairs. * * @return array */ public function getI18nTitle() { // ... } /** * Set translations of the "Title" column. * Accepts an associative array with locale => value pairs. * * @return Book */ public function setI18nTitle($allTitles) { // ... } /** * Get i18n value of the "Title" column, using locale fallback (reverse default locales) * if the value is empty. * Starts with either the given locale or the current/default locale, set previously using getTranslation(). * * @param string $locale * * @return mixed */ public function getTitleWithFallback($locale = null) { // ... } /** * Set an array of default locales to use for the c33s_i18n_helper behavior (getI18n*(), get*WithFallback()). * * @param array $locales * * @return Book */ public function setI18nDefaultLocales(array $locales) { // ... } /** * Get an array of default locales used by the c33s_i18n_helper behavior (getI18n*(), get*WithFallback()). * * @return array */ public function getI18nDefaultLocales() { // ... } }
此外,所有您的 i18n 模型都将实现 I18nModelInterface
,这有助于在任何需要的地方识别可翻译对象。
您可以使用 getI18n*()
方法与 Symfony2 collection
表单类型(例如 text
输入的集合)一起使用,允许您一次编辑给定字段的全部翻译,而无需任何其他代码或配置。
待办事项
- 表单示例。
- 实现 FormType 猜测器。