-2

So, I want my website to be multilingual. However, I used to do it with a txt.php file which had all the sentences defined. I want to transfer this to my database so that I can build a rather simple CMS kind of thingy. I entered all the translations into the database and then wanted to select them and define them, like below. This is not working however. What am I doing wrong?

$sql = "SELECT * FROM Main";
$result = mysql_query($sql);

function defineStrings() { 
    switch($_SESSION[lang]) { 
        case "en": 
            while($row = mysql_fetch_assoc($result)) {
                define("$row['identifier']","$row['texten']");
            }
        break;
    } 
} 

This is called in the index.php file, like this:

if ($_SESSION[lang] == "") { 
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    $_SESSION[lang] = $lang; 
    $currLang = $lang; 
} else { 
    $currLang = $_SESSION[lang]; 
    $_SESSION[lang] = $currLang; 
} 
include("Localization/lang.php"); 
include("Localization/txt.php");    
defineStrings();
user2755352
  • 27
  • 1
  • 7
  • Is this the whole code? Where did you call the function defineStrings()? – Osh Mansor Apr 27 '14 at 16:38
  • I edited it. I call it in the index.php file – user2755352 Apr 27 '14 at 16:39
  • 2
    Er - what does 'it doesn't work' mean? Do you get an error? Nothing? The wrong thing? Are you setting a session anywhere? What will happen on my first visit, where there is no session set? – andrewsi Apr 27 '14 at 16:43
  • Well, in the index.php file there is also the code I added to my question. I get the following error: `Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given` – user2755352 Apr 27 '14 at 16:46
  • Why are you not using something like gettext, which is precisely for this purpose, has the tools for the right translation workflow and has a giant ecosystem?! Believe me, you'll be better off with it in the long run compared to your own home brewed system. – deceze Apr 27 '14 at 16:51

2 Answers2

1

Try this:

Put the two lines inside your defineStrings function. EDITED (see inside while loop):

function defineStrings() { 
    $sql = "SELECT * FROM Main";
    $result = mysql_query($sql);
    switch($_SESSION[lang]) { 
        case "en": 
            while($row = mysql_fetch_assoc($result)) {
                define($row['identifier'],"'".$row['texten']."'");
            }
        break;
    } 
} 
Osh Mansor
  • 1,232
  • 2
  • 20
  • 42
0
$_SESSION[lang]

Change to:

$_SESSION['lang']
Lucas Henrique
  • 1,380
  • 1
  • 11
  • 15
  • Not sure why you were instantly downvoted, as you surely must encapsulate indices when they're strings. – Ohgodwhy Apr 27 '14 at 16:44
  • 1
    While it's true that this is a mistake that needs to be fixed, it should not make a difference unless you have a constant `lang` defined. – deceze Apr 27 '14 at 16:52