¨4.0.1¨
This commit is contained in:
@@ -8,6 +8,7 @@ class CreateTranslationsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
@@ -19,8 +20,10 @@ class CreateTranslationsTable extends Migration
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
|
||||
@@ -24,6 +24,7 @@ class CreateTranslationTranslationsTable extends Migration
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
|
||||
@@ -31,6 +31,7 @@ class Translation extends Model
|
||||
*/
|
||||
protected $translatedAttributes = ['value'];
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve all translations.
|
||||
*
|
||||
@@ -38,7 +39,7 @@ class Translation extends Model
|
||||
*/
|
||||
public static function retrieve()
|
||||
{
|
||||
if (! config('app.cache')) {
|
||||
if (!config('app.cache')) {
|
||||
return self::getTranslations();
|
||||
}
|
||||
|
||||
@@ -47,10 +48,6 @@ class Translation extends Model
|
||||
});
|
||||
}
|
||||
|
||||
protected static function getTranslations()
|
||||
{
|
||||
return array_replace_recursive(static::getFileTranslations(), static::getDatabaseTranslations());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file translations.
|
||||
@@ -76,6 +73,7 @@ class Translation extends Model
|
||||
return $translations;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get database translations.
|
||||
*
|
||||
@@ -93,4 +91,10 @@ class Translation extends Model
|
||||
|
||||
return $translations;
|
||||
}
|
||||
|
||||
|
||||
protected static function getTranslations()
|
||||
{
|
||||
return array_replace_recursive(static::getFileTranslations(), static::getDatabaseTranslations());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,18 @@ class TranslationTranslation extends Model
|
||||
*/
|
||||
protected $fillable = ['locale', 'value'];
|
||||
|
||||
|
||||
/**
|
||||
* Clear translations cache.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function clearCache()
|
||||
{
|
||||
Cache::tags('translations')->flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
@@ -32,14 +44,4 @@ class TranslationTranslation extends Model
|
||||
$translationTranslation->clearCache();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear translations cache.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function clearCache()
|
||||
{
|
||||
Cache::tags('translations')->flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Modules\Translation\Http\Controllers\Admin;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Modules\Translation\Entities\Translation;
|
||||
|
||||
class TranslationController
|
||||
@@ -9,7 +10,7 @@ class TranslationController
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
@@ -18,11 +19,13 @@ class TranslationController
|
||||
return view('translation::admin.translations.index', compact('translations'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param string $key
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function update($key)
|
||||
{
|
||||
@@ -33,6 +36,6 @@ class TranslationController
|
||||
['value' => request('value', '')]
|
||||
);
|
||||
|
||||
return trans('admin::messages.resource_saved', ['resource' => trans('translation::translations.translation')]);
|
||||
return trans('admin::messages.resource_updated', ['resource' => trans('translation::translations.translation')]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,12 @@ namespace Modules\Translation\Providers;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use Astrotomic\Translatable\Locales;
|
||||
use Modules\Support\Traits\AddsAsset;
|
||||
use Illuminate\Translation\Translator;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\Translation\TranslationLoader;
|
||||
|
||||
class TranslationServiceProvider extends ServiceProvider
|
||||
{
|
||||
use AddsAsset;
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*
|
||||
@@ -20,27 +17,15 @@ class TranslationServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if (! config('app.installed')) {
|
||||
if (!config('app.installed')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Carbon::setLocale(locale());
|
||||
|
||||
$this->setupTranslatable();
|
||||
|
||||
$this->addAdminAssets('admin.translations.index', ['admin.translation.css', 'admin.translation.js']);
|
||||
}
|
||||
|
||||
private function setupTranslatable()
|
||||
{
|
||||
$this->app['config']->set('translatable.use_fallback', true);
|
||||
$this->app['config']->set('translatable.fallback_locale', setting('default_locale'));
|
||||
$this->app['config']->set('translatable.locales', supported_locale_keys());
|
||||
|
||||
// Re-register translatable locales helper after overriding config.
|
||||
$this->app->singleton('translatable.locales', Locales::class);
|
||||
$this->app->singleton(Locales::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
@@ -54,6 +39,19 @@ class TranslationServiceProvider extends ServiceProvider
|
||||
$this->registerTranslator();
|
||||
}
|
||||
|
||||
|
||||
private function setupTranslatable()
|
||||
{
|
||||
$this->app['config']->set('translatable.use_fallback', true);
|
||||
$this->app['config']->set('translatable.fallback_locale', setting('default_locale'));
|
||||
$this->app['config']->set('translatable.locales', supported_locale_keys());
|
||||
|
||||
// Re-register translatable locales helper after overriding config.
|
||||
$this->app->singleton('translatable.locales', Locales::class);
|
||||
$this->app->singleton(Locales::class);
|
||||
}
|
||||
|
||||
|
||||
private function registerLoader()
|
||||
{
|
||||
$this->app->singleton('translation.loader', function ($app) {
|
||||
@@ -61,6 +59,7 @@ class TranslationServiceProvider extends ServiceProvider
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function registerTranslator()
|
||||
{
|
||||
$this->app->singleton('translator', function ($app) {
|
||||
|
||||
@@ -42,3 +42,9 @@
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('globals')
|
||||
@vite([
|
||||
'Modules/Translation/Resources/assets/admin/js/main.js',
|
||||
])
|
||||
@endpush
|
||||
|
||||
@@ -18,17 +18,19 @@ class TranslationLoader extends FileLoader
|
||||
return array_merge([$this->path], $this->hints);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load the messages for the given locale.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $group
|
||||
* @param string $namespace
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function load($locale, $group, $namespace = null)
|
||||
{
|
||||
if (! config('app.cache')) {
|
||||
if (!config('app.cache')) {
|
||||
return $this->getTranslations($locale, $group, $namespace);
|
||||
}
|
||||
|
||||
@@ -38,12 +40,14 @@ class TranslationLoader extends FileLoader
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get file and database translations.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $group
|
||||
* @param string $namespace
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getTranslations($locale, $group, $namespace)
|
||||
@@ -60,10 +64,49 @@ class TranslationLoader extends FileLoader
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get database translations.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $group
|
||||
* @param string $namespace
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function databaseTranslations($locale, $group, $namespace)
|
||||
{
|
||||
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);
|
||||
|
||||
return [$key => $translation->value];
|
||||
})->all();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get file translations.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $group
|
||||
* @param string $namespace
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function fileTranslations($locale, $group, $namespace)
|
||||
{
|
||||
return parent::load($locale, $group, $namespace);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Break flattened dot translations to an array.
|
||||
*
|
||||
* @param array $translations
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function breakDot($translations)
|
||||
@@ -80,37 +123,4 @@ class TranslationLoader extends FileLoader
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file translations.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $group
|
||||
* @param string $namespace
|
||||
* @return array
|
||||
*/
|
||||
private function fileTranslations($locale, $group, $namespace)
|
||||
{
|
||||
return parent::load($locale, $group, $namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get database translations.
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $group
|
||||
* @param string $namespace
|
||||
* @return array
|
||||
*/
|
||||
private function databaseTranslations($locale, $group, $namespace)
|
||||
{
|
||||
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);
|
||||
|
||||
return [$key => $translation->value];
|
||||
})->all();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user