4

I would like to change this:

// use appropiate lang.xx.php file according to the value of the $lang
switch ($_SESSION['lang']) {
case 'en':
 $lang_file = 'lang.en.php';
 break;

case 'es':
 $lang_file = 'lang.es.php';
 break;

case 'zh-tw':
 $lang_file = 'lang.zh-tw.php';
 break;

case 'zh-cn':
 $lang_file = 'lang.zh-cn.php';
 break;

default:
 $lang_file = 'lang.en.php';
}

into something like this:

//include file for final output
 include_once 'languages/lang.'.$_SESSION['lang'].'php;

(I think the $lang_file variable becomes redundant if I do the include-final-output-thing above)

So that I can skip the whole switch part. I tried other combinations but they don't seem to work. Any suggestions?

4 Answers 4

18

You can do this:

switch ($_SESSION['lang']) {
case 'en':
case 'es':
case 'zh-tw':
case 'zh-cn':
    $lang_file = 'lang.'.$_SESSION['lang'].'.php';
    break;

default:
    $lang_file = 'lang.en.php';
}

Or you use an array and use in_array to see if the value is in the array:

$languages = array('en', 'es', 'zh-tw', 'zh-cn');
if (in_array($_SESSION['lang'], $languages)) {
    $lang_file = 'lang.'.$_SESSION['lang'].'.php';
} else {
    $lang_file = 'lang.en.php';
}

You even could omit en in both cases since it’s the default.

2
  • @Gumbo Excellent answer. But I have a question: is there any way of including the file directly without using the $lang_file variable something like this not sure: include_once 'languages/lang.'.$_SESSION['lang'].'php;
    – wyc
    Commented Mar 16, 2010 at 16:18
  • @janoChen: It is possible to do it that way. But you should take the case that $_SESSION['lang'] is not set or doesn’t contain a valid value. Then you would get just languages/lang..php. But if you ensure that $_SESSION['lang'] does always exist and has a valid value, you sure can do it that way.
    – Gumbo
    Commented Mar 16, 2010 at 16:22
2
$lang_file = 'lang.' . ($_SESSION['lang']) . 'php';
if(!file_exists($lang_file))
{
    $lang_file = 'lang.en.php';
}

although it isn't secure against injections. It does, however, allow you to add new language codes without modifying the code.

1
  • I think it would be hard to inject something into the $_SESSION variable. The session variable is never accessed by the client, it is merely connected to the client via a cookie. However, the $_COOKIE variable is writeable by the client via HTTP headers.
    – joar
    Commented Mar 16, 2010 at 16:28
2

Or:


$allowed   = array('en', 'es', 'zh-tw', 'zh-cn');
$lang_file = (in_array($_SESSION['lang'], $allowed))
           ? 'lang.'.$_SESSION['lang'].'.php' : 'lang.en.php';
0

This will work perfectly fine

$lang_file_tmp = 'lang.' . $_SESSION['lang'] . '.php';
if ( preg_match( '/^[a-zA-Z\-]{2,5}$/', $_SERSSION['lang'] ) && file_exists( $lang_file_tmp ) ) { 
    $lang_file = $lang_file_tmp;
} else {
    $lang_file = 'lang.en.php';
}

With this, you won't have to edit the code every time your're adding a new language and you will not have to worry about security.

2
  • I can see what you're trying to do, but I don't think your code is right. Your if statement should probably be if( preg_match(...) && file_exists(...) ) {. Also I don't see the point of $lang_file_tmp Commented Mar 16, 2010 at 18:25
  • You're right, I've changed my post to reflect your changes. $lang_file_tmp is there to remove the need to type the concatenation sequence more than once.
    – joar
    Commented Mar 17, 2010 at 10:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.