views:

212

answers:

2

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 requested 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?

A: 

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.

prodigitalson
It's not a good thing that using English words on URL for a completely Turkish page for SEO techniques. And using Turkish words for English pages also...In our Router configuration Zend framework already gets requested controller from this type of translated segments. Only problem is, I couldn't get what translation used when getting current controller.If I use several routes for every language, my url management will ugly, I can't generate dynamic urls simply etc. If it's possible, I want to use one rule for all languages.
Murat Corlu
@Murat: "Only problem is, I couldn't get what translation used when getting current controller." Im sorry can you go into a little more detail here? It seems like if ZF is working the way you state then the `locale` should be avialable somewhere in the application stack like on the Request or in the Bootstrap. Where does the translation occur currently?
prodigitalson
A: 

We solved our problem by creating a new router with extending Zend_Controller_Router_Route. We overrided "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");
       }    
    }    
  }    
}    
.....
Murat Corlu