Skip to content

Commit a534160

Browse files
committed
add translation.loader
1 parent 7558a64 commit a534160

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

src/LaravelTranslationsApiServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ public function configurePackage(Package $package): void
4343
});
4444
});
4545
}
46+
// registerLoader
47+
public function registeringPackage()
48+
{
49+
$this->app->singleton('translation.loader', function ($app) {
50+
$class = TranslationLoaderManager::class;
51+
52+
return new $class($app['files'], $app['path.lang']);
53+
});
54+
}
4655
}

src/Models/Phrase.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Support\Facades\Cache;
89
use Samehdoush\LaravelTranslationsApi\Models\Concerns\HasDatabaseConnection;
910
use Samehdoush\LaravelTranslationsApi\Models\Concerns\HasUuid;
11+
use Illuminate\Support\Arr;
1012

1113
class Phrase extends Model
1214
{
@@ -25,7 +27,17 @@ class Phrase extends Model
2527
protected $with = [
2628
'source',
2729
];
30+
public static function boot()
31+
{
32+
parent::boot();
33+
34+
$flushGroupCache = function (self $languageLine) {
35+
$languageLine->flushGroupCache();
36+
};
2837

38+
static::saved($flushGroupCache);
39+
static::deleted($flushGroupCache);
40+
}
2941
public function file(): BelongsTo
3042
{
3143
return $this->belongsTo(TranslationFile::class, 'translation_file_id');
@@ -40,4 +52,56 @@ public function translation(): BelongsTo
4052
{
4153
return $this->belongsTo(Translation::class);
4254
}
55+
56+
public static function getTranslationsForGroup(string $locale, string $group): array
57+
{
58+
return Cache::rememberForever(static::getCacheKey($group, $locale), function () use ($group, $locale) {
59+
return static::query()
60+
->where('group', $group)
61+
->get()
62+
->reduce(function ($lines, self $languageLine) use ($group, $locale) {
63+
// $translation = $languageLine->getTranslation($locale);
64+
$translation = $languageLine->value;
65+
66+
if ($translation !== null && $group === '*') {
67+
// Make a flat array when returning json translations
68+
$lines[$languageLine->key] = $translation;
69+
} elseif ($translation !== null && $group !== '*') {
70+
// Make a nested array when returning normal translations
71+
Arr::set($lines, $languageLine->key, $translation);
72+
}
73+
74+
return $lines;
75+
}) ?? [];
76+
});
77+
}
78+
79+
// public function getTranslation(string $locale): ?string
80+
// {
81+
// if (!isset($this->text[$locale])) {
82+
// $fallback = config('app.fallback_locale');
83+
84+
// return $this->text[$fallback] ?? null;
85+
// }
86+
87+
// return $this->text[$locale];
88+
// }
89+
public static function getCacheKey(string $group, string $locale): string
90+
{
91+
return "samehdoush.translation-loader.{$group}.{$locale}";
92+
}
93+
public function flushGroupCache()
94+
{
95+
foreach ($this->getTranslatedLocales() as $locale) {
96+
Cache::forget(static::getCacheKey($this->group, $locale));
97+
}
98+
}
99+
100+
protected function getTranslatedLocales(): array
101+
{
102+
return Translation::with('language')
103+
->get()
104+
->pluck('language.code')
105+
->toArray();
106+
}
43107
}

src/TranslationLoaderManager.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Samehdoush\LaravelTranslationsApi;
4+
5+
use Illuminate\Database\QueryException;
6+
use Illuminate\Translation\FileLoader;
7+
use Illuminate\Support\Facades\Schema;
8+
use Samehdoush\LaravelTranslationsApi\Models\Phrase;
9+
use Samehdoush\LaravelTranslationsApi\TranslationLoaders\Db;
10+
use Samehdoush\LaravelTranslationsApi\TranslationLoaders\TranslationLoader;
11+
12+
class TranslationLoaderManager extends FileLoader
13+
{
14+
/**
15+
* Load the messages for the given locale.
16+
*
17+
* @param string $locale
18+
* @param string $group
19+
* @param string $namespace
20+
*
21+
* @return array
22+
*/
23+
public function load($locale, $group, $namespace = null): array
24+
{
25+
try {
26+
$fileTranslations = parent::load($locale, $group, $namespace);
27+
28+
if (!is_null($namespace) && $namespace !== '*') {
29+
return $fileTranslations;
30+
}
31+
32+
$loaderTranslations = $this->getTranslationsForTranslationLoaders($locale, $group, $namespace);
33+
34+
return array_replace_recursive($fileTranslations, $loaderTranslations);
35+
} catch (QueryException $e) {
36+
$modelClass = Phrase::class;
37+
$model = new $modelClass;
38+
if (is_a($model, Phrase::class)) {
39+
if (!Schema::hasTable($model->getTable())) {
40+
return parent::load($locale, $group, $namespace);
41+
}
42+
}
43+
44+
throw $e;
45+
};
46+
}
47+
48+
protected function getTranslationsForTranslationLoaders(
49+
string $locale,
50+
string $group,
51+
string $namespace = null
52+
): array {
53+
return collect(Db::class)
54+
->map(function (string $className) {
55+
return app($className);
56+
})
57+
->mapWithKeys(function (TranslationLoader $translationLoader) use ($locale, $group, $namespace) {
58+
return $translationLoader->loadTranslations($locale, $group, $namespace);
59+
})
60+
->toArray();
61+
}
62+
}

src/TranslationLoaders/Db.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Samehdoush\LaravelTranslationsApi\TranslationLoaders;
4+
5+
use Samehdoush\LaravelTranslationsApi\Models\Phrase;
6+
7+
8+
class Db implements TranslationLoader
9+
{
10+
public function loadTranslations(string $locale, string $group): array
11+
{
12+
13+
14+
return Phrase::getTranslationsForGroup($locale, $group);
15+
}
16+
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Samehdoush\LaravelTranslationsApi\TranslationLoaders;
4+
5+
interface TranslationLoader
6+
{
7+
/**
8+
* Returns all translations for the given locale and group.
9+
*
10+
* @param string $locale
11+
* @param string $group
12+
*
13+
* @return array
14+
*/
15+
public function loadTranslations(string $locale, string $group): array;
16+
}

0 commit comments

Comments
 (0)