Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Widgets /

Discuss SilverStripe Widgets.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

SS 2.4.1 Tranlate Widget


Reply


1152 Views

Avatar
Bambii7

Community Member, 254 Posts

5 August 2010 at 3:29pm

I can't see how I can get hold of the develop simon_w so I'll post the code here. This is in regards to Language Chooser Widget [v0.2.3] http://silverstripe.org/Language-Chooser-Widget-2/

Nothing needs changing apart from LanguageChooser.php all the logic still works just some of the class/function names have changed.

<?php

class LanguageChooser extends Widget {
static $title = "Language Chooser";
static $cmsTitle = "Language Chooser";
static $description = "Allows you to choose your language";

static $db = array("ShowCurrent" => "Boolean");
static $defaults = array('ShowCurrent' => false);

function getCMSFields(){
return new FieldSet(new CheckboxField('ShowCurrent', _t('LanguageChooser.SHOWCURRENT', 'Show current language')));
}

function Chooser() {
      if(!Controller::curr()) {
         return;
      }
$langs = Translatable::get_existing_content_languages ();
$data = new DataObjectSet();
foreach(array_keys($langs) as $code) {
if($code == Translatable::current_lang() && !$this->ShowCurrent) {
continue;
}
$page = Translatable::get_one_by_lang("SiteTree", $code, "`SiteTree`.ID = " . Controller::curr()->ID);
if(!$page) {
$data->push(new ArrayData(array('name' => i18n::get_locale_name($code), 'link' => Director::protocolAndHost() . Director::baseURL() . '?locale=' . $code)));
} else {
$data->push(new ArrayData(array('name' => i18n::get_locale_name($code), 'link' => Director::protocolAndHost() . Director::baseURL() . $page->URLSegment . '?locale=' . $code)));
}
}
return $data;
}

function WidgetHolder() {
if(count(Translatable::get_existing_content_languages()) > 1) {
return $this->renderWith("WidgetHolder");
}
return "";
}
}

?>

The i18n::get_locale_name($code) returns the language name which is used as the link. There are free sets of flag icons fr unicode, it'd be very nice to include these as the link, optionally instead of language names.