Jump to content

Multilanguage site problem


4554551n

Recommended Posts

Hi all,

How do I create a PHP code such that if a user clicks on a button they get transferred to the equivalent page in another language, I have index.php 

<?php include_once 'file.php'; ?>
<html><div class="menu">
<a href="index.php?lang=en" >
<img title="Eng" src="images/en.png" alt="" />
</a>                                                
<a href="index.php?lang=de">
<img title="Ger" src="images/de.png" alt="" />
</a>
<a href="index.php?lang=hu" >
<img title="Hun" src="images/hu.png" alt="" />
</a>
</div> </html>

and I have another file.php

<?php
ob_start();
session_start();
header('Cache-control: private'); 

if(isSet($HTTP_GET_VARS['lang']))
{
$lang = $HTTP_GET_VARS['lang'];

//* register the session and set the cookie *//
$HTTP_SESSION_VARS['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($HTTP_SESSION_VARS['lang']))
{
$lang = $HTTP_SESSION_VARS['lang'];
}
else if(isSet($HTTP_COOKIE_VARS['lang']))
{
$lang = $HTTP_COOKIE_VARS['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  case 'de':
  $lang_file = 'lang.de.php';
  break;

  case 'hu':
  $lang_file = 'lang.hu.php';
  break;

  default:
  $lang_file = 'lang.en.php';

  include_once './languages/'.$lang_file;
  
}


?>

and it doesn't work, basically all I want is php script, so when user browse through page can change it in another language...

 

Here is another way, but I found also problem here....

I have index.php

 <?php 

if($HTTP_GET_VARS['lang'] == 'de' || $HTTP_GET_VARS['lang'] == "") { include 'lang_de.php';} 
if($HTTP_GET_VARS['lang'] == 'de') { include 'lang_de.php';}
if($HTTP_GET_VARS['lang'] == 'en') { include 'lang_en.php';}
if($HTTP_GET_VARS['lang'] == 'hu') { include 'lang_hu.php';}
    global $lang1, $lang2, $lang3, $lang4, $txt4;
?>
<html>Some content here</html>

 

Problem is when user change language somewhere deep in the page, script automatically returns him to the home of page, so it's useless, cause u can't browse page on another language except default language.

All I want is one nice script for browsing through page on whatever language user want.

 

What's wrong with this? I mean in first case script doesn't work and I don't why.... and in the second case script works, but not properly...

Thanks.

 

 

 

Link to comment
Share on other sites

Create a languages folder

/lang/en.php

/lang/fr.php

 

In each of the files:

$lang[1] ="what ever text you need";
$lang[2] ="what ever text you need";
$lang[3] ="what ever text you need";

 

Then use a switch in the page headers to include the file that you want

yourfile.php?lang=en

 

$lang=['lang'];  

switch($lang){
     case'en':
              include 'lang/en.php';
              break;
     case'fr':
              include 'lang/fr.php';
              break;
}

 

Then just go ahead and use the text in where you need it with:

echo $lang[3];

Link to comment
Share on other sites

use sessions.

this way, no matter which page they goto, u will have which language.

I've seen a number of ways of doing this.

1) create a subdirectories for each language - prolly the easiest, but also means a lot of maintainance.

2) create a lang folder / with lang php files - intermediate, but maintainance is a lot easier, as you just need to edit the language files.

 

there are other ways. but those are the most common.

 

Link to comment
Share on other sites

use sessions.

this way, no matter which page they goto, u will have which language.

I've seen a number of ways of doing this.

1) create a subdirectories for each language - prolly the easiest, but also means a lot of maintainance.

2) create a lang folder / with lang php files - intermediate, but maintainance is a lot easier, as you just need to edit the language files.

 

there are other ways. but those are the most common.

 

I'm trying to use, but so far nothing :( it would be better if u can show me some example. Thanks.

Link to comment
Share on other sites

 

 

 

 

Then use a switch in the page headers to include the file that you want

yourfile.php?lang=en

 

$lang=['lang'];       

Here I have unexpected error cause of [ ] what should I change?

 

 

I believe he means

$lang['en'] ="what ever text you need";
$lang['fr'] ="what ever text you need";
$lang['de'] ="what ever text you need";[

$lang = $lang['de']; //Assign to language 'de'

switch($lang){
     case'en':
              include 'lang/en.php';
              break;
     case'fr':
              include 'lang/fr.php';
              break;
}

 

Also, $HTTP_GET_VARS is a registered global, which has been deprecated for nine years.

Link to comment
Share on other sites

go one step further oni-kun

lang.en.php

$langstrings =array(
  'hello'='>'Hello',
  'bye'=>'Good Bye',
);

lang.sp.php

$langstrings = array(
  'hello'=>'Hola',
  'bye'=>'Adios'
);

lang.fr.php

$langstrings = array(
  'hello'=>'bonjour',
  'bye'=>'au revoir'
);

 

so main code can go something like this

$lang='xx'; // give some goofy language

$lang = file_exists("lang.{$lang}.php"?"lang.{$lang}.php":"lang.en.php"; // select out language file, if not exist use default en.
include($lang); // load our language file

echo "{$langstring['hello']}";

 

a simple method, with an intermediate level of maintainance.

 

Link to comment
Share on other sites

I don't know what is the problem, I put this into index.php

 <?php

$lang= $lang[en];  

switch($lang){
     case'en':
              include 'lang/en.php';
              break;
     case'de':
              include 'lang/de.php';
              break; 

<html> some content </html>
}
?>

and I have

<?php echo $lang[1];?>

on proper place in index.php

 

I have this in en.php

<?php


$lang[1] ="HOME";
$lang[2] ="ABOUT US";
$lang[3] ="CONTACT";
$lang[4] ="FAQ";
  
$lang[5] = "Here goes txt,Here goes txt,Here goes txt,Here goes txt,
         Here goes txt,Here goes txt,Here goes txt,Here goes txt,
         Here goes txt, Here goes txt,Here goes txt,Here goes txt,
         Here goes txt, Here goes txt,Here goes txt,Here goes txt";
?>

 

and there is no output, and also I don't have text anywhere...

 

I forgot to say that I'm using Nusphere Phped 5.9, if that means anything....

 

 

Link to comment
Share on other sites

<?php 

if($HTTP_GET_VARS['lang'] == 'de' || $HTTP_GET_VARS['lang'] == "") { include 'lang_de.php';} 
if($HTTP_GET_VARS['lang'] == 'de') { include 'lang_de.php';}
if($HTTP_GET_VARS['lang'] == 'en') { include 'lang_en.php';}
if($HTTP_GET_VARS['lang'] == 'hu') { include 'lang_hu.php';}
    global $lang1, $lang2, $lang3, $lang4, $txt4;
?>

This works well, just as there is a problem when users surf the page, for example, and be part of the FAQ page and wants to change the language,this script automatically return to the home, and since the default language is de, it is not possible to surf in another language... How to change this?

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.