PHP 声明具有本地化字符串值的数组
PHP declaring array with localized string values
专家!
这肯定是一个新手问题,但我坚持我的项目。问题是:声明了一个对象数组,这些对象在声明数组时应该有一个值。如果值以字符串格式给出,代码工作正常,例如'foo bar'。但是,如果值是由函数设置的,则相同的数组声明会给我一个错误。
这是代码...
工作正常:
private static $summary_fields = array(
'Title' => 'Title',
'Description' => 'Description',
'SummaryOfPrice' => 'Amount',
'Country.Title' => 'Country'
);
工作不正常:
private static $summary_fields = array(
'Title' => _t('FlatFeeShippingRate.DESCRIPTION', 'Title'),
'Description' => _t('FlatFeeShippingRate.DESCRIPTION', 'Description'),
'SummaryOfPrice' => _t('FlatFeeShippingRate.AMOUNT', 'Amount'),
'Country.Title' => _t('FlatFeeShippingRate.COUNTRY', 'Country')
);
这里的函数_t()是对字符串进行本地化的函数。如果未从 .yml 语言特定文件中找到本地化字符串,则它 returns 作为第二个参数给出的字符串。
_t() 产生错误时数组声明有什么问题:
Parse error: syntax error, unexpected '(', expecting ')'
其他地方使用了_t()-函数作为魅力!
请帮忙。
谢谢!
来自http://us.php.net/manual/en/language.oop5.static.php:
Like any other PHP static variable, static properties may only be
initialized using a literal or constant; expressions are not allowed.
So while you may initialize a static property to an integer or array
(for instance), you may not initialize it to another variable, to a
function return value, or to an object.
这意味着您不能在初始化程序中使用 _t()
函数。但是,您可以按照建议 here. You can also initialize the array after you create the class, suggested here.
创建一个 getter 函数
专家!
这肯定是一个新手问题,但我坚持我的项目。问题是:声明了一个对象数组,这些对象在声明数组时应该有一个值。如果值以字符串格式给出,代码工作正常,例如'foo bar'。但是,如果值是由函数设置的,则相同的数组声明会给我一个错误。
这是代码...
工作正常:
private static $summary_fields = array(
'Title' => 'Title',
'Description' => 'Description',
'SummaryOfPrice' => 'Amount',
'Country.Title' => 'Country'
);
工作不正常:
private static $summary_fields = array(
'Title' => _t('FlatFeeShippingRate.DESCRIPTION', 'Title'),
'Description' => _t('FlatFeeShippingRate.DESCRIPTION', 'Description'),
'SummaryOfPrice' => _t('FlatFeeShippingRate.AMOUNT', 'Amount'),
'Country.Title' => _t('FlatFeeShippingRate.COUNTRY', 'Country')
);
这里的函数_t()是对字符串进行本地化的函数。如果未从 .yml 语言特定文件中找到本地化字符串,则它 returns 作为第二个参数给出的字符串。
_t() 产生错误时数组声明有什么问题:
Parse error: syntax error, unexpected '(', expecting ')'
其他地方使用了_t()-函数作为魅力!
请帮忙。
谢谢!
来自http://us.php.net/manual/en/language.oop5.static.php:
Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.
这意味着您不能在初始化程序中使用 _t()
函数。但是,您可以按照建议 here. You can also initialize the array after you create the class, suggested here.