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.