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.

Template Questions /

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

PageByLang function


Go to End


5 Posts   2372 Views

Avatar
chefkoch

Community Member, 4 Posts

8 April 2010 at 3:55am

Hello,

i'm having trouble with the PageByLang function explained at
http://doc.silverstripe.org/doku.php?id=multilingualcontent#templates

Well I pasted the code into my Page_Controller and added <% PageByLang(nuestro-despacho,es) %> to my page.ss. But it looks like the parser doesn't run the function. It just puts '<% PageByLang(nuestro-despacho,es) %>' as plain text into my html.

1. Is 'es' as second parameter correct? In the example it is: <% PageByLang(Kontakt,de) %>, but the locale isn't 'de' it is 'de_DE'

2. Is there any code it have to put around my function in the template?

@dev: Is there any example for the usage of this function?

Regards Chefkoch

Avatar
chefkoch

Community Member, 4 Posts

8 April 2010 at 7:39pm

Well maybe i explain it a bit more precisely:

<!--<% PageByLang(nuestro-despacho,es_ES) %>--> -> doesn't work
<% if Locale = es_ES %>
<h2><a href="./nuestro-despacho/" title="Contacto">Contacto</h2>
<% else_if Locale = de_DE %>
<h2><a href="./anwaltsbuero/" title="Kontakt">Kontakt</a></h2>
<% else_if Locale = en_GB %>
<h2><a href="./office/" title="Office">Our Office</a></h2>
<% else_if Locale = ca_ES %>
<h2><a href="./el-nostre-despatx/" title="Despatx">El nostre despatx</a></h2>
<% else %>
<% sprintf(_t('SHOWINPAGE','Show page in %s'),$Locale) %>
<% end_if %>
-> hardcoded version depending in <locale> works

but I have a lot of more links an I don't want to hardcode them all!
Did I missunderstand the PageByLang function? Does it return a link to the page depending on <locale>?

When I use it like
<h2><a href="<% PageByLang(nuestro-despacho,es_ES) %>" title="Contacto">Contacto</h2>
the function isn't executed at all? Does anybody know why?

Regards chefkoch
Thanks for answers =)

Avatar
Willr

Forum Moderator, 5523 Posts

8 April 2010 at 8:00pm

Note the docs have an error - should be <% control PageByLang(..... %>$Whatever<% end_control %>. They have been updated.

Avatar
chefkoch

Community Member, 4 Posts

8 April 2010 at 10:20pm

thank willr. just noticed the changes in the doku and fixed the problem

Avatar
Ironcheese

Community Member, 36 Posts

28 May 2010 at 6:34am

Edited: 29/05/2010 10:21am

Hey guys,

when i try to use the function from the updated tutorial site, it breaks my site with the error message:
"Fatal error: Call to a member function getTranslation() on a non-object in [...]/mysite/code/Page.php on line 143"

is there a way to fix this...

thanks

EDIT::

Today i tried it on SilverStripe 2.4 and it worked like a charm. I modified the function a little bit and now it has a nice fallback to english, if the requested page does not exist in the current locale ;)

public function PageByLang($url, $lang) {
		$SQL_url = Convert::raw2sql($url);
		$SQL_lang = Convert::raw2sql($lang);
	     
			
		$page = Translatable::get_one_by_lang('SiteTree', $SQL_lang, "URLSegment = '$SQL_url'");
	    
		if ($page->Locale != Translatable::get_current_locale()) {
			
			// Fallback to English
			if($page->hasTranslation(Translatable::get_current_locale())){
				$page = $page->getTranslation(Translatable::get_current_locale());
			}else{
				$page = $page->getTranslation('en_US');
			}
			
		}
		
		return $page;
	}

I need the same mechanism in the normal <% control Menu(x) %> function... but I just can't figure out how :S