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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

SS 2.4.1 Tranlate Widget


Go to End


1937 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.