Question

Sometimes after creating a dictionary, I get this error:

Undefined offset: 1 in /vendor/magento/framework/App/Language/Dictionary.php

a:4:{i:0;s:104:"Notice: Undefined offset: 1 in /srv/www/vendor/magento/framework/App/Language/Dictionary.php on line 196";i:1;s:4066:"#0 /srv/www/vendor/magento/framework/App/Language/Dictionary.php(196): Magento\Framework\App\ErrorHandler->handler(8, 'Undefined offse...', '/srv/www/vendor...', 196, Array) #1 /srv/www/vendor/magento/framework/App/Language/Dictionary.php(113): Magento\Framework\App\Language\Dictionary->readPackCsv('bleez', 'pt_br') #2 /srv/www/vendor/magento/framework/Translate.php(325): Magento\Framework\App\Language\Dictionary->getDictionary('pt_BR') #3 /srv/www/vendor/magento/framework/Translate.php(181):

The function which is in this path vendor\magento\framework\App\Language\Dictionary.php:

private function readPackCsv($vendor, $package)
    {
        $path = $this->componentRegistrar->getPath(ComponentRegistrar::LANGUAGE, strtolower($vendor . '_' . $package));
        $result = [];
        if (isset($path)) {
            $directoryRead = $this->directoryReadFactory->create($path);
            $foundCsvFiles = $directoryRead->search("*.csv");
            foreach ($foundCsvFiles as $foundCsvFile) {
                $file = $directoryRead->openFile($foundCsvFile);
                while (($row = $file->readCsv()) !== false) {

                    $result[$row[0]] = $row[1];
                }
            }
        }
        return $result;
    }

The problem is that there is a line in the dictionary which is not written properly, meaning that the fields are not properly created ( missing coma, or more than a comma) so the function sends an error. But the error doesn't give you any information about the line you have the error and with 13000 lines, you can't check each one.

So, which is the way you can know which line of the dictionary you created is wrong?

Was it helpful?

Solution

We modified the function in order to show us the text with the error. You can then search for it with a ctrl + f:

vendor\magento\framework\App\Language\Dictionary.php

private function readPackCsv($vendor, $package)
{
    $path = $this->componentRegistrar->getPath(ComponentRegistrar::LANGUAGE, strtolower($vendor . '_' . $package));
    $result = [];
    if (isset($path)) {
        $directoryRead = $this->directoryReadFactory->create($path);
        $foundCsvFiles = $directoryRead->search("*.csv");
        foreach ($foundCsvFiles as $foundCsvFile) {               
            $file = $directoryRead->openFile($foundCsvFile);
            while (($row = $file->readCsv()) !== false) {

                if (isset($row[0]) && isset($row[1])) {
                    $result[$row[0]] = $row[1];
                } else {
                    var_dump($row[0]);
                    var_dump($row[1]);
                    die("--------died---------");
                }
            }
        }
    }

We keep it out of the code, and if it happens we add it and modify the vendor until we find the error. Then we remove and we put the original function again.

Hopefully this will help.

OTHER TIPS

I Knew this is an old thread. someone might be useful

 private function readPackCsv($vendor, $package)
{
    $path = $this->componentRegistrar->getPath(ComponentRegistrar::LANGUAGE, strtolower($vendor . '_' . $package));
    $result = [];
    if (isset($path)) {
        $directoryRead = $this->directoryReadFactory->create($path);
        $foundCsvFiles = $directoryRead->search("*.csv");
        foreach ($foundCsvFiles as $foundCsvFile) {
            $file = $directoryRead->openFile($foundCsvFile);
            while (($row = $file->readCsv()) !== false) {
                if (is_array($row) && count($row) > 1) {
                    $result[$row[0]] = $row[1];
                }
            }
        }
    }
    return $result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top