mamadou diallo Posted September 21, 2007 Share Posted September 21, 2007 Hello everyone i'm currently working on a PHP application that requires the use of multiple languages and im really having problems doing it. if there is anyone that can help, please reply asap. thanks Quote Link to comment Share on other sites More sharing options...
448191 Posted September 21, 2007 Share Posted September 21, 2007 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted October 1, 2007 Share Posted October 1, 2007 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.) Quote Link to comment Share on other sites More sharing options...
mamadou diallo Posted October 12, 2007 Author Share Posted October 12, 2007 well thanks to both of for replying to my post. please can you guys provide step by step instructions on how to use xml to build a multilingual site. the reason for that is that im new to this world. thanks everyone trying to help. Quote Link to comment Share on other sites More sharing options...
448191 Posted October 13, 2007 Share Posted October 13, 2007 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(); Quote Link to comment Share on other sites More sharing options...
anatak Posted November 22, 2007 Share Posted November 22, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.