Search code examples
magentoattributesadditionmultilingualentity-attribute-value

Create Magento attribute option multilingual programmatically


i am searching for a way to create magento attribute options multilingual, for more than one language.

Let's say i do have store view name 'german' and 'english'. I do retrieve the store language codes with (the languages must be set each on the store view configuration pages):

$storelist = Mage::app()->getStores();
$storelang = array();
foreach ($storelist as $id => $s) {
    $storelang[$id] = substr(Mage::getStoreConfig('general/locale/code', $id), 0, 2);
}

This way i do create attribute options (the attributes do exist already):

function addAttributeValue($arg_attribute, $arg_value) {
   $attribute_model = Mage::getModel('eav/entity_attribute');
   $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
   $attribute = $attribute_model->load($attribute_code);

   if (!attributeValueExists($arg_attribute, $arg_value)) {
       $value['option'] = array($arg_value, $arg_value);
       $result = array('value' => $value);
       $attribute->setData('option', $result);
       $attribute->save();
       echo "Attribute '" . $arg_attribute . "' with 
       option '" . $arg_value . "' saved.<br />";
}

But where does the 2nd language come to play? Right now only the default value/default value gets filled into the database.

I would like to prepare a loop for each language, but i do not know the right "switch" for the language.

When creating products, not attribute options, there is a method so set all product fields according to the language with

$Product->setStoreId($lang_value);

but i am not sure about this on attribute options. Can you guys give me a hint?

Thanks.


Solution

  • Problem solved. Thanks to Marius for pointing out my messed up array. This line was wrong:

    $value['option'] = array($arg_value, $arg_value);
    

    Set it to something like:

    $value['option'] = array($arg_value_label, $arg_value_lang1, $arg_value_lang2);
    

    Now i am able to load the the values for each language and set them with that single line above. Maybe this is not the best solution, but it works for me at the moment. Have a nice weekend everyone.