Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationServiceProvider
95.24% covered (success)
95.24%
20 / 21
75.00% covered (warning)
75.00%
3 / 4
9
0.00% covered (danger)
0.00%
0 / 1
 boot
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 collectLocalesStrings
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 phpTranslations
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 jsonTranslations
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
1<?php
2
3namespace App\Providers;
4
5use Illuminate\Support\Collection;
6use Illuminate\Support\Facades\File;
7use Illuminate\Support\Facades\Cache;
8use Illuminate\Support\ServiceProvider;
9use Symfony\Component\Finder\SplFileInfo;
10
11class TranslationServiceProvider extends ServiceProvider
12{
13    /**
14     * Bootstrap the application services.
15     *
16     * @return void
17     */
18    public function boot(): void
19    {
20        foreach ($this->collectLocalesStrings() as $locale) {
21            // * Suported locales
22            Cache::rememberForever(sprintf('translations.%s', $locale), function () use ($locale) {
23                $translations = [
24                    'php' => $this->phpTranslations($locale),
25                    'json' => $this->jsonTranslations($locale),
26                ];
27                return $translations;
28            });
29        }
30    }
31
32    /**
33     * Gather provided langs checking from files
34     *
35     * @return \Illuminate\Support\Collection<string, string>
36     */
37    private function collectLocalesStrings(): Collection
38    {
39        return collect(File::allFiles(resource_path('lang/')))->flatMap(function (SplFileInfo $file) {
40            if ($file->getRelativePath() and \strpos($file->getRelativePath(), 'vendor') !== 0) {
41                return [$file->getRelativePath() => ''];
42            }
43        })->keys();
44    }
45
46    /**
47     * Gather all php translations
48     *
49     * @param string $locale
50     * @return array<string, string>
51     */
52    private function phpTranslations(string $locale): array
53    {
54        $path = resource_path("lang/$locale");
55
56        return collect(File::allFiles($path))->flatMap(function ($file) use ($locale) {
57            $translation = $file->getBasename('.php');
58            return [$translation => trans($translation, [], $locale)];
59        })->all();
60    }
61
62    /**
63     * Gather json translations
64     *
65     * @param string $locale
66     * @return array<string, string>
67     */
68    private function jsonTranslations(string $locale): array
69    {
70        $path = resource_path("lang/$locale.json");
71
72        if (is_string($path) && is_readable($path)) {
73            return json_decode(file_get_contents($path), true) ?? [];
74        }
75
76        return [];
77    }
78}