PHP Multilanguage from file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

PHP Multilanguage from file

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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!
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

Ok it is a good way to do this,

thank you!
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

ok but what about if I want to translate every string of multiple pages?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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);
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
Post Reply