Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Language switcher

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

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

Page: 1
Go to End
Author Topic: 2960 Views
  • mazu
    Avatar
    Community Member
    3 Posts

    Language switcher Link to this post

    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

  • kudesign
    Avatar
    Community Member
    64 Posts

    Re: Language switcher Link to this post

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

  • silverseba
    Avatar
    Community Member
    26 Posts

    Re: Language switcher Link to this post

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

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Language switcher Link to this post

    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

  • rotor
    Avatar
    Community Member
    3 Posts

    Re: Language switcher Link to this post

    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>

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Language switcher Link to this post

    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

    2960 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.