Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Simple Language File

Newbie Spellweaver
Joined
Dec 2, 2009
Messages
24
Reaction score
0
Hello there, i will show you how to so a simple language file in php.

First lets make our lang file

PHP:
<?php 
// Simple lang file
// So easy a caveman can do it!
$lang['page_text'] = "lulz hai thar";
?>

So this is our language file. You can name it anything but i recommend what language it is (e.g english french ect)

So our language file is ready now this is how we will call our text

PHP:
<?php echo $lang['your_text']; ?>

Our final product should look like this.

PHP:
<?php
require_once('lang_file_name.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<html>
<head>
<title>Language File Tutorial By Joint</title>
</head>
<body>
<p><?php echo $lang['page_text']; ?></p>
</html>
</body>

Thats all there is to it :eek:tt1:
 
Last edited:
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
As you move onto using a Database, it actually forces to get more complicated than that.. And is strongly database query oriented, in many cases.. Not saying there's no room for other methods..

Also, can you explain how that code would work with multiple languages? At what point do you put 'slang' or 'English' or whatever it is you wrote. ("lulz hai thar"?) If I wasn't a pirate who discovered computer chat, I wouldn't understand that there text.

Make one for English, and one for slang, because the tutorial doesn't show how to setup your site to use multiple languages.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
./languages/en.php
PHP:
$content['welcome_text'] = 'Hey everyone yada yada yada';

./languages/nl.php
PHP:
$content['welcome_text'] = 'Hallo allemaal yada yada yada';

./index.php
PHP:
<?php
session_name('language_tutorial'); //can be whatever you wish..
session_start();

//Define Supported Languages
$_SESSION['supported_languages'] = array('en','nl');

//Define default language
$_SESSION['default_lang'] = $_SESSION['supported_languages'][0];

//If the user requests a different language
if(isset($_GET['lang']))
{
    //If the user's requested language is supported
    if(in_array($_SESSION['supported_languages'],$_GET['lang']))
    {
        //Assign the user's requested language.
        $_SESSION['lang'] = $_GET['lang']; 
        
    } else { //If the user's language is not supported
        
        //Assign either the current or default language (if not yet defined)
        $_SESSION['lang'] = (!in_array($_SESSION['supported_languages'],$_SESSION['lang'])?$_SESSION['lang']:$_SESSION['default_lang']);
    }
} /* if the user didn't request a language, and the current language is not supported 
  [For example, not yet defined due to a brand new session] */
elseif(!in_array($_SESSION['supported_languages'],$_SESSION['lang']))
{
    //Define the default language.
    $_SESSION['lang'] = $_SESSION['default_lang'];
}

//Require the correct language file.
require_once('languages/'.$_SESSION['lang'].'.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Language File Tutorial</title>
</head>
<body>
<h1>
<?php 

//print text defined from any given language file.
echo $content['welcome_text']; 

?>
</h1>
</body>
</html>

This would be a more practical example, I think.
 
Last edited:
ex visor
Loyal Member
Joined
May 17, 2007
Messages
2,741
Reaction score
937
I think using the "switch" method would be even better! :)
Aaron
 
Newbie Spellweaver
Joined
Dec 2, 2009
Messages
24
Reaction score
0
./languages/en.php
PHP:
$content['welcome_text'] = 'Hey everyone yada yada yada';

./languages/nl.php
PHP:
$content['welcome_text'] = 'Hallo allemaal yada yada yada';

./index.php
PHP:
<?php
session_name('language_tutorial'); //can be whatever you wish..
session_start();

//Define Supported Languages
$_SESSION['supported_languages'] = array('en','nl');

//Define default language
$_SESSION['default_lang'] = $_SESSION['supported_languages'][0];

//If the user requests a different language
if(isset($_GET['lang']))
{
    //If the user's requested language is supported
    if(in_array($_SESSION['supported_languages'],$_GET['lang']))
    {
        //Assign the user's requested language.
        $_SESSION['lang'] = $_GET['lang']; 
        
    } else { //If the user's language is not supported
        
        //Assign either the current or default language (if not yet defined)
        $_SESSION['lang'] = (!in_array($_SESSION['supported_languages'],$_SESSION['lang'])?$_SESSION['lang']:$_SESSION['default_lang']);
    }
} /* if the user didn't request a language, and the current language is not supported 
  [For example, not yet defined due to a brand new session] */
elseif(!in_array($_SESSION['supported_languages'],$_SESSION['lang']))
{
    //Define the default language.
    $_SESSION['lang'] = $_SESSION['default_lang'];
}

//Require the correct language file.
require_once('languages/'.$_SESSION['lang'].'.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Language File Tutorial</title>
</head>
<body>
<h1>
<?php 

//print text defined from any given language file.
echo $content['welcome_text']; 

?>
</h1>
</body>
</html>

This would be a more practical example, I think.
i believe it would, tho when i do lang files i take the easy way out, cause i only usually write them in english. but hey you learn something new everyday =]
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
I think using the "switch" method would be even better! :)
Aaron

Actually yeah, it would be simpler +more efficient to use switch for a system that uses 2-3 languages (like the one I made, lol)

When you get to having around 4+ languages, the script I propose becomes more practical, and also requires a little less work to implement more languages. It really doesn't matter anyway though, until it uses a database to retrieve the correct languages, it's not all that great at all. Who wants to store a bunch of content in a PHP file? .. Though the language files can just as easily pull the variables from a database.. Eh, it's not half bad, come to think of it.. A little modification and it can probably work for someone who can actually use it.
 
Back
Top