创建多语种主页翻译

时间:2011-10-06 14:49:13

标签: php

好的,所以我得到了类文件,其中我有功能 -

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');它翻译好,但如果我正在翻译类文件中的内容,例如 -

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();

可能是什么问题,我该如何修复它,所以我也可以翻译类文件中的所有内容?非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

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

session_start();
第二个猜测: 假设您在另一个类中使用$this->translate(),则应首先启动该对象,在以下示例中,我将translate class传递给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);
  }
}
?>
相关问题