wyc
wyc

Reputation: 55293

How do I make a php if/switch statement according to the PHP lang of the site?

My site has a way of changing its language, and each time I change it I see something like this at the top of the source code:

<html dir="ltr" lang="en-US">
<html dir="ltr" lang="zh-TW">

I think using the URL may also work:

http://alexchen.info/en
http://alexchen.info/tw

or maybe this:

?lang=es
?lang=en

because this works too:

http://alexchen.info/?lang=es
http://alexchen.info/?lang=en

I would like to know what's the best way of using that to make a php if statement (or switch statement). For instance:

if(lang=en) {
 // do this
}


if(lang=tw) {
 // do this
}

Upvotes: 0

Views: 4079

Answers (2)

moteutsch
moteutsch

Reputation: 3841

If you have ?lang=en you can simply get via the $_GET global variable. However, you should first encapsulate the logic within a function.

function getLang()
{
    return $_GET['lang'];
}

// ...

if (getLang() == 'en') {
    // ...
}

Upvotes: 2

mpen
mpen

Reputation: 283173

To write a switch (which could have easily been looked up in their documentation):

switch($lang) {
    case 'en-US':
        // do this
        break;
    case 'zh-TW':
        // do this
        break;
}

Although this probably isn't the best approach to doing site translation. I haven't done much multi-language stuff myself, but I see a lot of frameworks wrap blocks of text in functions like

echo T("sample text");

And then the T function would replace that text with the translated text. That way you don't have you entire site littered with switch statements. The translations can be stored in a database. If there's a missing translation, that can be logged or inserted as a blank entry into your DB so that you know what you need to fill in/translate later without digging through your site trying to find all the places where text needs to be translated.

Upvotes: 1

Related Questions