Page 1 of 1

multiple language

Posted: Sat Mar 10, 2007 3:10 am
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 undertsand 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) { // pokud radek neobsahuje ## nebude jeho obsah zpracovan
			// komentar
			//echo $text[$i];
			continue;

		}
		else {
			// nastaveni konstant
			list ($constant, $value) = explode("##", $text[$i]);
			$value = trim($value);
			define($constant, $value);
		}
	}
}
?>

Posted: Sat Mar 10, 2007 7:15 am
by feyd
What do you want to understand? It defines constants.

Posted: Sat Mar 10, 2007 3:31 pm
by hrubos
now, example I have find index.php and so how I do to use this function.

May be this is a stupid question, but in fact I tried many times, but not runs

Posted: Sat Mar 10, 2007 3:37 pm
by John Cartwright
you call it by doing this:

Code: Select all

setLanguage()
Voila!

Now, your function does have two optional arguments, the first one accepts a language indicator, and the other variable accepts a path to the language files.. so it may look like

Code: Select all

setLanguage('en', '/path/to/files/')

Posted: Sat Mar 10, 2007 3:39 pm
by feyd
Your English is very hard to understand. The only thing I can pull out is that you've attempted to use the function, but apparently you don't think it did anything.

The function won't return any data. It only attempts to process the file defining constants where it can, blindly, I will add.

Posted: Sun Mar 11, 2007 7:47 am
by hrubos
I would like ask a question
I have a file en_test.txt : TIT_REGISTER##Registration
then in my file index.php

Code: Select all

setLanguage();
..
... 

echo "<a href='register.php'>TIT_REGISTER</a>"
I did it but it can't be outputted

Posted: Sun Mar 11, 2007 9:00 am
by feyd
Constants are not parsed in strings the way variables are; in fact, they aren't parsed at all. You need to exit the string first.

Posted: Mon Mar 12, 2007 1:25 am
by hrubos
I have done it and it runs.This is code, in which someone is interested.

Code: Select all

if (isset($_GET['language'])) {
		$language = $_GET['language'];
	}
	else {
		$language = "ko";
	}
	setLanguage($language, "");
Then call a text from file.txt by command echo .
Example : echo TIT_HOME;
You will have 2 languages.

Thank all in advance

Posted: Mon Mar 12, 2007 1:54 am
by dibyendrah
I guess you're into localizing application. getext will be useful if you're developing multilingual application.

Please refer the following manual :
http://www.php.net/gettext