Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Krzysztof Kucharzyk
seal-mask
.a{fill-rule:evenodd;}techdegree
Krzysztof Kucharzyk
Front End Web Development Techdegree Student 4,005 Points

Different link url for different website language version

Hey,

I have a website with multi language option and I have my links there to download a files. I'm learning php atm the moment so I wanna try different things.

I would like to know how to write a php code so when I'm on my website (default language is english).

I have a link to download a file - en.pdf with user's manual but when I switch language to japanese I would like this url link to be changed to jp.pdf.

my html is like this:

<ul class="pliki">
<li><a href="/files/en.pdf" title="User's manual">User's manual</a><small> - PDF file</small></li>
<li><a href="/files/jp.pdf" title="User's manual">User's manual</a><small> - PDF file</small></li>
</ul>

I tried to write something like this but it doesn't work:

<?php 

session_start();

    $languages = array('pl','en','es','el','ro','sl');

        if(in_array($_GET['lang'], $languages)) {
            $_SESSION['lang'] = $_GET['lang'];
        }   

define('LANG', in_array($_SESSION['lang'], $languages) ? $_SESSION['lang'] : 'PL');


echo '<ul>';
foreach ($languages as $language) {
    echo '<li>';
    echo '<a href="/files/?lang='.$language.'.pdf">Instrukcja uzytkownika</a>';
    echo '</li>'
}
echo '</ul>';

?>

I would really appreciate for your help.

1 Answer

Hanley Chan
Hanley Chan
27,771 Points

Hi,

From your code it looks like you already have the selected language stored in your LANG constant. Could you not output the correct link with this:

echo '<a href="/files/?lang='.LANG.'.pdf">Instrukcja uzytkownika</a>';
Krzysztof Kucharzyk
seal-mask
.a{fill-rule:evenodd;}techdegree
Krzysztof Kucharzyk
Front End Web Development Techdegree Student 4,005 Points

Yeah, I noticed that but I figured it out a lil bit easier and different way. I just refered to __lang using $_GET and assigned variable to it and assigned variable to

<a href="/files/$MyVar.pdf" >File</a>

and it worked. I didn't think that I could use __lang for this. But thanks for answer :)