dns_nx
dns_nx

Reputation: 3943

Laravel - Read all available languages, but all translations are German

I have a Service Provider in laravel which reads all available languages from locale and save them in a Cache file. Everything works fine, but all translations are in German. I expect to get them in language de and en. This is the code from the ServiceProvider:

$minutes = 24 * 60;
Cache::remember('translations', $minutes, function () {
    $directories = array('de', 'en');
    $collection = new \stdClass;
    foreach ($directories as $directory) {
        $path = resource_path('lang/' . $directory);
        $allTranslations = collect(File::allFiles($path))->flatMap(function ($file, $directory) {
            return [
                ($translation = $file->getBasename('.php')) => trans($translation, array(), null, $directory),
            ];
        });
        $allModuleTranslations = collect(File::allFiles($path . '/modul'))->flatMap(function ($file, $directory) {
            return [
                ($translation = $file->getBasename('.php')) => trans('modul/' . $translation, array(), null, $directory),
            ];
        });
        $collection->$directory = $allTranslations->merge($allModuleTranslations);
    }
    return json_encode($collection);
});

Can anybody see, why it returns all words in German?

The result looks like this:

{
   de:
      auth:
         failed: "Diese Zugangsdaten wurden nicht in unserer Datenbank gefunden."
         throttle: "Zu viele Login Versuche. Versuchen Sie es bitte in :seconds Sekunden."
   en:
      auth:
         failed: "Diese Zugangsdaten wurden nicht in unserer Datenbank gefunden."
         throttle: "Zu viele Login Versuche. Versuchen Sie es bitte in :seconds Sekunden."
}

Upvotes: 1

Views: 847

Answers (1)

Remul
Remul

Reputation: 8252

  1. You have to import the $directory variable from the parent scope with use, it will not be passed with the flatMap() callback

  2. trans only has 3 parameters but you are passing 4 arguments

    mixed trans(string $key, array $replace = [], string|null $locale = null)

This should work:

$minutes = 24 * 60;
Cache::remember('translations', $minutes, function () {
    $directories = array('de', 'en');
    $collection = new \stdClass;
    foreach ($directories as $directory) {
        $path = resource_path('lang/' . $directory);
        $allTranslations = collect(File::allFiles($path))
            ->flatMap(function ($file) use ($directory) {
                return [
                    ($translation = $file->getBasename('.php')) => trans($translation, array(), $directory),
                ];
            });
        $allModuleTranslations = collect(File::allFiles($path . '/modul'))
            ->flatMap(function ($file) use ($directory) {
                return [
                    ($translation = $file->getBasename('.php')) => trans('modul/' . $translation, array(), $directory),
                ];
            });
        $collection->$directory = $allTranslations->merge($allModuleTranslations);
    }
    return json_encode($collection);
});

Upvotes: 1

Related Questions