1

I splitted the logic of my application in multiple packages, and each one encapsulates it's own files (views, controllers, models, resources, migrations); everything is working fine, but, I'm having problems with the translations.

In the AppServiceProvider of each package I'm calling the function loadJsonTranslationsFrom and passing the path where the JSON translation file is located, like $this->loadJsonTranslationsFrom(__DIR__ . '/../../resources/lang').

This method works fine after loading the JSON translation file from the main project and one of the packages, but before that the function fails to load the rest of the files.

After a lot of search, I decided to debug the framework files by hand, then I modified the file vendor/laravel/framework/src/Illuminate/Translation/FileLoader.php in the following way:

In the function addJsonPath($path) (line 174), I added dump($this) to print the current state of the loader instance, after the line $this->jsonPaths[] = $path (line 176):

    /**
     * Add a new JSON path to the loader.
     *
     * @param  string  $path
     * @return void
     */
    public function addJsonPath($path)
    {
        $this->jsonPaths[] = $path;
        dump($this);
    }

In the function loadJsonPaths($locale) (line 139), I added dump('debug') to check when this line is called, after the very beginning of the function:

    /**
     * Load a locale from the given JSON file path.
     *
     * @param  string  $locale
     * @return array
     *
     * @throws \RuntimeException
     */
    protected function loadJsonPaths($locale)
    {
        dump('debug');
        return collect(array_merge($this->jsonPaths, [$this->path]))
        ...
    }

After that changes I found that the function loadJsonPaths is called before the other packages have loaded their respective translation files, this is the output:

FileLoader {#164 ▼
  #files: Filesystem {#156}
  #path: "/home/user/Projects/html/myerp/resources/lang"
  #jsonPaths: array:1 [▼
    0 => "/home/user/Projects/html/myerp/vendor/myerp/reports/src/Providers/../../resources/lang"
  ]
  #hints: []
}

"debug"

FileLoader {#164 ▼
  #files: Filesystem {#156}
  #path: "/home/user/Projects/html/myerp/resources/lang"
  #jsonPaths: array:2 [▼
    0 => "/home/user/Projects/html/myerp/vendor/myerp/reports/src/Providers/../../resources/lang"
    1 => "/home/user/Projects/html/myerp/vendor/myerp/communications/src/Providers/../../resources/lang"
  ]
  #hints: array:1 [▶]
}

FileLoader {#164 ▼
  #files: Filesystem {#156}
  #path: "/home/user/Projects/html/myerp/resources/lang"
  #jsonPaths: array:3 [▼
    0 => "/home/user/Projects/html/myerp/vendor/myerp/reports/src/Providers/../../resources/lang"
    1 => "/home/user/Projects/html/myerp/vendor/myerp/communications/src/Providers/../../resources/lang"
    2 => "/home/user/Projects/html/myerp/vendor/myerp/payments/src/Providers/../../resources/lang"
  ]
  #hints: array:2 [▶]
}

FileLoader {#164 ▼
  #files: Filesystem {#156}
  #path: "/home/user/Projects/html/myerp/resources/lang"
  #jsonPaths: array:3 [▼
    0 => "/home/user/Projects/html/myerp/vendor/myerp/reports/src/Providers/../../resources/lang"
    1 => "/home/user/Projects/html/myerp/vendor/myerp/communications/src/Providers/../../resources/lang"
    2 => "/home/user/Projects/html/myerp/vendor/myerp/payments/src/Providers/../../resources/lang"
    3 => "/home/user/Projects/html/myerp/vendor/myerp/persons/src/Providers/../../resources/lang"
  ]
  #hints: array:2 [▶]
}

As you can see, this behavior causes that only the JSON translation files of the main project and the first package are loaded.

My questions are:

Is this the correct behavior? How could I override this behavior and load the translation files of all the packages?

I'm using Laravel 5.7 over PHP 7.1

I hope you could help me, thanks in advance.

jceceniceros
  • 87
  • 1
  • 10

1 Answers1

2

Laravel is working as intended when it comes to registering and loading the JSON translation files from your various packages. What appears to be happening is something in your reports package is using a translation, causing the Translator to go ahead and say "Okay, get me all the translations so I can find the string I need." earlier than expected.

It is difficult to tell you where or why that might be happening without seeing how you're loading these packages, and/or where you might be using a Translation. The best recommendation I have is to install Xdebug and add a breakpoint in place of your dump('debug') line. This will tell you what stack trace has occurred to reach, and can help you trace back to why the translations are being loaded when you aren't expecting.

An alternative would be to use short key translations, which are namespaced and loaded independently instead of as a whole and combined.

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40
  • Yes! The problem was that I was using a translation inside a function that is called on the `boot` method in each AppServiceProvider; removing that line of code fixed the problem. – jceceniceros Oct 22 '19 at 20:30