Zend_Controller_Router: Get language from translated segment
I want to use a URL rewrite on my site:
/:@controller/:@action/
So I want to use translated segments on route and I want to detect requ开发者_StackOverflowested language from these translated segments. For example, if user request a url like this:
/user/profile/
then I could understand that requested language is English. And if user request a url like this:
/kullanici/profil/
then I coult understand that requested language is Turkish. How can I do this with Zend_Controller_Router?
I dunno if thats such a good idea. Because in order to do that you first have to determine the language of at least one of the route segments. In order to do that you either have to know the mapping up front (ie. this matches the profile route so is it in english or tukish?) or you would need to scan the route segments against a dictionary of turkish/english route segments. The former is going to require you to make 2 routes for every route - one in turkish and one in english, while the latter is going to require you pay a penalty in request processing time on top of the time it already takes to actually match the route. IMO it would better to stick with the typical :lang/:controller/:action
type of route construct.
That said if youre going to do it i would Make a new route type to handle matching the URI to a language. This would then set a language paramter for you i18n, but it should also reset the URI to a specific basline language which you will actually match to the standard route. I would then use Zend_Controller_Router_Route_Chain
to chain the two together.
We solved our problem by creating a new router with extending Zend_Controller_Router_Route. We overrode "match" method of class and added some lines of code to original match code.
.....
foreach( $translateMessages as $key => $val ) {
if (($originalPathPart = array_search($pathPart, $val)) !== false) {
$pathPart = $originalPathPart;
if (!$this->_localeSet) {
$locale = Zend_Registry::get('Zend_Locale');
$locale->setLocale($key); // Set Locale by translated key language
$this->_localeSet = true; // Added to class with default value false
$this->_activeLocale = $key; // Added to class with default value ''
}else{
// A second translated key but this is not same language.
// Then rise 404 error
if ($this->_activeLocale != $key) {
//FIXME: Rise 404 error
throw new Exception("URL Not Found");
}
}
}
}
.....
精彩评论