FleetCart/Modules/Translation/TranslationLoader.php

127 lines
3.2 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Translation;
use Illuminate\Support\Facades\Cache;
use Illuminate\Translation\FileLoader;
use Modules\Translation\Entities\Translation;
class TranslationLoader extends FileLoader
{
/**
* Get translation file paths.
*
* @return array
*/
public function paths()
{
return array_merge([$this->path], $this->hints);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Load the messages for the given locale.
*
* @param string $locale
* @param string $group
* @param string $namespace
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return array
*/
public function load($locale, $group, $namespace = null)
{
2023-12-03 14:07:47 +00:00
if (!config('app.cache')) {
2023-06-11 12:14:03 +00:00
return $this->getTranslations($locale, $group, $namespace);
}
return Cache::tags('translations')
->rememberForever(md5("translation_loader.{$locale}.{$group}.{$namespace}"), function () use ($locale, $group, $namespace) {
return $this->getTranslations($locale, $group, $namespace);
});
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Get file and database translations.
*
* @param string $locale
* @param string $group
* @param string $namespace
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return array
*/
private function getTranslations($locale, $group, $namespace)
{
$databaseTranslations = [];
if (config('app.installed')) {
$databaseTranslations = $this->databaseTranslations($locale, $group, $namespace);
}
return array_replace_recursive(
$this->fileTranslations($locale, $group, $namespace),
$this->breakDot($databaseTranslations)
);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
2023-12-03 14:07:47 +00:00
* Get database translations.
*
* @param string $locale
* @param string $group
* @param string $namespace
2023-06-11 12:14:03 +00:00
*
* @return array
*/
2023-12-03 14:07:47 +00:00
private function databaseTranslations($locale, $group, $namespace)
2023-06-11 12:14:03 +00:00
{
2023-12-03 14:07:47 +00:00
return Translation::where('key', 'LIKE', "{$namespace}::{$group}.%")
->whereHas('translations', function ($query) use ($locale) {
$query->where('locale', $locale);
})->get()->mapWithKeys(function ($translation) use ($namespace, $group) {
$key = str_replace("{$namespace}::{$group}.", '', $translation->key);
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
return [$key => $translation->value];
})->all();
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Get file translations.
*
* @param string $locale
* @param string $group
* @param string $namespace
2023-12-03 14:07:47 +00:00
*
2023-06-11 12:14:03 +00:00
* @return array
*/
private function fileTranslations($locale, $group, $namespace)
{
return parent::load($locale, $group, $namespace);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
2023-12-03 14:07:47 +00:00
* Break flattened dot translations to an array.
*
* @param array $translations
2023-06-11 12:14:03 +00:00
*
* @return array
*/
2023-12-03 14:07:47 +00:00
private function breakDot($translations)
2023-06-11 12:14:03 +00:00
{
2023-12-03 14:07:47 +00:00
$array = [];
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
foreach ($translations as $key => $value) {
if (strpos($key, '*') === false) {
array_set($array, $key, $value);
} else {
$array[$key] = $value;
}
}
return $array;
2023-06-11 12:14:03 +00:00
}
}