Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Link on a picture in a multilingual environment

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

Page: 1
Go to End
Author Topic: 971 Views
  • TomMiller
    Avatar
    Community Member
    26 Posts

    Link on a picture in a multilingual environment Link to this post

    Dear Users,
    here is what i do: in a template i use a href on a picture.

    <a href="internal-page-name/"><img src="path-to-the-image" width="xx" height="xx" alt="alternate-title" title="title" /></a>

    When i click on the picture i am always forwarded to the internal-page-name with the configured standard language.
    I couldn't find a way to fix this problem.

    tia Tom

  • ampedup
    Avatar
    Community Member
    133 Posts

    Re: Link on a picture in a multilingual environment Link to this post

    Can you give an example of where the page should go for the other languages?

  • TomMiller
    Avatar
    Community Member
    26 Posts

    Re: Link on a picture in a multilingual environment Link to this post

    I'm switching languages by adding the local to the URL e.g. http://www.silverstripe.org/?locale=fr_FR

    If i do not add a locale in the href, the href will forward to the default locale. I want the link to 'recognize' the selected language and forward accordingly.
    This should be possible if i used a cookies- or sessions-variable for the language. Unfortunately i don't know how to implement it in a at least half-descent way.

  • ampedup
    Avatar
    Community Member
    133 Posts

    Re: Link on a picture in a multilingual environment Link to this post

    Yes, definately go for session variables. That will solve all of your problems. All of them!

    http://www.w3schools.com/PHP/php_sessions.asp

    This is how I would go about using session variables for what you are doing:

    Where you are checking for $_GET['locale'] (im guessing page controller init function), if it is set then set the session variable:

    if (isset($_GET['locale'])
    {
    $_SESSION['locale'] = $_GET['locale'];
    }

    Then, where you are currently using the $_GET['locale'] value, just use the $_SESSION['locale'] instead.

    This should mean you only have to put ?locale=xx_XX in where the language is being changed, and no where else, but still all the pages should load in language xx_XX.

  • TomMiller
    Avatar
    Community Member
    26 Posts

    Re: Link on a picture in a multilingual environment Link to this post

    Thanks for your reply! Unfortunately i could not get your code working.

    Now i added
          /* handling of locales */
          function getCurrentLanguage() {
             $curLang = Session::get('currentLang');
             $localeString = "";
             
             if ($curLang == "de") {
                $localeString = "de_DE";
                return $localeString;
             }

             elseif ($curLang == "en") {
                $localeString = "en_US";
                return $localeString;
             }
             elseif ($curLang == "fr") {
                $localeString = "fr_FR";
                return $localeString;
             }

    to the init function of the controller. Is this valid php? Could you give me a hint on how to call a function from within a template?

  • ampedup
    Avatar
    Community Member
    133 Posts

    Re: Link on a picture in a multilingual environment Link to this post

    Yes this is valid code.

    To call a function in the template:

    $CurrentLanguage

    You can test this by putting die('Hello World'); at the start of the function and calling the function from your template.

    You really should set $localeString to a default language and always return something (I guess this does depend on how you parse what this function returns).

    Your code here relies on you setting the session variable currentLang somewhere else.

  • TomMiller
    Avatar
    Community Member
    26 Posts

    Re: Link on a picture in a multilingual environment Link to this post

    Thanks again!
    Here is what i did:
    In the page class i added a function:

       function getCurrentLanguage() {
          return Translatable::get_current_locale();
       }   

    In the template i just call the function: <a href="URL/?locale=$getCurrentLanguage">

    971 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.