rayamedia / yima-localize
该软件包最新版本(dev-master)没有提供许可证信息。
多语言数据库(可翻译字段)的本地化功能,数据完全本地化的 CLDR 数据,以及许多预定义的有用本地化助手。
dev-master
2014-09-08 05:49 UTC
Requires
- php: >=5.3.3
- poirot/cldr: dev-master
- poirot/datetime: dev-master
- rayamedia/yima-plugin-installer: dev-master
This package is auto-updated.
Last update: 2024-09-24 04:22:59 UTC
README
此模块是 Yima 应用程序框架的一部分
[zf2 模块] 多语言数据库(可翻译字段)的本地化功能,数据完全本地化的 CLDR 数据,以及许多预定义的有用本地化助手。
使用多语言 TableGateway 功能
设置数据库
- 在您的应用程序内部设置数据库适配器。
- 在您的 SQL 服务器中创建新的数据库。
- 通过将 yimaLocalize_i18n.sql 导入到您的 MySQL 服务器中创建 i18n 表。您可以在模块数据文件夹中找到此文件
创建可翻译的表
注意:您可以将此表注册为服务在服务管理器中,以后您可以从 SM 调用此表。
use yimaBase\Db\TableGateway\AbstractTableGateway; use yimaLocalize\Db\TableGateway\Feature\TranslatableFeature; class SampleTable extends AbstractTableGateway { # db table name protected $table = 'sampletable'; // this way you speed up running by avoiding metadata call to reach primary key // exp. usage in Translation Feature protected $primaryKey = 'sampletable_id'; public function init() { // this is translatable fields of table $feature = new TranslatableFeature( array('title', 'description') ); $this->featureSet->addFeature($feature); } }
在您的控制器或您访问 ServiceManager 的任何地方,您有这些内容
- 添加您当前区域的 数据 note: 当我们从 SQL 中获取数据时,只获取从相关区域插入的插入查询中的数据
$serviceLocator = $this->getServiceLocator(); // get sample table service $projectTable = $serviceLocator->get('yimaLocalize.Model.TableGateway.Sample'); $locale = \Locale::getDefault(); $title = ($locale == 'fa_IR') ? 'عنوان' : 'Title'; $descr = ($locale == 'fa_IR') ? 'توضیحات' : 'Description'; // Translatable Feature will automatically detect locale from \Locale::getDefault() $projectTable->insert( array( 'title' => $title, 'description' => $descr, 'image' => uniqid().'.jpg', ) ); // we can also use update query as well $projectTable->update( array( 'description' => 'This is '.$locale.' title updated from', ), array('sampletable_id'=>1 ) );
- 为数据表添加其他区域语言的翻译 note: 每次您查询所需区域语言的翻译时,将自动获取
$serviceLocator = $this->getServiceLocator(); // get sample table service $projectTable = $serviceLocator->get('yimaLocalize.Model.TableGateway.Sample'); $locale = \Locale::getDefault(); $title = ($locale == 'fa_IR') ? 'عنوان' : 'Title'; $descr = ($locale == 'fa_IR') ? 'توضیحات' : 'Description'; $projectTable->insert( array( 'title' => $title, 'description' => $descr, 'image' => uniqid().'.jpg', ) ); $currentLocale = $locale; $transLocale = 'fa_IR'; // add translation for other locale ... { // set locale for translation feature $projectTable->apply('setLocale', array($transLocale)); // add translation $projectTable->apply( 'addTranslationRows', array( array( 'title' => $transLocale.' Title', 'description' => $transLocale.' Description', ) ,$projectTable->getLastInsertValue() ) ); // bring back current locale, as you want !! $projectTable->apply('setLocale', array($currentLocale)); // ... }
- 从表中获取、更新、删除
$projectTable->apply('setLocale', array('fa_IR')); // Select Specific Data For "fa_IR" locale $select = $projectTable->getSql()->select() //->columns(array('t'=>'title','image','url')) //->where(array('sampletable_id' => 1)) ; $rowset = $projectTable->selectWith($select); foreach ($rowset as $projectRow) { \Zend\Debug\Debug::dump($projectRow); } // Delete Query Will Remove All Data And Translation $projectTable->delete( array('sampletable_id'=>1 ) );
使用本地化日历
这是 locale() 助手插件之一,它将日期转换为本地化结果。让我们看一些示例。
# in view or controllers as helper # Determine the detected locale is fa_IR echo $this->locale(); // fa_IR // this will print out date in persian calendar echo $this->locale()->datetime()->format('Y-m-d H:i:s'); // 1393-03-07 21:53:30 # Determine the detected locale is nl_BE echo $this->locale(); // nl_BE // this will print out date in persian calendar echo $this->locale()->datetime()->format('Y-M-d l'); // 2014-mei-28 woensdag