2010年4月27日星期二

RE: [fw-mvc] Language in URL: always a particular case !

Hi,

I think it is pretty easy to solve this. We call a method to handle this from the bootstrap before the routing process begins.
We have an xml-config-file that has the langauge informations for the site, it looks like this:

<?xml version="1.0"?>
<languages>
<enable>1</enable>
<route>true</route>
<default>de</default>
<lang>en</lang>
<lang>fr</lang>
<lang>es</lang>
<lang>nl</lang>
</languages>

The method we call from bootstrap looks like this:

<?php
public static function setLanguage()
{
$oLanguageConfig = Zend_Registry::get('config')->languages;
$sCurrentLang = $oLanguageConfig->default;

if ($oLanguageConfig->enable == 1) {
$bLangHit = false;
preg_match('@/[^/]*@', $_SERVER['REQUEST_URI'], $aUrlMatches);
if (!empty($aUrlMatches)) {
$sUrlMatch = substr($aUrlMatches[0], 1);
$aLangs = $oLanguageConfig->lang;
if (is_string($aLangs)) {
$aLangs = array($aLangs);
}
foreach($aLangs as $sLang) {
if ($sLang == $sUrlMatch) {
$bLangHit = true;
$sCurrentLang = $sLang;
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'],
(strlen($sLang) + 1));
break;
}
}
}
if (false == $bLangHit && true == $oLanguageConfig->route) {
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/'
. $sCurrentLang . $_SERVER['REQUEST_URI']);
exit();
}
}
Zend_Registry::set('currentLang', $sCurrentLang);
}
?>

In the config file you can enable or disable multi-langauge support.
You can chose if you want the default language forced to be included into URLs. (route = true)

The function only handles the langauge stuff, so the zf-router still can look for modules, controllers and actions and call the
error controller if the route can not be resolved.

Regards Viktor


> -----Original Message-----
> From: dbenjamin [mailto:bd.webdev@gmail.com]
> Sent: Tuesday, April 27, 2010 2:55 PM
> To: fw-mvc@lists.zend.com
> Subject: [fw-mvc] Language in URL: always a particular case !
>
> Hi, The same subject again and again. Adding the locale into
> URL to simulate a "one folder per language" structure with
> MVC. Just like the official Zend website does, but how
> exactly ? :) It's really complicated to add this behaviour
> with Zend routes and by browsing forums i never found a
> viable solution. The solution that comes up most often is : -
> redefine default routes - a plugin to get lang param and
> initialize Zend_Locale/Zend_Translate But that's not enough !
> We need to handle the following cases : - the lang param is
> not valid - the lang param is missing (ex.
> http://www.my-website.com/contact has to be redirected to
> http://www.my-website.com/defaultlang/contact) Let's assume
> an user enter the following url :
> http://www.my-website.com/dsqdsq, we have to return a 404
> error page, handled by the ErrorHandler plugin. But that's
> not possible, in our plugin we will get "dsqdsq" as the lang
> param value and throwing manually a 404 error from within a
> plugin is not handled by the ErrorHandler. Plus, we will
> maybe have to redirect automatically the user to :
> "http://www.my-website.com/en-gb/dsqdsq", but "dsqdsq" is a
> module ? a controller ? an action ? Who knows and how to
> redirect to a good url. In other words, if the first param is
> not a valid lang, we have to add the default language to the
> url and shift all the parts of the url (complicated). I'm
> wasting a loooot of precious time with that stuff ! :) Is
> there a solution that works for all cases ? Maybe with apache
> rewrite rules ? Thanks. (and sorry if my english is sometimes
> hard :p) Cdt, Benjamin.
>
> ________________________________
>
> View this message in context: Language in URL: always a
> particular case !
> <http://zend-framework-community.634137.n4.nabble.com/Language
> -in-URL-always-a-particular-case-tp2067501p2067501.html>
> Sent from the Zend MVC mailing list archive
> <http://zend-framework-community.634137.n4.nabble.com/Zend-MVC
> -f663775.html> at Nabble.com.
>
>

没有评论: