Code snippets for symfony 1.x

Navigation

Switch culture from any page of your site and improve SEO

CONTEXT

Symfony internationalization is normally managed using session variables. To switch culture you can visit a page that first changes you session variable and then send you back to the page you were visiting, but this potentially breaks your SEO.

Adding culture in every URL is a better solution, but if you want to go back to the page you were visiting, you have to deal with redirection by yourself.

This widget solves the redirection issue by creating localized urls.

CODE

Save your file here: apps/appname/lib/helper/I18nUrlHelper.php

/*
 * Generate a localized version for the URL you are visiting.
 */
 
function localized_current_url($sf_culture = null)
{
  if (! $sf_culture)
  {
    throw new sfException(sprintf('Invalid parameter $sf_culture "%s".', $sf_culture));
  }
 
  $routing    = sfContext::getInstance()->getRouting();
  $request    = sfContext::getInstance()->getRequest();
  $controller = sfContext::getInstance()->getController();
 
  // depending on your routing configuration, you can set $route_name = $routing->getCurrentRouteName()
  $route_name = '';
 
  $parameters = $controller->convertUrlStringToParameters($routing->getCurrentInternalUri());
  $parameters[1]['sf_culture'] = $sf_culture;
 
  return $routing->generate($route_name, array_merge($request->getGetParameters(), $parameters[1]));
}
 

EXAMPLE

Obviously, you must add the :sf_culture token in every rule of your application routing.yml:

product_page:
  url:   /:sf_culture/product/:id
  requirements: { sf_culture: (?:en|es|fr|it|de) }
  param: { module: product, action: show }
 
homepage:
  url:   /:sf_culture
  requirements: { sf_culture: (?:en|es|fr|it|de) }
  param: { module: page, action: home }

default_symfony:
  url:   /symfony/:sf_culture/:action/*
  requirements: { sf_culture: (?:en|es|fr|it|de) }
  param: { module: default }
 
default_index:
  url:   /:sf_culture/:module
  param: { action: index }
  requirements: { sf_culture: (?:en|es|fr|it|de) }
 
default:
  url:   /:sf_culture/:module/:action/*
  requirements: { sf_culture: (?:en|es|fr|it|de) }
 

This code let you create a list of link to the localized versions. The list of languages/cultures is taken from your config file.

<div>
  <?php foreach(sfConfig::get('app_languages_available') as $language) { ?>
    <a href="<?php echo localized_current_url($language); ?>"><?php echo $language; ?></a>  
  <?php } ?>
</div>
 
by Davide Fedrigo on 2009-09-28, tagged culture  helper  i18n  navigation  routing 

Comments on this snippet

gravatar icon
#1 Bela Hausmann on 2009-09-29 at 09:01

Nice widget. I'm using sfLanguageSwitchPlugin right now, which does about the same thing. Maybe you could send a patch to sfLanguageSwitchPlugin because your localized_current_url() function is more elegant than the one in sfLanguageSwitchPlugin (I think :))

gravatar icon
#2 Danny vogler on 2009-10-02 at 11:53

Great tips to improve SEO. I've got some more snippets at www.snippet.com

Thanks, Dan

gravatar icon
#3 David Jeanmonod on 2010-04-28 at 04:53

I have used this snippet in le layout.php

Everything was fine until, a user is on the 404 error page. At that's moment there is no module and no action define, so the generated url is randomly generated. To fix it, I'have to add trhe following lines:

// If no module are define, maybe we are on the 404 error page if (! isset($parameters[1]['module']) ){ $parameters[1]['module'] = sfConfig::get('sf_error_404_module'); $parameters[1]['action'] = sfConfig::get('sf_error_404_action'); }

Thanks