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.

Template Questions /

Link on a picture in a multilingual environment


Reply


7 Posts   1037 Views

Avatar
TomMiller

Community Member, 26 Posts

22 January 2011 at 5:11am

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

Avatar
3dgoo

Community Member, 133 Posts

24 January 2011 at 2:39pm

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

Avatar
TomMiller

Community Member, 26 Posts

24 January 2011 at 9:00pm

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.

Avatar
3dgoo

Community Member, 133 Posts

25 January 2011 at 11:01am

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.

Avatar
TomMiller

Community Member, 26 Posts

26 January 2011 at 5:19am

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?

Avatar
3dgoo

Community Member, 133 Posts

27 January 2011 at 10:26am

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.

Avatar
TomMiller

Community Member, 26 Posts

28 January 2011 at 2:01am

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