Jump to content


Photo

Multilingual page problems


  • Please log in to reply
22 replies to this topic

#1 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 09:06 AM

Hello again!

I'm PHP  neewbie, but I'm writing my first website, with huge help from good people. I have problem. I want to make multilingual site based on lang files

define ('LOGIN', "Login");

But I don't know how to start, I found some articles over the internet, but I don;t understand them:)

Greetings,
Pedro Juan  Alvaredo-Kowalski [Spanish-Polish Surname:)]


#2 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 24 October 2006 - 09:59 AM

index.php
<?php

define("LANGUAGE", "EN");
require_once("include/language/". LANGUAGE .".inc");

echo "<h1>". WELCOME_MESSAGE ."</h1>";

?>

EN.inc
<?php

define("WELCOME_MESSAGE", "Welcome to the site");

?>

Then based on what LANGUAGE is set to, it will load a difference language file.
SA PHP Archives - www.phparchives.za.org

#3 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 10:03 AM

Thank You.

But how to make lang switcher?

#4 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 10:05 AM

Usually, an array of strings is used, since constants cannot be redefined.

eg:

EN,
<?php
$language["welcome"] = "Welcome";

?>

DE,
<?php
$language["welcome"] = "Hallo"; // lols i dunno german :P

?>

then use something like:
<?php
define("LANGUAGE", "EN");
require_once("include/language/". LANGUAGE .".inc");
echo $language["welcome"];
?>

HTH!



#5 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 24 October 2006 - 10:08 AM

You can do the language selector like so

<?php
$lang = addslashes($_GET["lang"]);
require_once("include/language/". $lang .".inc");
echo $language["welcome"];

?>

<form action='index.php' method='get'>
<select name='lang'>
<option value='EN'>English</option>
</select>
<input type='submit' value='Change Language'>
</form>


SA PHP Archives - www.phparchives.za.org

#6 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 10:16 AM

<?php
$lang = addslashes($_GET["lang"]);
require_once("include/language/". $lang .".inc");
echo $language["welcome"];

?>

<form action='index.php' method='get'>
<select name='lang'>
<option value='EN'>English</option>
</select>
<input type='submit' value='Change Language'>
</form>

I use this for test and i get error

Warning: main(include/language/.inc): failed to open stream: No such file or directory in c:\usr\apache\httpd\html\index.php on line 3

Fatal error: main(): Failed opening required 'include/language/.inc' (include_path='.') in c:\usr\apache\httpd\html\index.php on line 3

I've got files made.

BTW Have I put this code :

<?php
$lang = addslashes($_GET["lang"]);
require_once("include/language/". $lang .".inc");
echo $language["welcome"];

?>
in all my pages?

#7 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 10:40 AM

xsist10's method is prone to URL injection.

Think about it, what if ?lang= was defined as ../../../veryimportantpasswordfile or somesuch... ?

Better off to employ swtiches...

eg,
on all your pages:
<?php
require_once("langman.php");
if(!SelectLanguage($_GET["lang"]))
	die("Error selecting language!");

echo $language["welcome"];
?>

then in langman.php:
<?php
$language = array();

function SelectLanguage($strLang)
{
	global $language;
	
	$inc = "en";
	switch(stripslashes($strLang))
	{
		case "de":
			$inc = "de";
		break;
		
		case "en":
			$inc = "en";
		break;
		
		default:
			$inc = "en";
	}
	

	include_once("languages/" . $inc . ".inc");
	if(isset($lang))
	{
		$language = $lang;
		return true;
	}

	return false;
}
?>

Then in ./languages/en.inc for example:
<?php
$lang["welcome"] = "welcome";
?>

and ./languages/de.inc :
<?php
$lang["welcome"] = "hallo";
?>

If your unsure how to apply this, just ask ;)

Then to select the language, just use mypage.php?lang=de on ALL your pages, where you are using language, if ?lang is not defined, english will be used.

hth.

#8 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 10:54 AM

Thank You.

I apply what You write and I get blank page.

What did I make wrong?

#9 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 10:59 AM

Haha, I spelt welcome wrong :s, sorry Try it now... :D


#10 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 11:04 AM

It works now. Thank You veerry much. You are verny nice human!

But tell me one thing...how to select language??:>:)

#11 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 11:10 AM

Well, the method above is simple but can become confusing in large sites... (Since ?lang= has to be appended to every link)...

Ideally, you should store the users language in a session...

Meaning on the top of every page you would place:
<?php
session_start();
require_once("langman.php");
if(!SelectLanguage($_SESSION["lang"]))
	die("Error selecting language!");

echo $language["welcome"];
?>

Then setlang.php :
<?php
session_start();
$strLang = $_GET["lang"];
$_SESSION["lang"] = stripslashes($strLang);
header("Location: index.php"); //** redirect to where u want...
?>

Then!, create links to change the language:
<a href="setlang.php?lang=de">German</a><br />
<a href="setlang.php?lang=en">English</a>

Tell me how that pans out... :)

hth.

#12 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 11:15 AM

t works ;D ;D ;D
Thank You very muuuch!!

I'm sure I will have problem later:) I'll post here ok?:)




#13 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 11:18 AM

Yeah just make a new topic... :P

No probs :P

#14 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 11:33 AM

I knew it:)
When I put it on my site, it works, but i get this error:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at c:\usr\apache\httpd\html\index2.php:2) in c:\usr\apache\httpd\html\index2.php on line 3
too.

Whats wrong?:>:)

#15 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 11:35 AM

session_start(); MUST be called before any output is sent to the browser (eg spaces or HTML), session_start() should idealy be the topmost thing in your website. The manual also mentions this :)

hth.

#16 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 11:39 AM

OMG...what a asshole:) I'm:)

Thank You
You know what? You are veeery kind to me:)

#17 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 24 October 2006 - 11:43 AM

session_set(); MUST be called before any output is sent to the browser (eg spaces or HTML), session_set() should idealy be the topmost thing in your website. The manual also mentions this :)

hth.


session_set doesnt exist as a function, but otherwise you're right. it's session_start instead.

cheers
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#18 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 24 October 2006 - 11:45 AM

Im kind to everyone... im told ;)
No probs helping... First time ive written a multi-lingual system :P

session_set doesnt exist as a function, but otherwise you're right. it's session_start instead.


Sorry, my bad, just got back into php after a year off (CGI instead):P

#19 pedro84

pedro84
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 24 October 2006 - 01:27 PM

One question:

I incude some scripts with this code:
<?php
if (isset($_GET['op'])){
switch ($_GET['op']) {
case 1:
    include("content1.php");
   break;
case 2:
   include("content2.php");
   break;
case 3:
include ("sendtofriends/sendtofriends.php");
  break;     
default:
      include("news/show_news.php"); // this stops pageid to be set by the user trying to break your script
}
} 

else {
include ("news/show_news.php");  //this one allows for just index.php to get default page
} 
?>

Can I make them mulitlingual the same way? Or make each module lang file?

Thanks in Advance

#20 True`Logic

True`Logic
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 24 October 2006 - 03:34 PM

basically youll need to include a file which has an array of the english word into the other language, then split your dialogues into an array on chr(32), after that you only need to use str_replace($words[$num], $lang[$words[$num]]) in a while statement =)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users