-
12-12-2019 - |
题
我想为客户翻译性别选项。起初我以为我可以简单地使用翻译文件来实现此目的,但查看了模板 app/design/frontend/base/default/template/customer/widget/gender.phtml
我看到选项标签没有传递给翻译函数。
为不同商店更新这些值的最佳流程是什么?
解决方案
这 gender
属性是一个与其他属性一样的下拉属性。它的标签存储在表中 eav_attribute_option_value
.
但由于没有(开箱即用的)方式从后端管理客户属性,我在这里看到两个选项。
选项1。 又快又脏:
更改模板中的选项。
复制 app/design/frontend/base/default/template/customer/widget/gender.phtml
到你的主题并添加后
<?php $options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();?>
这:
<?php foreach ($options as $key => $option) : ?>
<?php $options[$key]['label'] = $this->__($option['label']);?>
<?php endforeach;?>
然后您可以像翻译任何其他字符串一样翻译这些值。
选项2 - 长但干净。
插入表中 eav_attribute_option_value
第二个(第三个等等……)商店视图的值。
我说这是“干净”版本。
理论上它很干净,但我的解决方案并不那么干净。
识别属性id:
SELECT * FROM eav_attribute where attribute_code = 'gender';
假设是 1000。
然后确定属性的选项
select * from eav_attribute_option where attribute_id = 1000;
假设您得到 100 和 101。
然后查看 eav_attribute_option_value
看看哪个是哪个。
select * from eav_attribute_option_value where option_id in (100, 101);
现在你会看到哪个是 Male
选项,哪个是 Female
选项。
比方说 100 => Male
, 101=>Female
.
现在插入第二个商店的值。
Insert into `eav_attribute_option_value` set option_id = 100, store_id = 1, value = 'Translation for Male here';
Insert into `eav_attribute_option_value` set option_id = 101, store_id = 1, value = 'Translation for Female here';
抱歉,我没有这方面的升级脚本版本。
我会选择又快又脏的。头痛更少。
其他提示
另一个解决方案是更新(覆盖) widget/gender.phtml
启用翻译。
从 :
<?php foreach ($options as $option):?>
<option value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' selected="selected"' ?>><?php echo $option['label'] ?></option>
<?php endforeach;?>
到 :
<?php foreach ($options as $option):?>
<option value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' selected="selected"' ?>><?php echo $this->__($option['label']) ?></option>
<?php endforeach;?>
最后,将您的翻译文本添加到 Mage_Customer.csv
, , 像这样 :
"Male","Your translate for Male"
"Female","Your translate for Female"
不要忘记清除缓存 system > cache management
9 月 7 日编辑2015年
我在 Magento2 上请求了一个 Pull 请求,请参阅: https://github.com/magento/magento2/pull/1801
工作更新脚本...只需要调整 $labels
变量来匹配您的商店 ID ...
<?php
require_once('./app/Mage.php');
Mage::app();
$labels = array(
'Male' => array( # default value
1 => 'Male-DE', # key is storeId
2 => 'Male-FR'
),
'Female' => array(
1 => 'Female-DE',
2 => 'Female-FR'
)
);
$attributeCode = 'gender';
$attribute = Mage::getModel('customer/customer')->getResource()
->getAttribute($attributeCode);
$values = array();
if ($attribute->usesSource()) {
foreach ($labels as $label => $translations) {
if ($optionId = $attribute->getSource()->getOptionId($label)) {
$values[$optionId] = array(0 => $label) + $translations;
}
}
}
$data = array();
$data['option']['value'] = $values;
$attribute->addData($data);
try {
$attribute->save();
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
轻松的方式
找出属性
select * from eav_attribute_option_value where option_id in (select option_id from eav_attribute_option where attribute_id = (SELECT attribute_id FROM eav_attribute where attribute_code = 'gender'))
现在只需进行我们的更新
:D