mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-06 02:33:48 +00:00
- chunking translations query
- language translations fallback
This commit is contained in:
@@ -111,18 +111,15 @@ class LanguageController extends Controller
|
||||
|
||||
/**
|
||||
* Delete the language with all children strings
|
||||
*
|
||||
* @param Language $language
|
||||
* @return ResponseFactory|Response
|
||||
* @return Response
|
||||
*/
|
||||
public function delete_language(Language $language)
|
||||
public function delete_language(Language $language): Response
|
||||
{
|
||||
// Abort in demo mode
|
||||
abort_if(is_demo(), 204, 'Done.');
|
||||
|
||||
if ($language->locale === 'en') {
|
||||
abort(401, "Sorry, you can't delete default language.");
|
||||
}
|
||||
abort_if($language->locale === 'en', 401, "Sorry, you can't delete default language.");
|
||||
|
||||
// If user try to delete language used as default,
|
||||
// then set en language as default
|
||||
|
||||
@@ -14,8 +14,11 @@ use Illuminate\Support\Facades\Mail;
|
||||
use App\Http\Mail\SendContactMessage;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Doctrine\DBAL\Driver\PDOException;
|
||||
use Illuminate\Database\QueryException;
|
||||
use App\Http\Resources\PricingCollection;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Http\Requests\PublicPages\SendContactMessageRequest;
|
||||
|
||||
class AppFunctionsController extends Controller
|
||||
@@ -178,18 +181,22 @@ class AppFunctionsController extends Controller
|
||||
|
||||
/**
|
||||
* Get language translations for frontend app
|
||||
*
|
||||
* @param $lang
|
||||
* @return array
|
||||
*/
|
||||
public function get_translations($lang)
|
||||
{
|
||||
$translations = Cache::rememberForever("language-translations-$lang", function () use ($lang) {
|
||||
return Language::whereLocale($lang)
|
||||
->firstOrFail()
|
||||
->languageTranslations;
|
||||
});
|
||||
$translations = cache()
|
||||
->rememberForever("language-translations-$lang", function () use ($lang) {
|
||||
try {
|
||||
return Language::whereLocale($lang)
|
||||
->firstOrFail()
|
||||
->languageTranslations;
|
||||
} catch (QueryException | ModelNotFoundException $e) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
return map_language_translations($translations);
|
||||
return $translations
|
||||
? map_language_translations($translations)
|
||||
: get_default_language_translations();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
/**
|
||||
* Obfuscate email
|
||||
@@ -532,7 +533,7 @@ function get_file_type($file_mimetype)
|
||||
* @param $translations
|
||||
* @return mixed
|
||||
*/
|
||||
function map_language_translations($translations)
|
||||
function map_language_translations($translations): Collection
|
||||
{
|
||||
return $translations->map(function ($string) {
|
||||
return [$string->key => $string->value];
|
||||
@@ -594,7 +595,7 @@ function get_image_meta_data($file)
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
function get_default_language_translations()
|
||||
function get_default_language_translations(): Collection
|
||||
{
|
||||
return collect([
|
||||
config('language-translations.extended'),
|
||||
@@ -855,12 +856,12 @@ function set_time_by_user_timezone($time)
|
||||
|
||||
/**
|
||||
* Translate the given message.
|
||||
*
|
||||
* @param $key
|
||||
* @param null $values
|
||||
* @return string|string[]
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
function __t($key, $values = null)
|
||||
function __t($key, $values = null): string
|
||||
{
|
||||
// Get current locale
|
||||
$locale = cache()->rememberForever('language', function () {
|
||||
@@ -874,16 +875,14 @@ function __t($key, $values = null)
|
||||
// Get language strings
|
||||
$strings = cache()->rememberForever("language-translations-$locale", function () use ($locale) {
|
||||
try {
|
||||
return Language::whereLocale($locale)->first()->languageTranslations ?? get_default_language_translations();
|
||||
} catch (QueryException $e) {
|
||||
return get_default_language_translations();
|
||||
return Language::whereLocale($locale)->firstOrFail()->languageTranslations;
|
||||
} catch (QueryException | ModelNotFoundException $e) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}) ?? get_default_language_translations();
|
||||
|
||||
// Find the string by key
|
||||
$string = $strings->get($key)
|
||||
? $strings->get($key)
|
||||
: $strings->firstWhere('key', $key)->value;
|
||||
$string = $strings->firstWhere('key', $key)->value ?? $strings->get($key);
|
||||
|
||||
if ($values) {
|
||||
return replace_occurrence($string, collect($values));
|
||||
|
||||
@@ -88,7 +88,6 @@ class LanguageService
|
||||
$chunks = array_chunk($translations, 100);
|
||||
|
||||
foreach ($chunks as $chunk) {
|
||||
|
||||
// Store translations into database
|
||||
DB::table('language_translations')
|
||||
->insert($chunk);
|
||||
|
||||
@@ -248,18 +248,18 @@ class LanguageEditorTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
__t('actions.close'),
|
||||
'Close'
|
||||
'Close',
|
||||
__t('actions.close')
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
__t('shared_link_email_subject', ['user' => 'John']),
|
||||
'🙋 John share some files with you. Look at it!'
|
||||
'🙋 John share some files with you. Look at it!',
|
||||
__t('shared_link_email_subject', ['user' => 'John'])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
__t('test', ['name' => 'John', 'surname' => 'Doe']),
|
||||
'Hi, my name is John Doe'
|
||||
'Hi, my name is John Doe',
|
||||
__t('test', ['name' => 'John', 'surname' => 'Doe'])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,4 +7,9 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
use CreatesApplication;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user