PHP Muilti-Language script

sarvar

New Member
Messages
82
Reaction score
0
Points
0
Hey, I recently made a php script on my site which allows visitors to change the language. I made languages folder and stored en.php, ru.php and uz.php with translations. Then I pulled the language file with this code.

PHP:
// Get user language  
$lang = $_GET['lang'];    
  
switch($lang){  
      
	 case 'en':  
            include 'languages/en.php';  
            break;  
     case 'ru':  
            include 'languages/ru.php';  
            break;  
     case 'uz':  
            include 'languages/uz.php';  
            break;  
	 default:  
	    include 'languages/en.php';			  
			    
}

When I visit the site, default language works fine. But when I assing lang in the URL, I get random letters. Any ideas to fix this issue?
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
are you using the proper encoding? try utf-8, as it seems to work the best. if that doesn't work for your current language, take a look at the iso ones.

also, a link to the site would help us out ;)
 

sarvar

New Member
Messages
82
Reaction score
0
Points
0
I don't know what you mean by utf-8.

See the problem yourself.

ENGLISH VERSION and DEFAULT LANGUAGE

Also, If I change the default langiage to english, It works fine, but when I set the language from URL it gets all nasty.

(I think cossacks is down right now.)
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
appears to be a problem with how you implement your lang file.
can you post a snippet of the lang file (pref en)?

also, i'm using a custom multi-dimensional lang file on one of my projects, and there's no problems with it, so if it needs to be rewritten, i can help ;)
 
Last edited:

sarvar

New Member
Messages
82
Reaction score
0
Points
0
Ok, here is a snippet code from language file and php file.

PHP:
<?php

// Top Menu
$lang['HOME'] = "Home";
$lang['MP3'] = "Mp3";
$lang['CONTACT'] = "Contact";

// Login Section
$lang['LOGIN'] = "Login";
$lang['LOGOUT'] = "Logout";
$lang['USERNAME'] = "Username";
$lang['PASSWORD'] = "Password";
$lang['VERIFY_PASSWORD'] = "Verify Password";
$lang['REMEMBER'] = "Remember";
$lang['DONT_HAVE_ACCOUNT'] = "Don\'t have account?";
$lang['MY_ACCOUNT'] = "My Account";
$lang['WELCOME'] = "Welcome";
$lang['EMAIL'] = "Email";
$lang['FIRST_NAME'] = "First Name";
$lang['LAST_NAME'] = "Last Name";
$lang['GENDER'] = "Gender";
$lang['MALE'] = "Male";
$lang['FEMALE'] = "Female";
$lang['BIRTHDATE'] = "Birth Date";
$lang['COUNTRY'] = "Country";
$lang['ENTER_CAPTCHA'] = "Enter the code above";
$lang['REGISTER'] = "Register";

// Months
$lang['JANUARY'] = "January";
$lang['FEBRUARY'] = "February";
$lang['MARCH'] = "March";
$lang['APRIL'] = "April";
$lang['MAY'] = "May";
$lang['JUNE'] = "June";
$lang['JULY'] = "July";
$lang['AUGUST'] = "August";
$lang['SEPTEMBER'] = "September";
$lang['OCTOBER'] = "October";
$lang['NOVEMBER'] = "November";
$lang['DECEMBER'] = "December";

// Admin Section
$lang['ADMIN_HOME'] = "Administrator";

// Error messages
$lang['RESTRICTED'] = "Restricted!";
$lang['BANNED'] = "Sorry, you appear to be banned from our site. Here's why you got banned:<br />".$get_banned_ip_rst."<br />Feel free to contact us <a href=\"contact.php\">here</a>.";
$lang['NULL_USERNAME'] = "Error: You must enter your username.";
$lang['NULL_PASSWORD'] = "Error: You must enter your password.";
$lang['BAD_LOGIN'] = "Error: Please verify your username and password.";
$lang['REG_NULL_LOGIN'] = "ERROR: Please enter username you would like to use.";
$lang['NULL_EMAIL'] = "ERROR: Please enter your e-mail.";
$lang['NULL_SEARCH'] = "You did not enter a search term.";
$lang['CHAR_SEARCH'] = "Query must be at least 3 characters long.";
$lang['INVALID_SEARCH_LETTER'] = "No singers exist for that letter.";

// Misc
$lang['SEARCH'] = "Search";
$lang['SEARCH_RESULTS'] = "Search Results for";
$lang['INVALID_SEARCH'] = "No matches found for";
$lang['RE_SEARCH'] = "Search Again!";

?>

PHP file

PHP:
<?php
// Top panel
$top_menu = "<ul>
			  <li><a href=\"../index.php\">".$lang['HOME']."</a></li>
			  <li><a href=\"../contact.php\">".$lang['CONTACT']."</a></li>
			</ul>";
?>
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
You're setting $lang to $_GET['lang'], and then when the language file is included you're using the same variable name for the string array. The string keys equate to a numeric 0, and the first letter of each string is being placed as the first letter of $lang. That is, those "random" characters are actually the first letter of $lang['RE_SEARCH'].

In short, don't bother storing $_GET['lang'] in a variable, just use it in the switch statement instead ;-)
 

sarvar

New Member
Messages
82
Reaction score
0
Points
0
Thank you woiwky! Your answer fixed the problem. +reps.
 

scriptando

New Member
Messages
30
Reaction score
0
Points
0
Also good for you if there is a prioblema you must put this sentiencia for when not in the url and do not generate error

if(isset($_GET["xxx"]))
{
...
}
 

sarvar

New Member
Messages
82
Reaction score
0
Points
0
I have done that, that was just a code that was making error. Thanks though.
 
Top