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, biapar, Willr, Ingo, swaiba

Language switcher


Go to End
Reply


6 Posts   3294 Views

Avatar
mazu

Community Member, 3 Posts

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

Avatar
kudesign

Community Member, 64 Posts

2 August 2009 at 7:22pm

Same problem for me, have you or anyone found a solution to this? thanks.

Avatar
silverseba

Community Member, 26 Posts

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 page

Just 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 %>

Avatar
Juanitou

Community Member, 323 Posts

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

Avatar
rotor

Community Member, 3 Posts

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>

Avatar
borriej

Community Member, 267 Posts

9 November 2010 at 3:44am

Edited: 09/11/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&ccedil;ais or espa&ntilde;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&ccedil;ais'){
				$WhichLanguage=French;
			}else if ($WhichLanguage== 'espa&ntilde;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