Page 1 of 1

PHP Multilanguage from file

Posted: Mon Oct 13, 2003 8:00 pm
by richie256
Hi

What is the best way to make a website of multilanguage?

I this the easiest way is to create a "Package" of different language and get text from this?

If you have any suggestion or link about a tutorial, please give it to me!

Thanks

Posted: Mon Oct 13, 2003 8:15 pm
by JAM
You could try something like this...

Code: Select all

// swedish.php
<?php
    $lang['smallcat']    = "kattunge";
    $lang['bigcat']      = "katt";
    $lang['coffee']      = "kaffe";
?>

// english.php
<?php
    $lang['smallcat']    = "kitten";
    $lang['bigcat']      = "cat";
    $lang['coffee']      = "java";
?>

// index.php
<?php
// two examples using the files above.
    $language = "english"; // set the language to fit your needs...
    require ($language.'.php'); // require or include the language file based on the above...
?>
I have a <?php echo $lang['bigcat']; ?> that has a <?php echo $lang['smallcat']; ?> that drinks <?php echo $lang['coffee']; ?>!
<hr>
<?php
    $language = "swedish";
    require ($language.'.php');
?>
Jag har en <?php echo $lang['bigcat']; ?> som har en <?php echo $lang['smallcat']; ?> som dricker <?php echo $lang['coffee']; ?>!
Output:

Code: Select all

I have a cat that has a kitten that drinks java! 
---
Jag har en katt som har en kattunge som dricker kaffe!

Posted: Mon Oct 13, 2003 8:19 pm
by richie256
Ok it is a good way to do this,

thank you!

Posted: Tue Oct 14, 2003 12:02 am
by richie256
ok but what about if I want to translate every string of multiple pages?

Posted: Tue Oct 14, 2003 3:55 am
by JAM
Then I'd make different pages for each language.

You can use something similiar as the above example, but instead you include files that are holding the actual content of the site.

Code: Select all

switch ($_GET['lang']) {
        case "en": $inc = 'en_welcome.php';
        break;
        case "se": $inc = 'se_welcome.php';
        break;
        case "dk": $inc = 'dk_welcome.php';
        break;
        case "fi": $inc = 'fi_welcome.php';
        break;
        default: $inc = 'en_welcome.php'; // if no language is selected, choose a default langiage
        break;
    }
    include ($inc);

Posted: Tue Oct 14, 2003 3:59 am
by Nay
You might want to use str_replace for that then. Well, if the page is being drawn from a MySQL database it's the best. Since you can translate without affecting the data for outputting. Do something along this way:

Code: Select all

<?php

function translate($source, $foreign, $word) {
if(empty($source) || empty($foreign) || empty($word) {
echo "Error! Arguments are not valid";
} else {
$langfile = $foreign . ".php";
require($langfile);
$r_word = $lang['$word'];
echo $r_word;
}
}

?>
Hope that helps. I'm not sure if it's 100% right since I wrote it on spot and didn't get the chance to test it, so other developers - please do advise.

-Nay