Forum Moderators: coopster

Message Too Old, No Replies

how to include different languages

         

ernest1b

6:05 pm on Apr 29, 2010 (gmt 0)

10+ Year Member



I was asking in previous post about folder structure and now cames out a question about organizing languages structure. I would be really happy if anybody can comment if the following is a common way for language versions.

settings.php
if(empty($_SESSION[lang])){
include_once '/languages/lang_en.php';
}else{
include_once '/languages/lang_'.$_SESSION[lang].'.php';
}

lang_en.php
$profileUpdated="Profile has been sucesfully changed";
$writeProfileInfo="Please submit your information";
$needFirstName="Missing first name";
$needFirstName="Missing last name";
$from[profile]="from";
$from[home]="cooming from";

lang_si.php
$profileUpdated="Profil je bil uspešno spremenjen";
$writeProfileInfo="Prosimo izpolnite vaš profil";
$needFirstName="Manjka ime";
$needFirstName="Manjka priimek";
$from[profile]="iz";
$from[home]="prihaja iz";

profile.php

<?
include_once 'settings.php'
if(isset($_POST[submit])){
if(empty($_POST[firstName])){
$error[]=$needFirstName;
}elseif(empty($_POST[lastName])){
$error[]=$needLastName;
}else{
$msg=$profileUpdated;
}
}
//...
?>
<html>
<body>
<div id="msg">
<?
if(isset($msg)){
echo $msg;
}
?>
</div>
<div id="profile">
<?
echo $writeProfileInfo;
echo '<br />';
echo $firstName;
echo '<input type="text" name="firstName" />';
echo '<br />';
?>
</div>
<div id="friends">
<?
echo $friendFullName[1]." ".$from[profile]." ".$friendLocation[1];
?>
</div>
</body>
</html>

jatar_k

6:13 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



often it would be in an array as opposed to individual variables, the array is useful becaus eit reduces the possibility of collision

in general, this is a common approach

we call internationalization i18n [google.com]

b0r1s

3:31 pm on Apr 30, 2010 (gmt 0)



id prefer using simple text files where i define a defined variable name and its value like :
msg1="HOME"
msg1txt="HOME"
... and so on. never had problems with collisions.

Matthew1980

4:14 pm on Apr 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there ernest1b,

I shall pull you up on two things here ;-p

Firstly:-

The php tags - get into the habit of using full tags, as not all servers use the short tags option in the php.ini file, and it will save you headaches later on ;-p

<?php
include_once 'settings.php'
if(isset($_POST[submit])){
if(empty($_POST[firstName])){
$error[]=$needFirstName;
}elseif(empty($_POST[lastName])){
$error[]=$needLastName;
}else{
$msg=$profileUpdated;
}
}

Secondly:-

Accessing the superglobal arrays: $_POST[firstname]; $_SESSION[your_session]; $_GET[get_url]; This too is not the best way to handle this, and if you have error_reporting() turned on it will generate an "Notice: Use of undefined constant" error.

So it is best to use (single quotes): $_POST['firstname']; $_SESSION['your_session']; $_GET['get_url']; when accessing that information.

From what I read here you are just trying to implement a "language pack" option so that you cover more than one language? Excellent, this improves accessibility for people accessing your site all over the globe. Cool!

Is this something as you want to have stored as a cookie(?) so that each time you have a visitor they can select a language, or is it something as you intend to publish and have people download as an addon for your software?

Personally, from what you ask I think as your best solution would be in cookie/session - use the cookie to restore a session after the browser is closed, but if the cookie has been deleted, just prompt the user to select their language preference again.

[EDIT]: Having read Jatar_K's suggestion of using an array, this would be good:-

$LanguageENG = array();
$LanguageENG['welcome_text'] = "Welcome to my website";
$LanguageENG['contact_text'] = "Use this form to contact me";
//etc, etc

You can use constants too:-

define('WelcomeText', "Welcome to my website");
define('ContactText', "Use this form to contact me");
//etc, etc

Just a matter of preference really though,

Hopefully I have understood your question ;-p

Cheers,
MRb

jamie

4:43 pm on Apr 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



we use arrays stored like this: $language['en']['welcome']. that way your templates always use the same variables

<?php echo $language[$selected_language]['welcome'] ?>

i am not sure about the cookie/session idea, because search engines may have trouble indexing the content from each language. when building for one domain, i prefer www.example.com/en/content, www.example.com/de/content, etc.

however for nearly all important sites we have made, we have opted for a local .tld and a standalone site for each language. example.com, .de, .es, .it, etc.

if you have control of your own server, you can serve each of these domains from the same docroot, meaning you can use the same code base for each domain. this makes admin/maintenance just as easy as if it were the same site.

the reason for separate domains with local tld's is indexing in google. if you have a .com site, google won't show your spanish pages in the "pages from spain" listings for searches from google.es. have a look at this thread: [webmasterworld.com...]

from bitter experience, plan from the start to keep all translations in one place. we have just recently had to translate our site to russian and although most of it was ok, there were still plenty of if/else's directly in the templates with hardcoded translations... nightmare ;)

good luck