gossi / propel-l10n-behavior
Requires
- php: >=5.4
- ext-intl: *
- propel/propel: ~2@dev
Requires (Dev)
- phpunit/phpunit: ~4
- propel/propel: dev-master@dev
This package is auto-updated.
Last update: 2024-08-24 04:03:49 UTC
README
propel-l10n-behavior 是 propel 自身 i18n 行为的扩展。基本上,它将一个 API 放在 i18n 行为之前,并允许您使用 propel 的默认 API,但带有本地化内容。您只需(全局)提供一次想要使用的本地化,就可以开始使用。
安装
通过 composer 安装
{ "require": { "gossi/propel-l10n-behavior": "~0" } }
区域设置和依赖项
当使用区域设置时,您应该了解可以为 propel-l10n-behavior 定义的区域设置和依赖项。有三种机制
- 区域设置(这是默认区域设置,当从 propel 对象检索本地化内容时)
- 依赖项(这是一个依赖链,当字段在默认区域设置中不可用时)
- 回退(如果没有找到任何内容,则尝试回退区域设置)
设置默认区域设置
默认区域设置为 en
,但当然您可以更改此设置
PropelL10n::setLocale('de'); echo PropelL10n::getLocale(); // de
默认区域设置可以针对每个对象进行覆盖,例如,您有一个 Book
模型,这是如何工作的
$book = new Book(); $book->setLocale('ja');
现在,书的语言是日语,而其他所有内容仍然是德语(如上面示例所示)。您可以通过将区域设置设置为 null
来重置与此对象相关的覆盖
$book->setLocale(null);
对象默认区域设置可以针对每个调用进行覆盖,例如
PropelL10n::setLocale('it'); $book = BookQuery::create() // locale: it ->setLocale('ja') // locale: ja ->findOneByTitle('Herr der Ringe', null, 'de') // locale: de ; $book->setLocale('ja'); $book->setTitle('Lord of the Rings', 'en');
设置回退区域设置
回退区域设置默认为 en
,但您当然可以更改它
PropelL10n::setFallback('de'); echo PropelL10n::getFallback(); // de
与依赖项一起工作
有一些 API 可以用于处理依赖项
// get and set one dependency PropelL10n::addDependency('de-CH', 'de'); echo PropelL10n::getDependency('de-CH'); // de // get and set all dependencies PropelL10n::setDependencies([ 'de' => 'en', 'de-CH' => 'de', 'de-AT' => 'de' ]); print_r(PropelL10n::getDependencies()); // outputs the array from above // check if dependencies exist PropelL10n::hasDependency('de'); // true PropelL10n::hasDepdedency('ja'); // false // count depdencies for a given locale PropelL10n::setDependencies(['de' => 'en', 'de-CH' => 'de']); PropelL10n::countDependencies('de-CH'); // 2
检索本地化字段
每次检索本地化字段时,行为将使用以下算法来找到字段在最近区域设置中的内容
- 设置默认区域设置为区域设置。
- 尝试在设置的区域设置中获取字段
- 如果为空,则检查该区域设置是否有依赖项,如果有,则 a. 如果依赖项的默认语言与当前区域设置不同,则沿当前区域设置的语言标签链向下工作 b. 将依赖项设置为新的区域设置,继续步骤 2
- 如果没有为该区域设置设置依赖项,则沿语言标签链向下工作
- 如果默认语言为空,则使用回退作为新的区域设置并继续步骤 2
- 最后一步:放弃,返回 null
语言标签链
给定以下语言标签: de-DE-1996
,它由三个子标签组成。当沿语言标签链工作时,这意味着最后一个子标签将被删除,并将尝试为剩余的语言标签获取字段的内容,直到只剩下默认语言。
用法
在 schema.xml 中使用
在 schema.xml 中的用法与 i18n 行为非常相似。
<table name="book"> <column name="id" type="INTEGER" primaryKey="true" required="true" autoIncrement="true" /> <column name="title" type="VARCHAR" size="255" /> <column name="author" type="VARCHAR" size="255" /> <behavior name="l10n"> <parameter name="i18n_columns" value="title" /> </behavior> </table>
参数与 i18n 参数 相同,但 default_locale
不存在。
使用 API
对于您的应用程序,您只需做三件事情,就可以开始使用 propel 而无需使用任何 l10n/i18n 行为。
- 设置默认区域设置
- 设置回退区域设置
- 设置您的依赖项
示例
PropelL10n::setLocale('de'); // this is mostly the language a user decided to use PropelL10n::setFallback('en'); // that's the default language of your app PropelL10n::setDependencies([ // that's the locales you have available 'de' => 'en' 'de-CH' => 'de', 'de-AT' => 'de', 'ja' => 'en' ]);
您就可以开始了。
从对象中检索
$book = new Book(); $book->setTitle('Lord of the Rings'); $book->setLocale('de'); $book->setTitle('Herr der Ringe'); $book->setLocale(null); echo $book->getTitle(); // Lord of the Rings - using the default locale (`en`) echo $book->getTitle('de-DE'); // Herr der Ringe $book->setLocale('de-DE'); echo $book->getTitle(); // Herr der Ringe echo $book->getTitle('ja-JP'); // Lord of the Rings - using fallback locale (`en`)
查询数据库
您可以使用您熟悉的方法来查询数据库
// default locale: de // contains all books with a german title starting with 'Harry Potter' $books = BookQuery::create() ->filterByTitle('Harry Potter%') ->find(); $books = ...; // the book lord of the rings as searched with an english title name $book = BookQuery::create() ->findOneByTitle('Lord of the Rings', null, 'en'); $book = ...; // all harry potter books searched with the japanese title $books = BookQuery::create() ->setLocale('ja') // overwrites query default locale ->findByTitle('Harī Pottā%'); $books = ...; // find lord of the rings with the japanese title, overwrite locale with the filter method $book = BookQuery::create() ->findOneByTitle('Yubiwa Monogatari', null, 'ja'); $book = ...;
最佳实践
使用最短的区域设置!只有在需要更具体的情况下才使用较长的语言标签。如果没有必要,直接使用 de
而不是 de-DE
(这本身也是一种冗余)。然而,使用 de
和 de-CH
,因为可能存在针对德国或瑞士人的不同内容(例如,联系地址,一个针对德国,另一个针对瑞士)。
性能
我非常确定这是一个性能噩梦。目前只使用了 propel API 方法,意味着还没有手动查询。性能优化将在 propel 将 data-mapper
分支合并到 master
后开始。欢迎提出建议,请将其发布到问题跟踪器。
参考文献
关于本地化、语言标签的材料很多。有时它涉及到寻找正确的子标签,这可能足够复杂。以下是一些好的参考文献
相关规范