sfFormLanguage displaying languages in their respective language
Is there a way to display languages using sfFormLanguage, but 开发者_Go百科in their respective language?
I mean, instead of this: English, French, Spanish
I want this: English, Français, Español
I know this question is marked as answered, but I just stumbled across the same problem and wanted to solve it in a more generic way, here is what I have.
Subclass sfFormLanguage with your own implementation and just override configure():
class myFormLanguage extends sfFormLanguage {
public function configure()
{
$this->setValidators(array(
'language' => new sfValidatorI18nChoiceLanguage(array('languages' => $this->options['languages'])),
));
$this->setWidgets(array(
'language' => new myWidgetFormI18nChoiceLanguage(array('languages' => $this->options['languages'])),
));
}
}
As you can see another widget is used for the actual selection, whose implementation is shown below.
class myWidgetFormI18nChoiceLanguage extends sfWidgetFormChoice
protected function configure($options = array(), $attributes = array())
{
parent::configure($options, $attributes);
$this->addOption('languages');
$this->addOption('add_empty', false);
// get the desired language codes
$languageCodes = isset($options['languages']) ? $options['languages'] : null;
if ($languageCodes == null) {
$languageCodes = array_keys(sfCultureInfo::getInstance()->getLanguages());
}
$languages = array();
// for each language code add the language to the choices by querying a corresponding culture info instance
foreach ($languageCodes as $lc) {
$languages = array_merge($languages, sfCultureInfo::getInstance($lc)->getLanguages(array($lc)));
}
$addEmpty = isset($options['add_empty']) ? $options['add_empty'] : false;
if (false !== $addEmpty)
{
$languages = array_merge(array('' => true === $addEmpty ? '' : $addEmpty), $languages);
}
$this->setOption('choices', $languages);
}
}
Both snippets are heavily inspired by the original classes from symfony, but I think they solve the problem much better. Note that no culture option is required (or allowed) as every language should be presented natively.
No, sfFormLanguage is fully-formed and vacuum-packed, so you have to take it as it is. It's a handy bit of code which I've used myself, but I guess it would be nice to have some configuration options.
However it should be quite easy to write your own by delving into the sfFormLanguage class and changing the parts which do the culture lookup and instead use a static list of your own creation.
There's an even simpler option though: just create your own dropdown list which connects to the sfFormLanguage form in the manner it expects:
<form action="/change-language">
<label for="language">Language</label>
<select name="language" id="language">
<option value="es">Español</option>
<option value="en" selected="selected">English</option>
<option value="fr">Français</option>
</select>
<input type="submit" value="ok">
</form>
What I have is this:
<?php $languages = array('en' => 'English', 'es' => 'Español', 'ca' => 'Català'); ?>
<?php foreach (array_keys($languages) as $lang):?>
<?php echo link_to($languages[$lang], 'language/changeLanguage?lang='.$lang)?>
<?php endforeach ?>
With links. I find this easier for people that just scans the page looking for their language.
精彩评论