6

I would like to translate the gender options for a customer. At first I thought that I could simply use a translation file for this, but looking into the template app/design/frontend/base/default/template/customer/widget/gender.phtml I see that the option label is not passed to the translate function.

What is the best process for updating these values for different stores?

9

The gender attribute is a dropdown attribute like any other one. Its labels are stored in the table eav_attribute_option_value.
But since there is no (out of the box) way to manage from the backend the customer attributes I see 2 options here.

Option 1. Quick and dirty:
Change the options in the template.
Copy app/design/frontend/base/default/template/customer/widget/gender.phtml to your theme and add after

 <?php $options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();?>

this:

<?php foreach ($options as $key => $option) : ?>
    <?php $options[$key]['label'] = $this->__($option['label']);?>
<?php endforeach;?>

Then you can translated the values like any other string.

Option 2 - Long but clean.
Insert into the table eav_attribute_option_value the values for your second (and third and so on..) store view.

I said that this is the 'clean' version.
The theory it's clean but the solution I have it's not that clean.

Identify the attribute id:

SELECT * FROM eav_attribute where attribute_code = 'gender';

Let's say it's 1000.
then identify the options for the attribute

select * from eav_attribute_option where attribute_id = 1000;

Let's say you get 100 and 101.
Then look in the eav_attribute_option_value to see which is which.

select * from eav_attribute_option_value where option_id in (100, 101);

now you will see which is the Male option and which is the Female option.

Let's say 100 => Male, 101=>Female.
Now insert your values for the second store store.

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';

Sorry but I don't have an upgrade script version for this.

I would go with the quick and dirty one. Fewer headaches.

  • Perfect, well apart from not having an update script version ;) – David Manners Sep 1 '14 at 11:05
  • @DavidManners. Then it's not perfect :) – Marius Sep 1 '14 at 11:05
1

Another solution, is to update (override) widget/gender.phtml to enable translation.

from :

<?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;?>

to :

<?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;?>

And finally, add your translate text in Mage_Customer.csv, like this :

"Male","Your translate for Male"
"Female","Your translate for Female"

Don't forget to clear the cache in system > cache management


EDIT 07 sept. 2015

I asked for a pull request on Magento2, see: https://github.com/magento/magento2/pull/1801

0

Working update script ... just need to adjust $labels variable to match your store IDs ...

<?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();
}
-1

Easily way

Find out the attributes

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'))

Now just do our update

:D

  • 1
    Initial question was asking about using translations, this will not work for that. – Danny Nimmo Jun 27 '17 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.