创建多语言主页翻译


Creating multilanguage homepage translating

好的,我得到了class文件,在这个文件中我得到了function -

var $text;                     
public function languages()
{
  if (isset($_GET['lang']) && $_GET['lang'] != '')
  {
    $_SESSION['lang'] = $_GET['lang'];
  }
  switch($_SESSION['lang'])
  {
    case 'en_EN': require_once('language/lang.eng.php');break;
    case 'lv_LV': require_once('language/lang.lv.php');break;
    case 'ru_RU': require_once('language/lang.ru.php');break;
    default: require_once('language/lang.eng.php');
  }
  $this->text = $text;
}
public function translate($txt)
{
  if(isset($this->text[$txt]))
  {
    return $this->text[$txt];
  }
}

如果我是通过index.php翻译像这样-> echo $index->translate('search');翻译ok,但如果我翻译的东西在类文件,例如-

if ($country_rows > 0)
{
  $_SESSION['country'] = $_GET['country'];
}
else
{                                                      
  $_SESSION['country'] = $this->translate('all_countries');
}
}
if ($_SESSION['country'] == '')
{ 
  $_SESSION['country'] = $this->translate('all_countries');
}

它没有显示。在index.php头文件中包含了-

require_once('class.index.php');
$index = new index;
$index->get_country();
$index->languages();

什么可能是问题,我怎么能解决它,所以我可以翻译类文件内的一切?非常感谢您的帮助!

第一次猜测:会话没有开始?

session_start();

猜2:假设您在另一个类中使用$this->translate(),您应该首先初始化对象,在下面的示例中,我将translate类传递给var $index;

<?
include_once('class.index.php');
class myClass {
  var $index;
  public function __construct() {
    $index = new index();
    $index->get_country();
    $index->languages();
    $this->index = $index;
  }
  public function yourFunction() {
    echo $this->index->translate('all_countries');
    print_r($this->index);
  }
}
?>