- chunking translations query

- language translations fallback
This commit is contained in:
Peter Papp
2021-06-07 17:42:53 +02:00
parent 847221d385
commit b50e0041e0
6 changed files with 41 additions and 34 deletions

View File

@@ -111,18 +111,15 @@ class LanguageController extends Controller
/** /**
* Delete the language with all children strings * Delete the language with all children strings
*
* @param Language $language * @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 in demo mode
abort_if(is_demo(), 204, 'Done.'); abort_if(is_demo(), 204, 'Done.');
if ($language->locale === 'en') { abort_if($language->locale === 'en', 401, "Sorry, you can't delete default language.");
abort(401, "Sorry, you can't delete default language.");
}
// If user try to delete language used as default, // If user try to delete language used as default,
// then set en language as default // then set en language as default

View File

@@ -14,8 +14,11 @@ use Illuminate\Support\Facades\Mail;
use App\Http\Mail\SendContactMessage; use App\Http\Mail\SendContactMessage;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Driver\PDOException;
use Illuminate\Database\QueryException;
use App\Http\Resources\PricingCollection; use App\Http\Resources\PricingCollection;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Contracts\Routing\ResponseFactory; use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Requests\PublicPages\SendContactMessageRequest; use App\Http\Requests\PublicPages\SendContactMessageRequest;
class AppFunctionsController extends Controller class AppFunctionsController extends Controller
@@ -178,18 +181,22 @@ class AppFunctionsController extends Controller
/** /**
* Get language translations for frontend app * Get language translations for frontend app
*
* @param $lang
* @return array
*/ */
public function get_translations($lang) public function get_translations($lang)
{ {
$translations = Cache::rememberForever("language-translations-$lang", function () use ($lang) { $translations = cache()
return Language::whereLocale($lang) ->rememberForever("language-translations-$lang", function () use ($lang) {
->firstOrFail() try {
->languageTranslations; 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();
} }
} }

View File

@@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image; use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Database\Eloquent\ModelNotFoundException;
/** /**
* Obfuscate email * Obfuscate email
@@ -532,7 +533,7 @@ function get_file_type($file_mimetype)
* @param $translations * @param $translations
* @return mixed * @return mixed
*/ */
function map_language_translations($translations) function map_language_translations($translations): Collection
{ {
return $translations->map(function ($string) { return $translations->map(function ($string) {
return [$string->key => $string->value]; return [$string->key => $string->value];
@@ -594,7 +595,7 @@ function get_image_meta_data($file)
/** /**
* @return Collection * @return Collection
*/ */
function get_default_language_translations() function get_default_language_translations(): Collection
{ {
return collect([ return collect([
config('language-translations.extended'), config('language-translations.extended'),
@@ -855,12 +856,12 @@ function set_time_by_user_timezone($time)
/** /**
* Translate the given message. * Translate the given message.
*
* @param $key * @param $key
* @param null $values * @param null $values
* @return string|string[] * @return string
* @throws Exception
*/ */
function __t($key, $values = null) function __t($key, $values = null): string
{ {
// Get current locale // Get current locale
$locale = cache()->rememberForever('language', function () { $locale = cache()->rememberForever('language', function () {
@@ -874,16 +875,14 @@ function __t($key, $values = null)
// Get language strings // Get language strings
$strings = cache()->rememberForever("language-translations-$locale", function () use ($locale) { $strings = cache()->rememberForever("language-translations-$locale", function () use ($locale) {
try { try {
return Language::whereLocale($locale)->first()->languageTranslations ?? get_default_language_translations(); return Language::whereLocale($locale)->firstOrFail()->languageTranslations;
} catch (QueryException $e) { } catch (QueryException | ModelNotFoundException $e) {
return get_default_language_translations(); return null;
} }
}); }) ?? get_default_language_translations();
// Find the string by key // Find the string by key
$string = $strings->get($key) $string = $strings->firstWhere('key', $key)->value ?? $strings->get($key);
? $strings->get($key)
: $strings->firstWhere('key', $key)->value;
if ($values) { if ($values) {
return replace_occurrence($string, collect($values)); return replace_occurrence($string, collect($values));

View File

@@ -88,7 +88,6 @@ class LanguageService
$chunks = array_chunk($translations, 100); $chunks = array_chunk($translations, 100);
foreach ($chunks as $chunk) { foreach ($chunks as $chunk) {
// Store translations into database // Store translations into database
DB::table('language_translations') DB::table('language_translations')
->insert($chunk); ->insert($chunk);

View File

@@ -248,18 +248,18 @@ class LanguageEditorTest extends TestCase
]); ]);
$this->assertEquals( $this->assertEquals(
__t('actions.close'), 'Close',
'Close' __t('actions.close')
); );
$this->assertEquals( $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( $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'])
); );
} }

View File

@@ -7,4 +7,9 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase abstract class TestCase extends BaseTestCase
{ {
use CreatesApplication; use CreatesApplication;
public function setUp(): void
{
parent::setUp();
}
} }