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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

translatable partly working--questions on minor issues

Go to End

19 Posts   5334 Views


Community Member, 203 Posts

23 January 2010 at 12:27pm

Edited: 23/01/2010 12:30pm

I changed my function to this:

public function LangMenu() {
    if($this->URLSegment == "Security") return false;
    $availTransl = Translatable::getTranslations();
    if(count($availTransl) == 0) {return false;}

    $langs = Translatable::get_existing_content_languages();
    $data = new DataObjectSet();

    foreach(array_keys($langs) as $code) {
	$page = $this->getTranslation($code);
	if (isset($page)) {
            $lang = i18n::get_lang_from_locale($code);
            $data->push(new ArrayData(array(
            'Name' => i18n::get_language_name($lang, true),
             'Title' => $page->Title,
             'Link' => Director::baseURL() . $page->URLSegment,
             'Code' => $code
    return $data;

Where the important part is if (isset($page)): you use the existing content languages, but it is not necessarily so that all the translations are there. Plus the if(count($availTransl) == 0) {return false;} for if there is just one text and no translations - in that case you do not need a menu. Because of that you have to give a <% if LangMenu %> in your code.

At least that is what I think is the right way, anyone sees any problem with this?


Community Member, 10 Posts

24 January 2010 at 6:49am

Thank you.
I checked. The code is working fine now. So i want to do something more. so i have 2 Questions:
My Website is now in two languages: German and french. In the template, the language switcher should link all available languages except the current language. How is it possible?
It is possible instead of language shortcode to show the Flag?
thank you


Community Member, 203 Posts

24 January 2010 at 7:21am

Edited: 24/01/2010 7:25am

Show a flag: Use css to do that, I use something like this:

<% if LangMenu %>
 <ul id="languageMenu" >
 	<% control LangMenu %>
 	<li class="$Name"><a href="$Link" hreflang="$Code" title="$Name">$Name</a></li>
	<% end_control %>
<% end_if %>

ul#languageMenu {
	width: 180px;
	height: 43px;
	list-style: none;

ul#languageMenu li.yourlanguage {
	display: block;
	height: 43px;
	width: 60px;
	float: left;
	background: url(../images/yourlanguage.gif) no-repeat;
ul#languageMenu li.yourlanguage a {
	display: block;
	height: 43px;
	width: 60px;
	font-size: 0.1px;
	line-height: 0.1px;
	text-indent: -10000px;
	background: url(../images/yourlanguage.gif) no-repeat;
	white-space: nowrap; /* required in IE 6 */
    outline: 0; /* prevent dotted border in Firefox */

Where you have to replace yourlanguage.gif with the image you want to use and li.yourlanguage and li.yourlanguage a with the actual language you are using (and of course you have to change the dimensions of the elements to suit your need). You have to run a test to see what the language name is that the function returns. (Disclaimer: I have changed the code a bit to make it easier to understand so there could be some errors there, you could use the (more or less) standard code used for menus to create hover effects and use one image that holds all your elements etc YMMV)

Regarding the first point: you have to compare the current language with the language of the translation of the page during the loop with an if statement. You have to compare $code with $this->Locale.

if (isset($page)) { 
 if (!($code == $this->Locale)) {

//continue looping


EDIT: for the use of flags you can of course also do: <a href="$Link"><img src="/imagelocation/$Name.gif"></a> where $Name.gif returns the name of your image and imagelocation is the location of the image

Go to Top