Product attribute labels in admin attribute grid and admin attribute view are not translatable (and my fix)

localization
bug-fix

(Dmitry Fedyuk) #1

This is long-term issue from Magento 1.x and still is in Magento 2.

Surely, you can translate product attribute labels manually by editing attributes: typing new labels and saving them to Magento database.
But it is inconvenient because:

  • you must do it manullay for each new Magento installation and for each of 43 standard product attributes: a lot of work!
  • attribute labels will not react to the current admin locale.

The bug is similar to 73

My fix (with 3 plugins)

Plugin №1

See 73

Plugin №2

<type name='\Magento\Eav\Model\Resource\Entity\Attribute'>
	<plugin
		name='Df\Eav\Model\Resource\Entity\AttributePlugin'
		type='Df\Eav\Model\Resource\Entity\AttributePlugin'
		sortOrder='100'
	/>
</type>
namespace Df\Eav\Model\Resource\Entity;
use Magento\Eav\Model\Resource\Entity\Attribute;
class AttributePlugin {
	/**
	 * @see Attribute::load()
	 * @param Attribute $subject
	 * @param \Closure $proceed
	 * @param \Magento\Eav\Model\Entity\Attribute $object
	 * @param mixed $value
	 * @param string|null $field [optional]
	 * @return Attribute
	 */
	public function aroundLoad(
		Attribute $subject
		, \Closure $proceed
		, \Magento\Eav\Model\Entity\Attribute $object
		, $value
		, $field = null
	) {
		$proceed($object, $value, $field);
		$object['frontend_label'] = __($object['frontend_label']);
		return $subject;
	}
}

Plugin №3

<type name='\Magento\Eav\Model\Resource\Entity\Attribute\Collection'>
	<plugin
		name='Df\Eav\Model\Resource\Entity\Attribute\CollectionPlugin'
		type='Df\Eav\Model\Resource\Entity\Attribute\CollectionPlugin'
		sortOrder='100'
	/>
</type>
namespace Df\Eav\Model\Resource\Entity\Attribute;
use \Magento\Eav\Model\Entity\Attribute;
use \Magento\Eav\Model\Resource\Entity\Attribute\Collection;
class CollectionPlugin {
	/**
	 * @param Collection $subject
	 * @param Attribute $item
	 * @return array(Attribute)
	 */
	public function beforeAddItem(Collection $subject, Attribute $item) {
		$item['frontend_label'] = __($item['frontend_label']);
		return [$item];
	}
}

So now the product attributes are translatable:

GitHub issue: 1983


How to translate attribute labels on admin category pages
(Mugua) #2

Hi, DF
Just used your course, I had some confusing things about it.
1: your course base on 2.0.* dev ,but now it was 2.1.0 master, there have some different about this:
1st, the folder Resource changed to ResourceModel
2nd, Master edition module was in vendor/magento/module-***, but Dev editon was in app/code/Magento. How can I do it with your course in Master?
3rd, Should me use this Plugin in a full module, or just di.xml & ***plugin.php at all?

Thanks for your feedback.

Mugua in China.


(Mugua) #3

Just find the problem about it.
1st, the folder Resource were changed to ResourceModel
2nd, set the Plugin in the /app/code/Folder, it suit for Dev & Master.
3rd, just di.xml & ***plugin.php ok
4th, the php file should be utf-8(without BOM)