templates, php function or preg_replace() ?

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
remco-v
Forum Newbie
Posts: 8
Joined: Thu May 18, 2006 5:11 am

templates, php function or preg_replace() ?

Post by remco-v »

I am not sure if this post has the right topic but its kind of a tricky one for me.
What i am trying to do is provide an eazy way for designers work with multi lingual pages.
Its only about labels and short messages, real content is stored in the database and will be handled by the business logic.

Here is how a design would look at the moment.

Code: Select all

<table width="333" border="1" class="tableDark">
    <tr>
      <td><?translate('User name')?></td>
      <td><input name="username" type="text" id="username" accesskey="u" tabindex="1" /></td>
    </tr>
    <tr>
      <td><?translate('Password')?></td>
      <td><input name="password" type="password" id="password" accesskey="p" tabindex="2" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input name="login" type="submit" id="login" accesskey="l" tabindex="3" value="<?translate('Login')?>" /></td>
    </tr>
  </table>
</form>
The question for me is that do i want designers to have to deal with the <?translate()?> part?
I was thinking about preg_replacing <label>text</label> by the translated string. But this will decrease speed rather a lot!

I was also thinking that it would be nice if i could overload print(), cause this would be more designer friendly.

I am kind of stuck with this one.

At the moment i implemented the <?translate()?> approach.
Also I got a nice class that keeps track of all translate calls inside every design and generates a .loc file for me.

I know i am new to this forum but any help would be appriciated!
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

A much-used solution is to create a sepeate file to be included with a list of all the words as variables, for example:

Code: Select all

$hello = 'hello';
$goodybye = 'bye';
$cake = 'yummy';
etc...

Then the user only has to deal with one file.
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

Here is what I use in my framework:

Code: Select all

<?php
class Yarns{
    private static $_defaultLanguage;
    private static $_registry = array();
    
    public static function setDefaultLanguage($language){
        self::$_defaultLanguage = $language;
    }
    
    public static function getDefaultLanguage(){
        if (!isset(self::$_defaultLanguage)) {
            $config = Config::get('Yarns');
            self::$_defaultLanguage = $config['defaultLanguage'];
        }
        return self::$_defaultLanguage;
    }
    
    public static function make($source, $language = NULL){
        if ($language == NULL) { $language = self::getDefaultLanguage(); }
        if (isset(self::$_registry[$source][$language])) {
            return self::$_registry[$source][$language];
        }
        self::$_registry[$source][$language] = new Yarns($source, $language);
        return self::$_registry[$source][$language];
    }
    
    private $_yarns = array();

    private function __construct($source, $language){
        $this->_yarns = @include "yarns/$source/$language.inc";
        if (!$this->_yarns) {
            trigger_error("Could not load yarns file for '$source/$language'.");
        }
    }
    
    function __get($name){
        $yarn = $this->_yarns[$name];
        if (!isset($yarn)) { $yarn = $name; }
        return $yarn;
    }
    
    function dump($name){
        echo $this->$name;
    }
}
?>
You use it like this:

Code: Select all

<?
$y = Yarns::make('Class_Name');
?>
bla<?=$y->stringName?>
The good thing is that is will automatically replace unknown strings with their names. Agile. Plus, you can use it inside heredoc.
remco-v
Forum Newbie
Posts: 8
Joined: Thu May 18, 2006 5:11 am

Hi again.

Post by remco-v »

Tx for your replies,

Perhaps i forgot to mention that.
1) I am using a class simulair to yarns, only i created a global wrapper function that translates the string and returns string if no translation is found.
2) I created 1 translation file per language for the whole application.
3) And i created a class that keeps this file in sink with calls made to translate();

My real problem is how to make it more user friendly for the designers?
What is the best solution, or is this as good as it gets?

Code: Select all

<?php
/* ------------------------------*\
 * VESSIE 2005                    |
 * Author: Remco Verton           |
 * remcoverton@hotmail.com        |
 * ------------------------------*/
// Translation function wrapper
function translate($string){
	print($GLOBALS['_localisation']->translate($string));
}
// This class will provide the localisation!
// I choose not to use gettext!
// User language preferences will be strored inside a cookie on his computer or inside the user database table
class V_Localisation extends V_Object{
	
	private $language;		// String
	private $messagePool;	// Array()
	private $locPath;		// String
	
	public function __construct($language,$locPath){
		// To call the translate function from the wrapper
		$GLOBALS['_localisation'] = $this;
		// Adding a slash to prevent mistakes 
		$this->locPath = $locPath.'/'; 
		$this->language = $language;
		
		$file = $this->locPath.$this->language.'.loc';
		if(is_file($file)){
			$msg = array();
			include($file);			
			$this->messagePool = $msg;
		}
		else{
			$this->messagePool = array();
		}
	}
	
	public function translate($string){
		if(array_key_exists($string,$this->messagePool)){
			return $this->messagePool[$string];
		}
		//trigger_error('No translation found for: "'. $string .'" ',E_USER_NOTICE);
		return $string;
	}
	
	// If an instance is stored in the session make sure that the wrapper function can call $this
	public function __wakeup(){
		$GLOBALS['_localisation'] = $this;
	}
}
?>
my NL.loc file

Code: Select all

<?php
// Translation file for language: NL
// May 18 2006 16:23:10 
$msg['Edit']='Bewerken';
//MODULES/CORE/CONTACT/Contact.html
//MODULES/CORE/HEADER/Header.html
//MODULES/CORE/HOME/Home.html
$msg['Edit profile']='Profiel bewerken';
//MODULES/CORE/HEADER/Header.html
$msg['Good by']='Tot ziens';
//MODULES/CORE/LOGIN/logMeOut.html
$msg['Login']='Inloggen';
//MODULES/CORE/LOGIN/login.html
$msg['Password']='Wachtwoord';
//MODULES/CORE/LOGIN/login.html
$msg['Save']='Opslaan';
//MODULES/CORE/CONTACT/ADMIN/ContactAdmin.html
//MODULES/CORE/HEADER/ADMIN/HeaderAdmin.html
//MODULES/CORE/HOME/ADMIN/HomeAdmin.html
$msg['This is the page footer']='Dit is the footer pagina';
//MODULES/CORE/FOOTER/Footer.html
$msg['User name']='Gebruikersnaam';
//MODULES/CORE/LOGIN/login.html
$msg['You are not autorised']='U heeft geen toegang';
//MODULES/CORE/LOGIN/notAutorised.html
?>
Post Reply