Magento – Translation of name prefix options

configurationmagento2multistoretranslate

I'm setting up a Multi-store in English, French and German. I'd like to enable Name Prefix as an optional field but Customer Configuration > Name and Address Options is only editable at the website level. So I cannot translate Mr, Mrs etc to Herr, Frau or Mr, Mme.

I saw a solution to a similar problem in M1 here but, being an average store owner, I don't know to adapt it to M2.

Has anyone found a way around this ? I can't be the first to run into this problem.

Best Answer

This is a Magento issue i know happens until 2.2.7 at least

you need to override using a custom module

di.xml:

preference for="Magento\Customer\Model\Options" type="Vendor\Module\Model\Options"

Class:

namespace Vendor\Module\Model;

use Magento\Config\Model\Config\Source\Nooptreq as NooptreqSource;

class Options extends \Magento\Customer\Model\Options {

    public function getNamePrefixOptions($store = null)
    {
        return $this->prepareNamePrefixSuffixOptions(
            $this->addressHelper->getConfig('prefix_options', $store),
            $this->addressHelper->getConfig('prefix_show', $store) == NooptreqSource::VALUE_OPTIONAL
        );
    }

    /**
     * Retrieve name suffix dropdown options
     *
     * @param null $store
     * @return array|bool
     */
    public function getNameSuffixOptions($store = null)
    {
        return $this->prepareNamePrefixSuffixOptions(
            $this->addressHelper->getConfig('suffix_options', $store),
            $this->addressHelper->getConfig('suffix_show', $store) == NooptreqSource::VALUE_OPTIONAL
        );
    }

    private function prepareNamePrefixSuffixOptions($options, $isOptional = false)
    {
        $options = trim($options);
        if (empty($options)) {
            return false;
        }
        $result = [];
        $options = explode(';', $options);
        foreach ($options as $value) {
            $value = $this->escaper->escapeHtml(trim($value));
            $result[__($value)] = __($value);
        }
        if ($isOptional && trim(current($options))) {
            $result = array_merge([' ' => ' '], $result);
        }

        return $result;
    }

}

the difference: original: $result[$value] = $value; fixed: $result[$value] = __($value);

Related Topic