phpixie/amalgama

PHPixie 国际化库

dev-master / 2.x-dev 2014-11-24 06:26 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:03:20 UTC


README

  • 易于使用
  • 默认语言不需要在URL中添加lang参数
  • 在翻译中使用参数
  • 自动路由修改

设置

  • composer.json 的 "require" 部分定义此包
"phpixie/amalgama": "2.*@dev"
  • 更新包
php composer.phar update -o  --prefer-dist
  • /assets/config/amalgama.php 下添加配置文件
return array(
  // The list of languages
  'list' => array('en', 'ru', 'kk'),
  // Default language
  'default' => 'en',
  // For using autorouting extension
  'autorouting' => true,
  // Names of routes for except them from autorouting extension
  'autoroutingExcept' => '^admin_'
);
  • 覆盖你的 Pixie.php
namespace App;

class Pixie extends \PHPixie\Amalgama\Pixie {
  ...
  protected function after_bootstrap() {
    parent::after_bootstrap();
  }
}
  • Pixie.php 中定义模块
protected $modules = array(
  ...
  'amalgama' => '\PHPixie\Amalgama'
);
  • 覆盖你的基本控制器
<?php

namespace App;

class Page extends \PHPixie\Amalgama\Controller {

  public function before() {
    parent::before();
    ...
  }

  ...

}
  • 定义路由(如果你不想要使用自动路由扩展)
'default' => array(
  array('(/<lang>)(/<controller>(/<action>(/<id>)))', array('lang' => '(en|ru)')
  array(
    'controller' => 'hello',
    'action' => 'index',
    'lang' => 'en'
  ),
),
  • /assets/config/amalgama 下添加翻译文件
//ru.php
<?php

return array(
  'Hello World!' => 'Привет мир!',
  'Hello <?>!' => 'Привет <?>!'
);

用法

// view example
<div><?php $__('Hello World!'); ?></div>
<div><?php $__('Hello <?>!', array($user->name); ?></div>
// lang switcher example
<?php foreach($this->helper->getLangList() as $lang) : ?>
  <?php if ($lang == $this->helper->getCurrentLang()) : ?>
    <span><?php echo $lang; ?></span>
  <?php else: ?>
    <a href="<?php echo $this->helper->langSwitchLink($lang); ?>"><?php echo $lang; ?></a>
  <?php endif; ?>
<?php endforeach; ?>
// Paginate example
...
$page = $this->request->param('page');

$comments = $this->pixie->orm->get('comment');
$pager = $this->pixie->paginate->orm($comments, $page, 10);
$pager->set_url_route('comments', array('lang' => $this->lang));
...
// Validate example
$validator->field('username')
  ->rule('filled')
  ->error($this->__('Field <?> must not be empty', array($this->__('username'))));