agopaul/zf-linguo

此包最新版本(dev-master)没有可用的许可信息。

提供多语言路由功能的Zend Framework 2模块

dev-master 2015-07-19 12:38 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:49:17 UTC


README

为ZF2提供真正的多语言路由支持

此zf2模块有助于处理完全本地化的URL

示例

假设你有一个具有两个完全本地化URL的操作

GET /en/contacts
GET /it/contatti

module/Application/config/config.php

<?php
return array(
	"router" => array(
		"router_class" => "Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack", // Add this
		'routes' => => array(
	
			// Route
			'contacts' => array(
				'type' => 'Linguo\Library\Mvc\Router\Http\MultiLangSegment', // Change this
				'options' => array(
					'route' => '/:lang/{contacts}',
					'defaults' => array(
						'__NAMESPACE__' => 'Application\Controller',
						'controller' => 'Site',
						'action' => 'contacts'
					),
				),
			),
			
		),
	),
);

配置

config/application.config.php

<?php
return array(
	'modules' => array(
		'Linguo',
	
		// Your modules here
	)
)

config/autoload/linguo.php

"linguo" => array(
	"langMap" => array(
		// locale - slug pair
		"en_US" => "en",
		"it_IT" => "it",
	)
)

module/Application/Module.php

<?php
/**
 * On bootstrap
 * @param MvcEvent $e
 */
public function onBootstrap(MvcEvent $e){
	
	/* @var $eventManager EventManager */
	$eventManager = $e->getApplication()->getEventManager();
	
	// Set translator locale based on the request URL
	$eventManager->attach($this->getServiceLocator()->get('Linguo\Library\Translator\SetLocaleFromUrlStrategy'));
}

限制

  • 语言参数必须命名为:lang
  • 路由没有非常优化(出于保守原因),可能比Segment路由慢。