Skip to content

Commit

Permalink
chore: Refactor NovaTranslationsServiceProvider to improve readabilit…
Browse files Browse the repository at this point in the history
…y and organization
  • Loading branch information
ferdiunal committed Sep 8, 2024
1 parent f5d5b6c commit a256506
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 44 deletions.
2 changes: 1 addition & 1 deletion config/nova-translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'patternA' => implode('', [
'[^\w]', // Must not start with any alphanum or _
'(?<!->)', // Must not start with ->
'(' . implode('|', [
'('.implode('|', [
'trans',
'trans_choice',
'Lang::get',
Expand Down
10 changes: 7 additions & 3 deletions src/Nova/TranslationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Ferdiunal\NovaTranslations\Nova;

use App\Nova\Resource;
use Ferdiunal\NovaTranslations\Models\Translation;
use Ferdiunal\NovaTranslations\TranslationField;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource;

class TranslationResource extends Resource
{
Expand All @@ -21,7 +21,11 @@ class TranslationResource extends Resource
public static $displayInNavigation = false;

public static $search = [
'namespace', 'group', 'key', 'text', 'meta',
'namespace',
'group',
'key',
'text',
'meta',
];

public function fields(NovaRequest $request)
Expand All @@ -46,6 +50,6 @@ public static function defaultOrderings($query)
{
$table = app(static::$model)->getTable();

return $query->reorder()->orderBy($table . '.namespace', 'asc')->orderBy($table . '.group', 'asc')->orderBy($table . '.key', 'asc');
return $query->reorder()->orderBy($table.'.namespace', 'asc')->orderBy($table.'.group', 'asc')->orderBy($table.'.key', 'asc');
}
}
141 changes: 106 additions & 35 deletions src/NovaTranslationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,122 @@ class NovaTranslationsServiceProvider extends ServiceProvider
*/
public function boot()
{
$resource = $this->app['config']->get('nova-translations.resource', TranslationResource::class);

if (!$this->app->configurationIsCached()) {
$this->mergeConfigFrom(__DIR__ . '/../config/nova-translations.php', 'nova-translations');
}
$this->registerConfig();
$this->registerResources();
$this->registerMigrations();
$this->registerCommands();

Nova::serving(function (ServingNova $event) {
$this->registerNovaResources();
$this->loadNovaTranslations();
});
}

if ($this->app->runningInConsole()) {
//Register Migrations
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
/**
* Register package configuration.
*
* @return void
*/
protected function registerConfig()
{
if (! $this->app->configurationIsCached()) {
$this->mergeConfigFrom(__DIR__.'/../config/nova-translations.php', 'nova-translations');
}
}

$this->publishes([
__DIR__ . '/../config/nova-translations.php' => config_path('nova-translations.php'),
], 'nova-translations-config');
/**
* Register Nova resources.
*
* @return void
*/
protected function registerResources()
{
$resource = $this->getConfiguredResource();
$this->app->booted(function () use ($resource) {
$this->setTranslationModel($resource);
$this->routes();
});
}

//Publish Migrations
$this->publishes([
__DIR__ . '/../database/migrations' => database_path('migrations'),
], 'nova-translations-migrations');
/**
* Register migrations for the package.
*
* @return void
*/
protected function registerMigrations()
{
if ($this->app->runningInConsole()) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

$this->app->booted(function () use (&$resource) {
$config = $this->app['config'];
$config->set(
'translation-loader.model',
$config->get('nova-translations.model')
);
$this->publishes([
__DIR__.'/../config/nova-translations.php' => config_path('nova-translations.php'),
], 'nova-translations-config');

$resource::$model = $config->get('nova-translations.model');
$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
], 'nova-translations-migrations');
}
}

/**
* Register console commands.
*
* @return void
*/
protected function registerCommands()
{
if ($this->app->runningInConsole()) {
$this->commands([
ImportScanCommand::class,
]);
}
}

$this->routes();
});
/**
* Register Nova-specific resources.
*
* @return void
*/
protected function registerNovaResources()
{
$resource = $this->getConfiguredResource();
Nova::resources([$resource]);
}

Nova::serving(function (ServingNova $event) use (&$resource) {
Nova::resources([
$resource,
]);
/**
* Load translations for Nova interface.
*
* @return void
*/
protected function loadNovaTranslations()
{
Nova::$translations = Translation::getTranslationsForGroup(
$this->app->getLocale(),
'*'
);
}

Nova::$translations = Translation::getTranslationsForGroup(
$this->app->getLocale(),
'*'
);
});
/**
* Set the translation model for the resource.
*
* @param string $resource
* @return void
*/
protected function setTranslationModel($resource)
{
$config = $this->app['config'];
$config->set('translation-loader.model', $config->get('nova-translations.model'));
$resource::$model = $config->get('nova-translations.model');
}

/**
* Get the configured Nova resource class.
*
* @return string
*/
protected function getConfiguredResource()
{
return $this->app['config']->get('nova-translations.resource', TranslationResource::class);
}

/**
Expand All @@ -81,11 +151,11 @@ protected function routes()
}

Nova::router(['nova', Authenticate::class, Authorize::class], 'nova-translations')
->group(__DIR__ . '/../routes/inertia.php');
->group(__DIR__.'/../routes/inertia.php');

Route::middleware(['nova', Authorize::class])
->prefix('nova-vendor/ferdiunal/nova-translations')
->group(__DIR__ . '/../routes/api.php');
->group(__DIR__.'/../routes/api.php');
}

/**
Expand All @@ -95,5 +165,6 @@ protected function routes()
*/
public function register()
{
// Register any application services or bindings here
}
}
10 changes: 5 additions & 5 deletions src/Services/SaveScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ protected function save(string $namespace, string $group, string $key, ?string $

$model = app(config('nova-translations.model'));

if (!is_a($model, Translation::class, true)) {
throw new \Exception('Model must be an instance of ' . Translation::class);
if (! is_a($model, Translation::class, true)) {
throw new \Exception('Model must be an instance of '.Translation::class);
}

foreach ($locals as $locale) {
Expand Down Expand Up @@ -102,12 +102,12 @@ protected function lang($key, $replace = null, $locale = null): string
}

$isTranslatable = (str($trans)->contains('::') || str($trans)->contains('_') ||
(!str($trans)->contains('::') &&
!str($trans)->endsWith('.') &&
(! str($trans)->contains('::') &&
! str($trans)->endsWith('.') &&
str($trans)->contains('.')));

if ($this->defaultLocale !== $locale) {
if ($this->translater instanceof Translator && !$isTranslatable) {
if ($this->translater instanceof Translator && ! $isTranslatable) {
$trans = $this->translater->run(
source: $this->defaultLocale,
target: $locale,
Expand Down

0 comments on commit a256506

Please sign in to comment.