Jump to content

Multilingual page problems


pedro84

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:)]
Link to comment
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.
Link to comment
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!

Link to comment
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]
Link to comment
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?
Link to comment
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.
Link to comment
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.
Link to comment
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?:>:)
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.