Question

I have a what should be a simple task of translating the roles and permissions control page in b2b but I just cant make it work

enter image description here

I know that the texts are from this file

vendor/magento/module-company/etc/company_acl.xml

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Company::index" title="All" translate="title" sortOrder="100">
                <resource id="Magento_Company::view" title="Company Profile" translate="title" sortOrder="100">
                    <resource id="Magento_Company::view_account" title="Account Information (View)" translate="title" sortOrder="100">
                        <resource id="Magento_Company::edit_account" title="Edit" translate="title" sortOrder="100" />
                    </resource>
                    <resource id="Magento_Company::view_address" title="Legal Address (View)" translate="title" sortOrder="200">
                        <resource id="Magento_Company::edit_address" title="Edit" translate="title" sortOrder="200" />
                    </resource>
                    <resource id="Magento_Company::contacts" title="Contacts (View)" translate="title" sortOrder="300" />
                </resource>
                <resource id="Magento_Company::user_management" title="Company User Management" translate="title" sortOrder="200">
                    <resource id="Magento_Company::roles_view" title="View roles and permissions" translate="title" sortOrder="100">
                        <resource id="Magento_Company::roles_edit" title="Manage roles and permissions" translate="title" sortOrder="200" />
                    </resource>
                    <resource id="Magento_Company::users_view" title="View users and teams" translate="title" sortOrder="300">
                        <resource id="Magento_Company::users_edit" title="Manage users and teams" translate="title" sortOrder="400" />
                    </resource>
                </resource>
                <resource id="Magento_Sales::all" title="Sales" translate="title" sortOrder="10">
                    <resource id="Magento_Sales::place_order" title="Checkout (place order)" translate="title" sortOrder="20">
                        <resource id="Magento_Sales::payment_account" title="Use Pay On Account method" translate="title" sortOrder="30" />
                    </resource>
                    <resource id="Magento_Sales::view_orders" title="View orders" translate="title" sortOrder="40">
                        <resource id="Magento_Sales::view_orders_sub" title="View orders of subordinate users" translate="title" sortOrder="40" />
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

what I found on the internet but sadly none helped:

In Magento 2, what does `translate="true"` really do in XML?

https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/translations/translate_theory.html

How to translate title attribute in acl.xml

What I tried after making a working i18n translation file:

1- change the translate attribute to true

2- change the translate attribute to the string i want to translate

Any advice would be appreciated Thanks in advance

Was it helpful?

Solution 2

So in the end I found the responsible block for this area which is: vendor/magento/module-company/Block/Company/Role/Edit.php

And at first I tried the solution of (Adam H.) but it was then pointed out to me that if there is a future update for magento that changes this my override might be breaking the code

So i ended up building an after plugin in a new module

<?php

namespace ModuleScope\ModuleName\Plugin;

use Magento\Company\Block\Company\Role\Edit;

class TranslateRolesAndPermissionsPlugin
{
    public function afterGetTreeJsOptions(Edit $subject, array $data)
    {
        array_walk_recursive($data, function (&$elementValue, $elementKey) {
            if ($elementKey === "text") {
                $elementValue = __($elementValue);
            }
        });
        return $data;
    }
}

and added this di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Company\Block\Company\Role\Edit">
        <plugin name="translate_roles_and_permissions" type="ModuleScope\ModuleName\Plugin\TranslateRolesAndPermissionsPlugin" />
    </type>
</config>

I think this way is clearer and more clean and can mange changes from Magento

OTHER TIPS

I ran into the same problem yesterday. Apparently, there is no translation function in module-company. I could solve the problem by overwriting the class Block/Company/Role/Edit.php and changing the code in line 121 as follows:

from

$resources[$counter]['text'] = $resources[$counter]['title'];

to

$resources[$counter]['text'] = __($resources[$counter]['title']);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top