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

Post Reply
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 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);
		}
	}
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What do you want to understand? It defines constants.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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/')
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post 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
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post 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
Post Reply