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.

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

[ solved] Language Switcher Translatable


Go to End


4 Posts   1141 Views

Avatar
m4kx

Community Member, 5 Posts

18 August 2015 at 9:55pm

Edited: 18/08/2015 9:56pm

Hello there !

i'm currently trying to add a language switcher to a website with translatable
i actually did this in my .ss

<ul class="translations">
	<li class="currentLang">$ContentLocale.LimitCharacters(2,'')</li>
	<% if Translations %>
		<% loop Translations %>
			<li class="$Locale.RFC1766"><a href="$Link" hreflang="$Locale.RFC1766"><% sprintf(_t('SHOWINPAGE','%s'),$Locale.LimitCharacters(2,'')) %></a></li>
		<% end_loop %>
	<% end_if %>
</ul>

This works but the current language is always getting in the first place... and its not what i want. ( i'd like it to always be in the same order )
Is there a way to use the loop with the current language inside?
should i modify the Translatable.php?
Thx for you help!

Bye :)

Avatar
Pyromanik

Community Member, 419 Posts

19 August 2015 at 2:36am

A good rule when developing is to never change code you didn't write. It makes for maintenance nightmares in the future.

If there is no existing method to do what you want, you could look at using an Extension however.

Avatar
m4kx

Community Member, 5 Posts

19 August 2015 at 3:12am

Edited: 19/08/2015 3:29am

Hello! thanks for you reply :)
The fact is that translatable is already installed and it works pretty fine.
my problem is only about how the language switcher is displayed
example with 3 languages : EN / FR / NL
So when the site is loaded in english, the swticher is displayed like EN / FR / NL
then if i change to NL, my site is in Dutch but my switcher is NL / EN / FR
and then if i change to FR my switcher looks like FR / EN / NL
i know this is because of the way i made it in my template.ss but the part where

<% loop Translations %>
... // loops only on the available languages, not the current one
<% end_loop %>

i'd just like my switcher to stay in the same order, whatever language i pick
i've seen another post here where a guy said :
"I'm trying to put a language switcher on my site. I have two language versions Polish (main) and English (second). In both versions I want to have listed Polish and English versions, so I removed filter from getTranslations in Translatable.php"
so i was wondering what to do...

if i understand what you say is that i have to extends Translatable and overide the getTranslations method to make my own one?

Avatar
m4kx

Community Member, 5 Posts

19 August 2015 at 9:38am

Edited: 19/08/2015 10:29am

ok so in Translatable.php
in the getTranslations function
there are these lines

if($locale) {
	$filter .= sprintf(' AND "%s"."Locale" = \'%s\'', $baseDataClass, Convert::raw2sql($locale));
} else {
	// exclude the language of the current owner
	$filter .= sprintf(' AND "%s"."Locale" != \'%s\'', $baseDataClass, $this->owner->Locale);
}

and if i put comments on the else line
// exclude the language of the current owner
// $filter .= sprintf(' AND "%s"."Locale" != \'%s\'', $baseDataClass, $this->owner->Locale);

Then in my translation.ss file i did
<ul class="translations">
	<% if Translations %>
		<% loop Translations %>
			<% if $Top.Locale = $Locale %>
				<li class="currentLang">$Locale.LimitCharacters(2,'')</li>
			<% else %>	
				<li class="$Locale.RFC1766"><a href="$Link" hreflang="$Locale.RFC1766">$Locale.LimitCharacters(2,'')</a></li>
			<% end_if %>
		<% end_loop %>
	<% end_if %>
</ul>

this works exactly how i need
so now i will find a way to override the getTranslations method in my app