Magento2 Module Localisation – How Does Translation Scope Work in Magento 2?

localisationmagento2module

In Magento 1 different modules could provide different translations for the same string and the various __() methods in helpers and blocks always passed a module scope to the translator model, to determine which translation is used. In theme translations you could specify the module scope with translations like:

"Mage_Sales::Tax","Tax"

In Magento 2, one global function __() is used for translation and it seems to know nothing about the context where it is called:

function __()
{
    $argc = func_get_args();

    $text = array_shift($argc);
    if (!empty($argc) && is_array($argc[0])) {
        $argc = $argc[0];
    }

    return new \Magento\Framework\Phrase($text, $argc);
}

Does this mean there is no such thing as a module scope anymore? If so, what happens if two different modules define different translations for one string?

I'm thinking of ambiguous words like "state" that will be translated different depending on the context. How does Magento 2 deal with these?

Best Answer

Does this mean there is no such thing as a module scope anymore?

Yes

If so, what happens if two different modules define different translations for one string?

Magento 2 load translation from all modules and then load from current (based on current controller). In most cases this resolve problem.

I'm thinking of ambiguous words like "state" that will be translated different depending on the context. How does Magento 2 deal with these?

Looks like is no big issue there. In any case Magento can add inline context like State (Address), State (order)

Related Topic