Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » translatable partly working--questions on minor issues

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 2 3
Go to End
Author Topic: 3013 Views
  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: translatable partly working--questions on minor issues Link to this post

    I changed my function to this:

    public function LangMenu() {
    if($this->URLSegment == "Security") return false;
    $availTransl = Translatable::getTranslations();
    if(count($availTransl) == 0) {return false;}

    $langs = Translatable::get_existing_content_languages();
    $data = new DataObjectSet();

    foreach(array_keys($langs) as $code) {
       $page = $this->getTranslation($code);
       if (isset($page)) {
    $lang = i18n::get_lang_from_locale($code);
    $data->push(new ArrayData(array(
    'Name' => i18n::get_language_name($lang, true),
    'Title' => $page->Title,
    'Link' => Director::baseURL() . $page->URLSegment,
    'Code' => $code
    )));
    }
    }
    return $data;
    }

    Where the important part is if (isset($page)): you use the existing content languages, but it is not necessarily so that all the translations are there. Plus the if(count($availTransl) == 0) {return false;} for if there is just one text and no translations - in that case you do not need a menu. Because of that you have to give a <% if LangMenu %> in your code.

    At least that is what I think is the right way, anyone sees any problem with this?

  • blaise4714
    Avatar
    Community Member
    10 Posts

    Re: translatable partly working--questions on minor issues Link to this post

    Thank you.
    I checked. The code is working fine now. So i want to do something more. so i have 2 Questions:
    My Website is now in two languages: German and french. In the template, the language switcher should link all available languages except the current language. How is it possible?
    It is possible instead of language shortcode to show the Flag?
    thank you

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: translatable partly working--questions on minor issues Link to this post

    Show a flag: Use css to do that, I use something like this:

    <% if LangMenu %>
    <ul id="languageMenu" >
       <% control LangMenu %>
       <li class="$Name"><a href="$Link" hreflang="$Code" title="$Name">$Name</a></li>
       <% end_control %>
    </ul>
    <% end_if %>

    ul#languageMenu {
       width: 180px;
       height: 43px;
       list-style: none;
    }

    ul#languageMenu li.yourlanguage {
       display: block;
       height: 43px;
       width: 60px;
       float: left;
       background: url(../images/yourlanguage.gif) no-repeat;
    }
    ul#languageMenu li.yourlanguage a {
       display: block;
       height: 43px;
       width: 60px;
       font-size: 0.1px;
       line-height: 0.1px;
       text-indent: -10000px;
       background: url(../images/yourlanguage.gif) no-repeat;
       white-space: nowrap; /* required in IE 6 */
    outline: 0; /* prevent dotted border in Firefox */
    }

    Where you have to replace yourlanguage.gif with the image you want to use and li.yourlanguage and li.yourlanguage a with the actual language you are using (and of course you have to change the dimensions of the elements to suit your need). You have to run a test to see what the language name is that the function returns. (Disclaimer: I have changed the code a bit to make it easier to understand so there could be some errors there, you could use the (more or less) standard code used for menus to create hover effects and use one image that holds all your elements etc YMMV)

    Regarding the first point: you have to compare the current language with the language of the translation of the page during the loop with an if statement. You have to compare $code with $this->Locale.

    if (isset($page)) {
    if (!($code == $this->Locale)) {

    //continue looping

    }
    }

    EDIT: for the use of flags you can of course also do: <a href="$Link"><img src="/imagelocation/$Name.gif"></a> where $Name.gif returns the name of your image and imagelocation is the location of the image

    3013 Views
Page: 1 2 3
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.