Jump to content

Archived

This topic is now archived and is closed to further replies.

pedro84

Multilingual page problems

Recommended Posts

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

[code]define ('LOGIN', "Login");[/code]

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:)]

Share this post


Link to post
Share on other sites
index.php
[code]
<?php

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

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

?>
[/code]

EN.inc
[code]
<?php

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

?>
[/code]

Then based on what LANGUAGE is set to, it will load a difference language file.

Share this post


Link to post
Share on other sites
Usually, an array of strings is used, since constants cannot be redefined.

eg:

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

?>[/code]

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

?>[/code]

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

HTH!

Share this post


Link to post
Share on other sites
You can do the language selector like so

[code]
<?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>

[/code]

Share this post


Link to post
Share on other sites
[code]<?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>
[/code]

I use this for test and i get error

[code]
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[/code]

I've got files made.

BTW Have I put this code :

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

?>[/code]
in all my pages?

Share this post


Link to post
Share on other sites
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:
[code]<?php
require_once("langman.php");
if(!SelectLanguage($_GET["lang"]))
die("Error selecting language!");

echo $language["welcome"];
?>[/code]

then in langman.php:
[code]
<?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;
}
?>
[/code]

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

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

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.

Share this post


Link to post
Share on other sites
It works now. Thank You veerry much. You are verny nice human!

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

Share this post


Link to post
Share on other sites
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:
[code]<?php
session_start();
require_once("langman.php");
if(!SelectLanguage($_SESSION["lang"]))
die("Error selecting language!");

echo $language["welcome"];
?>[/code]

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

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

Tell me how that pans out... :)

hth.

Share this post


Link to post
Share on other sites
t works ;D ;D ;D
Thank You very muuuch!!

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


Share this post


Link to post
Share on other sites
I knew it:)
When I put it on my site, it works, but i get this error:
[code]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[/code] too.

Whats wrong?:>:)

Share this post


Link to post
Share on other sites
[url=http://php.net/session_start]session_start()[/url]; MUST be called before any output is sent to the browser (eg spaces or HTML), [url=http://php.net/session_start]session_start()[/url] should idealy be the topmost thing in your website. The manual also mentions this :)

hth.

Share this post


Link to post
Share on other sites
[quote author=heckenschutze link=topic=112507.msg456745#msg456745 date=1161689759]
[url=http://php.net/session_set]session_set()[/url]; MUST be called before any output is sent to the browser (eg spaces or HTML), [url=http://php.net/session_set]session_set()[/url] should idealy be the topmost thing in your website. The manual also mentions this :)

hth.
[/quote]

session_set doesnt exist as a function, but otherwise you're right. it's [url=http://www.php.net/session_start]session_start[/url] instead.

cheers

Share this post


Link to post
Share on other sites
Im kind to everyone... im told ;)
No probs helping... First time ive written a multi-lingual system :P

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

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

Share this post


Link to post
Share on other sites
One question:

I incude some scripts with this code:
[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
}
?>[/code]

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

Thanks in Advance

Share this post


Link to post
Share on other sites
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 =)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.