Selecting alternative language!?

Hi,

I’m trying to set up an multiple language site and are doing OK untill now… My language is based on tags like this:

<?=''._LOGIN;?>

I’m then calling my db table for the right language depending on a cookie:

if ($language == 'se') {
			
    ///// SWEDISH /////

    $result = mysql_query('SELECT * FROM '.$prefix.'_lang_code WHERE lang_id=2');
    while ($row = mysql_fetch_assoc($result)) {  

        $string = $row['string'];             
    
        define("_".$row['tagname']."","".$string."");

    }
    
} else {

	///// ENGLISH /////

    $result = mysql_query('SELECT * FROM '.$prefix.'_lang_code WHERE lang_id=1');
    while ($row = mysql_fetch_assoc($result)) {  

        $string = $row['string'];             
    
        define("_".$row['tagname']."","".$string."");

    }
    
}

So as you can see my default language is english, but if a user has choosen Swedish (se) it should be in swedish. And this also works like a charm… Now here is my problem.

Lets say a page have 2 tags (“_COME_TOGETHER” and “_JOIN”) and the tag “_COME_TOGETHER” has not been translated to swedish yet, then if a user chooses swedish he will then just see _COME_TOGETHER… Is there any way I can make an rule so if the tag will then automaticly go to english.

Thanks in advance…

Why not select English & the users chosen language from the database at the same time? Then it’s a simple piece of logic to create the constant.

Something along the lines of…


<?php
$res = mysql_query('SELECT string, tag FROM lang WHERE id IN (1, 2) AND tag NOT NULL ORDER BY id DESC;')
while($row = mysql_fetch_assoc($res)){
  $tag = '_' . $row['tag'];
  if(false === defined($tag)){
    define($tag, $row['string']);
  }
}

This will define all the Swedish constants first, then the English ones of any are missing.

It’s a messy example, but should convey my point. Hopefully. :slight_smile:

Ideally, you would just craft an SQL query to perform this logic though.

Hmmmm, not sure what you mean?

I have tryied it with this:

$sql = mysql_query('SELECT string, tagname FROM fb_lang_code WHERE lang_id IN (1, 2) AND tagname NOT NULL');
			$result = mysql_query($sql) or die("Error: (" . mysql_errno() . ") " . mysql_error());
			while($row = mysql_fetch_array($result)){
			  $tag = '_' . $row['tagname'];
			  if(false === defined($tag)){
				define($tag, $row['string']);
			  }
			}

But I get this error:
Error: (1065) Query was empty

What do I do wrong?

Try this query:

SELECT string, tagname FROM lang WHERE id IN (1, 2) ORDER BY id DESC;

Tryied this:

SELECT string, tagname FROM fb_lang_code WHERE lang_id IN (1, 2) ORDER BY lang_id

But now get this error:
Error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Resource id #7’ at line 1

What is wrong and what is ‘Resource id #7’?


$res = mysql_query('SELECT string, tagname FROM fb_lang_code WHERE lang_id IN (1, 2) ORDER BY lang_id DESC;');

if(false === is_resource($res)){
  printf('Error: (%d) %s', mysql_errno(), mysql_error());
  exit;
}

while($row = mysql_fetch_assoc($res)){
  $tag = '_' . $row['tagname'];
  if(false === defined($tag)){
    define($tag, $row['string']);
  }
}

This is assuming that any non-translated phrases are not in the database at all.

Ahhh, this did the trick:

$res = mysql_query('SELECT string, tagname FROM fb_lang_code WHERE lang_id IN (1, 2) ORDER BY lang_id DESC;');

			if(false === is_resource($res)){
			  printf('Error: (%d) %s', mysql_errno(), mysql_error());
			  exit;
			}
			
			while($row = mysql_fetch_assoc($res)){
				$string = $row['string'];             
				define("_".$row['tagname']."","".$string."");
			}

Thanks alot for your help ;.-)