[eluser]MaD HamsteR[/eluser]
In addition, i implemented translator class for PHPTAL to use in CI.
Instructions:
1. Create file translator.php in libraries/ folder
2. Put this code inside translator.php file
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//Include PHPTAL translation interface
require_once 'phptal/PHPTAL/TranslationService.php';
class I18n implements PHPTAL_TranslationService{
private $_currentLang = NULL;
private $_currentDomain = array();
private $_context = array();
private $CI = NULL;
/**
* @param string (name of the language)
* @return string (language you've just set)
*
* This method sets translation language.
* Name of the language is a dir name where you keep your translation files
*/
public function setLanguage($language='english')
{
$this->CI = &get;_instance(); //BUGGGGGG!!!! CI FORUM BUG, REMOVE the ; SYNTAX ERROR!
$this->_currentLang = $language;
return $this->_currentLang;
}
/**
* @param string (translation file name)
* @return void
*
* You can separate translations in several files, and use only when needed.
* Use this method to specify witch translation file you want to
* use for current controller.
*/
public function useDomain($domain)
{
$file = APPPATH."/language/{$this->_currentLang}/{$domain}.php";
$lang = include($file);
$this->_currentDomain = array_merge($this->_currentDomain, $lang);
$this->CI->tal->setTranslator($this);
}
/**
* Set an interpolation var.
*
* Replace all ${key}s with values in translated strings.
*/
public function setVar($key, $value){
$this->_context[$key] = $value;
}
/**
* Translate a text and interpolate variables.
*/
public function translate($key, $htmlescape=true)
{
$value = $this->_currentDomain[$key];
while (preg_match('/\${(.*?)\}/sm', $value, $m)){
list($src,$var) = $m;
if (!array_key_exists($var, $this->_context)){
$err = sprintf('Interpolation error, var "%s" not set', $var);
throw new Exception($err);
}
$value = str_replace($src, $this->_context[$var], $value);
}
return $value;
}
/**
* Not implemented yet, default encoding is used
*/
public function setEncoding($encoding)
{
}
}
3. Set autoload “translator” library in autoload config file AFTER tal library, or preload it manually in your script
Use like
Code:
//Some controller
function index()
{
$this->translator->setLanguage('russian');
$this->translator->useDomain('common'); //Commnon UI elemnts, buttons, links etc.
$this->translator->useDomain('register'); //Specific page elements
$this->tal->display('register.html'); //I LOVE Macro Extension for TAL!
}
I assume that you have common.php and register.php files inside
/system/application/language/russian(whatever)/ folder
Language file should look like this:
Code:
//Keys which will be replaced with values in templates like i18n:translate="string:menu"
return array(
'menu' => 'Навигация',
'index' => 'Главная',
'rules' => 'Участие',
'photos' => 'Фото',
'forum' => 'Форум',
'about' => 'О проекте',
'contacts' => 'Контакты',
);
Duplicate keys will be overwritten!
Thanx and DO report any bugs!