Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
afsakar committed Oct 10, 2024
1 parent caf235d commit 26633b7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 28 deletions.
1 change: 1 addition & 0 deletions .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"pest_2.35.1","defects":[],"times":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.068,"P\\Tests\\ExampleTest::__pest_evaluable_it_can_test":0.001}}
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ parameters:
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/tr/filament-translate-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
'success_message' => 'İçerik başarıyla çevrildi.',
'error_title' => 'Hata!',
'error_message' => 'İçerik çevrilemedi.',
'original_content' => 'Orijinal İçerik',
'translated_content' => 'Çevrilen İçerik',
];
40 changes: 26 additions & 14 deletions src/Actions/TranslatableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
use Afsakar\FilamentTranslateAction\Helpers\GoogleTranslate;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Illuminate\Support\HtmlString;

class TranslatableAction
{
public static function make(): void
{
Field::macro('translatable', function () {
// @phpstan-ignore-next-line
return $this->hintAction(
function (Field $component) {
return Action::make('google_translate')
->icon('heroicon-o-language')
->label(__('filament-translate-action::filament-translate-action.modal_title'))
->slideOver()
->hidden(function () use ($component) {
return $component->getState() === null;
})
->form([
Select::make('source')
->label(__('filament-translate-action::filament-translate-action.source'))
Expand All @@ -30,28 +38,32 @@ function (Field $component) {
->options(fn ($get) => collect(getLangs())
->filter(fn ($locale, $key) => $key != $get('source'))
->toArray())
->reactive()
->searchable(),
Placeholder::make('preview')
->label(__('filament-translate-action::filament-translate-action.original_content'))
->content(fn () => new HtmlString($component->getState()))
->disabled(),
Group::make()
->visible(fn ($get) => $get('source') && $get('target'))
->schema([
Placeholder::make('translated')
->label(__('filament-translate-action::filament-translate-action.translated_content'))
->content(function ($get) use ($component) {
$translated_text = translate_text($component, $get('source'), $get('target'), $component->getState());

return new HtmlString($translated_text);
}),
]),
])
->modalSubmitActionLabel(__('filament-translate-action::filament-translate-action.translate'))
->action(function (array $data, $livewire) use ($component) {
$googleTranslate = new GoogleTranslate();
$googleTranslate = new GoogleTranslate;

$source = $data['source'] ?: (string) config('app.locale');

$text = null;

if (class_exists('FilamentTiptapEditor\TiptapEditor') && $component instanceof \FilamentTiptapEditor\TiptapEditor) {
match (data_get($component->getOutput(), 'value')) {
'json' => $text = tiptap_converter()->asJSON($component->getState()),
'text' => $text = tiptap_converter()->asText($component->getState()),
default => $text = tiptap_converter()->asHTML($component->getState()),
};
} else {
$text = $component->getState();
}

try {
$googleTranslate = $googleTranslate->translate($source, $data['target'], $text);
$googleTranslate = translate_text($component, $source, $data['target'], $component->getState());
$component->state($googleTranslate);

$livewire->dispatch('refresh-tiptap-editors', [
Expand Down
4 changes: 1 addition & 3 deletions src/FilamentTranslateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Afsakar\FilamentTranslateAction;

class FilamentTranslateAction
{
}
class FilamentTranslateAction {}
38 changes: 38 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Afsakar\FilamentTranslateAction\Helpers\GoogleTranslate;
use Filament\Forms\Components\Field;
use Filament\Notifications\Notification;

if (! function_exists('getLangs')) {
function getLangs()
{
Expand All @@ -9,3 +13,37 @@ function getLangs()
};
}
}

if (! function_exists('translate_text')) {
function translate_text(Field $component, string $source, string $target, string $text): string
{
$googleTranslate = new GoogleTranslate;

try {
$text = null;

if (class_exists('FilamentTiptapEditor\TiptapEditor') && $component instanceof \FilamentTiptapEditor\TiptapEditor) {
match (data_get($component->getOutput(), 'value')) {
'json' => $text = tiptap_converter()->asJSON($component->getState()), // @phpstan-ignore-line
'text' => $text = tiptap_converter()->asText($component->getState()), // @phpstan-ignore-line
default => $text = tiptap_converter()->asHTML($component->getState()), // @phpstan-ignore-line
};
} else {
$text = $component->getState();
}

$googleTranslate = $googleTranslate->translate($source, $target, $text);

return $googleTranslate;

} catch (\Exception $exception) {
Notification::make()
->title(__('filament-translate-action::filament-translate-action.error_title'))
->body(__('filament-translate-action::filament-translate-action.error_message') . '<br/>' . $exception->getMessage())
->danger()
->send();

return $component->getState();
}
}
}
10 changes: 0 additions & 10 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
use BladeUI\Icons\BladeIconsServiceProvider;
use Filament\Actions\ActionsServiceProvider;
use Filament\FilamentServiceProvider;
use Filament\Forms\FormsServiceProvider;
use Filament\Infolists\InfolistsServiceProvider;
use Filament\Notifications\NotificationsServiceProvider;
use Filament\SpatieLaravelSettingsPluginServiceProvider;
use Filament\SpatieLaravelTranslatablePluginServiceProvider;
use Filament\Support\SupportServiceProvider;
use Filament\Tables\TablesServiceProvider;
use Filament\Widgets\WidgetsServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
Expand All @@ -38,16 +33,11 @@ protected function getPackageProviders($app)
BladeCaptureDirectiveServiceProvider::class,
BladeHeroiconsServiceProvider::class,
BladeIconsServiceProvider::class,
FilamentServiceProvider::class,
FormsServiceProvider::class,
InfolistsServiceProvider::class,
LivewireServiceProvider::class,
NotificationsServiceProvider::class,
SpatieLaravelSettingsPluginServiceProvider::class,
SpatieLaravelTranslatablePluginServiceProvider::class,
SupportServiceProvider::class,
TablesServiceProvider::class,
WidgetsServiceProvider::class,
FilamentTranslateActionServiceProvider::class,
];
}
Expand Down

0 comments on commit 26633b7

Please sign in to comment.