21301 Posts in 5735 Topics by 2603 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2330 Views |
-
Language switcher

25 July 2009 at 8:48pm
Hi,
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. to get all translations including current. I want to have text links to language versions with the name of a language as a link, in current language. So in Polish version I want to have 'Polski', 'Angielski', and in English 'Polish', 'English'. I tried something like this in my Page.ss<% if Translations %>
<ul class="translations">
<% control Translations %>
<li class="$Locale.RFC1766">
<a href="$Link" hreflang="$Locale.RFC1766" title="$Title">
<% sprintf(_t('$Locale.Nice','Show page in %s'),$Locale.Nice)%>
</a>
</li>
<% end_control %>
</ul>
<% end_if %>But this is not working. I have translation files in /mysite/lang folder. I would appreciate any help.
--
mazu -
Re: Language switcher

2 August 2009 at 7:22pm
Same problem for me, have you or anyone found a solution to this? thanks.
-
Re: Language switcher

10 August 2009 at 9:47pm
Based on the modification of the language switcher of Juanito in this post: http://www.silverstripe.org/releases-and-announcements/show/260948?start=0 I wrote my custom language switcher.
Here is how the language switcher works:
1. it gets the default language of the site
2. it gets all translated versions of the current page
3. it generates a DataObjectSet with the locale code, language name, urlSegment and link to each translation of the pageJust place the following function in the code of your PageClass:
/**
* Returns a DataObjectSet with all available translations of the current page
* @return DataObjectSet
*/
function getTranslationsFunc() {
if($this->URLSegment == "Security") return "";
$langs = Translatable::get_existing_content_languages();
$data = new DataObjectSet();
// generate entry with only the defaul language if no translations of this page are available
$availTransl = Translatable::getTranslations();
if(count($availTransl) == 0) {
$code = Translatable::default_locale();
$data->push(new ArrayData(array(
'name' => i18n::get_language_name(i18n::get_lang_from_locale($code), true),
'link' => Director::baseURL() . $this->URLSegment,
'urlSegment' => $this->URLSegment,
'code' => $code,
'current' => 'current"'
)));
}
else {
foreach(array_keys($langs) as $code) {
// get country code (e.g. "de")
$lang = i18n::get_lang_from_locale($code);
// current language?
$current = ($code == Translatable::get_current_locale()) ? 'current': '';
$page = $this->getTranslation($code);
$data->push(new ArrayData(array(
'name' => i18n::get_language_name($lang, true),
'link' => Director::baseURL() . $page->URLSegment,
'urlSegment' => $page->URLSegment,
'code' => $code,
'current' => $current
)));
}
}
return $data;
}Then place this code in your Page Controller (the controller just calls the class function):
/**
* Returns a DataObjectSet with all available translations of the current page
* @return DataObjectSet
*/
function getTranslations() {
// call method of Page Object
return $this->getTranslationsFunc();
}Then you can call this Controller Function from your template like this:
<% if getTranslations %>
<ul>
<% control getTranslations %>
<li class="$selected"><a href="$link">$name</li>
<% end_control %>
</ul>
<% end_if %> -
Re: Language switcher

29 August 2009 at 5:19am
Thank you! You sent it on my birthday… I'm sure I'll learn something, because I feel my language switcher is somewhat primitive.
Best regards,
Juan -
Re: Language switcher

1 September 2009 at 8:52pm
thanks a lot for this post, very useful.
mini typo:
line in template code
<li class="$selected"><a href="$link">$name</li>
should be
<li class="$current"><a href="$link">$name</li> -
Re: Language switcher

9 November 2010 at 3:44am Last edited: 9 November 2010 3:48am
Some extra code:
<% if getTranslations %>
<ul>
<% control getTranslations %>
<li class="$name $current"><a href="$link" ><span>$name</a></li>
<% end_control %>
</ul>
<% end_if %>When you want French of Spanish the class name would render: français or español
So you cant use this as a className in CSS, because of the illegal characters
//EDIT
$WhichLanguage= i18n::get_language_name($lang, true);
if ($WhichLanguage== 'français'){
$WhichLanguage=French;
}else if ($WhichLanguage== 'español' ){
$WhichLanguage=Spanish;
}
$data->push(new ArrayData(array(
'name' => $WhichLanguage,
'link' => Director::baseURL() . $page->URLSegment,
'urlSegment' => $page->URLSegment,
'code' => $code,
'current' => $current
)));
}
}now you can make .Spanish and .French classes in your CSS
| 2330 Views | ||
|
Page:
1
|
Go to Top |


