multiple language

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

multiple language

Post by hrubos »

I have a code for change language in my web and a folder contains 2 file.txt : en.txt a ko.txt
But I don't understand how to do use it.

Code: Select all

function setLanguage ($lang='ko', $path = '') {
	if(empty($lang)) {
		$lang = 'ko';
	}

	// read text from folder
	$file = $lang."_text.txt";
	$text = file_get_contents( $path."text/".$file);


	$erase = array('\r', '\t');
	$text = str_replace($erase, "", $text);


	$text = explode("\n", $text);


	for( $i = 0; $i < sizeof($text); $i++) {
		if (strpos($text[$i], "##") === FALSE) { 
			// komentar
			//echo $text[$i];
			continue;

		}
		else {

			list ($constant, $value) = explode("##", $text[$i]);
			$value = trim($value);
			define($constant, $value);
		}
	}
}
?>
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

by giving the user an option to selct the language you can do this with sessions:

Code: Select all

$lang = 'ko';//set variable to default

if(isset($_GET['lang'])){
  $lang = $_GET['lang'];
  $_SESSION['selected_lang'] = $lang;
}

//check session for value

if(isset($_SESSION['selected_lang'])){
  $lang = $_SESSION['selected_lang'];
  
  //check if en or ko
  if(($lang != 'en') && ($lang != 'ko')) {
    $lang = 'ko';
  }
}

setLanguage ($lang, $path = '');//call function here
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Thread duplicate.

hrubos, be careful doing this...
Locked