Jump to content

multilingual PHP APPLICATION


mamadou diallo

Recommended Posts

This really isn't as hard as some may have it look...

 

The most simple implementation would be to use constants, and include a file containing those constants. I'm not a big fan of that method myself, but it is the easiest.

 

Example:

 

include 'messages_eng.php';
if(!$something){
   echo SOME_MSG;
}

 

I personally prefer storing messages in an XML file, and fetching the appropriate message based on the status of an object:

 

public function getMessage($object){
    foreach($this->xml->getElementById(get_class($object)) as $node){
        if($node->getAttribute('status') == $object->getStatus()){
             return $node->nodeValue;
        }
    }
}

 

And please, do not use the letter combination 'asap' in any of your questions. You only annoy people with it and you are actually LESS likely to get a rapid response.

Link to comment
Share on other sites

  • 2 weeks later...

I use a db table (but think I will switch to xml) but similar philosphy - I have a field for each language and each row has the string for a key specified and the strings in each langauge - I load these into an array and simply use a key to echo out the correct string... (only load the field for the specified language into the array.)

Link to comment
Share on other sites

  • 2 weeks later...

There are plenty of different approaches. I use a strategy based on the status of an object, but you may prefer a template driven approach, especially if you're not that much of an object bigot.

 

Here's a thread where I posted a very simple method of getting messages for use in a template from an ini file.

 

The problem with ini files is that their not very easy to write to.

 

The actual storage method you use isn't that important. Use what you are familiar with. If you're not comfortable using XML, then don't, or get comfortable with it, I'm not making this a DOM or SimpleXML tutorial.

 

To build a little on that post, a very simple custom tag parser:

 

class TemplateParser {

private $buffer;

public function __construct($string){
	$this->buffer = new DOMDocument();
	$this->buffer->loadXml($string);	
}
public function evaluate(){
	$msgNodes = $this->buffer->getElementsByTagName('msg');
	foreach($msgNodes as $node) {
		$msg = Messages::get($node->getAttribute('id'));
		$parentNode = $node->parentNode;
		$node->parentNode->nodeValue = $msg;
	}
	return $this->buffer->saveXml();
}
}
$parser = new TemplateParser('<h1><msg id="get_barred"/></h1>');
echo $parser->evaluate();

 

If you prefer simple string parsing, a tiny bit of regex goes a long way:

 

class TemplateParser {

private $buffer;

public function __construct($string){
	$this->buffer = $string;
}
public function evaluate(){
	if(preg_match_all('/<msg id="(.*?)"\s?\/>/', $this->buffer, $matches)){
		//The id's ($1) are in $matches[1]
		foreach($matches[1] as $offset => $id) {
			//The full matched strings ($0) are in $matches[0]
			$this->buffer = str_replace($matches[0][$offset], Messages::get($id), $this->buffer);
		}		
	}
	return $this->buffer;	
}
}
$parser = new TemplateParser('<h1><msg id="get_barred"/></h1><h1><msg id="meh"/></h1>');
echo $parser->evaluate();

Link to comment
Share on other sites

  • 1 month later...

I use a database to store the messages and get those messages out using adodb and caching.

Not the best way I guess but it is something I understand.

I use a database because you can query for the message you need in more than one language. I am not up to speed with XML but I guess it would be possible.

I need this because I do not understand all the languages that the site has to support and this enables me to get the default language message and the user desired language message in one query.

check if the user desired language message exist and display that message if it exist or the default language message if it does not exist.

 

I went with the database approach because it makes it easy to add languages afterwards.

 

Probably of topic but I just wanted to let you know.

 

anatak

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.