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
PHP Multilanguage from file
Moderator: General Moderators
You could try something like this...
Output:
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']; ?>!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!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.
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);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:
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
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;
}
}
?>-Nay