Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "i18n helper"

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 
(3 comments)

call the translation helper from an action

You sometimes need to access the __() helper for text translation outside of a template. There are two ways to do it.

You can include the I18NHelper.php manually in your action, or use the symfony command to include a helper (sfLoaded::LoadHElpers()):

sfLoader::LoadHelpers(array('I18N'));

The problem is that this will add the helper functions to the global namespace. Alternately, use the following syntax:

sfContext::getInstance()->getI18n()->__($text, $args, 'messages');

Note: prior to 1.0, you had to do this (now deprecated)

sfConfig::get('sf_i18n_instance')->__($text, $args, 'messages');

(updated 29/11/06)

by Francois Zaninotto on 2006-07-24, tagged helper  i18n 
(3 comments)

Localized image Helper

Returns localized image <img> tag bassed on the the user culture and the image file location in the filesystem.

Based on the technique recomended in the symfony advent calendar day twenty-three

Directory tructure:

Inside images directory, create one directory for each culture in use plus one (named 'no-loc' by default) for non-localized images or images that doesn't change for different cultures.

Example:

web/images/en_US
web/images/es_MX
web/images/fr_FR
................
web/images/non-loc

<?php
/**
 * Returns localized image <img> tag bassed on the the user culture
 * and the image file location in the filesystem
 * 
 * If the image file is found under the directory corresponding
 * the user culture, it returns its img tag,
 * else returns img tag under 'non-loc'directory.
 * 
 *  Structure example:
 * 
 *   web/images/en_US
 *   web/images/es_MX
 *   web/images/fr_FR
 *   ................
 *   web/images/non-loc
 * 
 * $source   -  relative path within culture directory
 * $options  -  is a normal options array passed to image_tag function
 * 
 */
function local_image_tag($source, $options = array())
{
  $images_path       = sfConfig::get('sf_root_dir') . '/web/images';
  $non_localized_dir = 'non-loc';
  $culture           = sfContext::getInstance()->getUser()->getCulture();
  $default_ext       = 'png';
 
  if (strpos(basename($source), '.') === false)
  {
    $source .= '.'.$default_ext;
  }
 
  if (!$culture || !(is_file($images_path.'/'.$culture.'/'.$source)))
  {
    return image_tag($non_localized_dir.'/'.$source, $options);
  }
 
  return image_tag($culture.'/'.$source, $options);
}
?>

Call example:

<?php
echo local_image_tag('some/image', 'alt=image');
/*
Will search in /web/images/<culture>/some/image.png,
and if file is not found, will return image tag in
/web/images/non-loc/some/image.png
*/
?>
by David Regla on 2006-05-25, tagged helper  i18n 
(1 comment)