Skip to content

Commit

Permalink
first commit 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Sep 4, 2022
0 parents commit d4da406
Show file tree
Hide file tree
Showing 43 changed files with 1,283 additions and 0 deletions.
Empty file added Config/.gitkeep
Empty file.
85 changes: 85 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

return [
"name" => "Translations",
/*
|--------------------------------------------------------------------------
| Paths
|--------------------------------------------------------------------------
|
| add path that will be show to the scaner to catch lanuages tags
|
*/
"paths" => [
app_path(),
resource_path('views'),
base_path('Modules')
],

/*
|--------------------------------------------------------------------------
| Locals
|--------------------------------------------------------------------------
|
| add the locals that will be show on the languages selector
|
*/
"locals" => [
"en" => "English",
"ar" => "Arabic"
],

/*
|--------------------------------------------------------------------------
| Show Switcher
|--------------------------------------------------------------------------
|
| show switcher item on the navigation menu
|
*/
"show-switcher" => true,

/*
|--------------------------------------------------------------------------
| Switcher
|--------------------------------------------------------------------------
|
| the lanuages of the switcher navigation item must be 2
|
*/
"switcher" => [
"ar",
"en",
],

/*
|--------------------------------------------------------------------------
| Switcher Item Option
|--------------------------------------------------------------------------
|
| custome switcher menu item
|
*/

"languages-switcher-menu" => [
"group" => "Translations",
"icon" => "heroicon-o-translate",
"sort" => 10,
"url" => 'admin/translations/change'
],

/*
|--------------------------------------------------------------------------
| Modal
|--------------------------------------------------------------------------
|
| use simple modal resource for the translation resource
|
*/
"modal" => true,

"auto" => true,

"google_key" => env('GOOGLE_API', null)

];
Empty file added Console/.gitkeep
Empty file.
72 changes: 72 additions & 0 deletions Console/InstallTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Modules\Translations\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Modules\Base\Helpers\Resources\VILT;
use Modules\Menu\Entities\Menus;
use Modules\Menu\Entities\MenusGroups;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class InstallTranslation extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'translations:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Install translations permissions to main admin role';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Install Permissions');
Artisan::call('roles:generate language_lines');
$this->info('Your Translations is ready now');

return Command::SUCCESS;
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [ ];
}
}
Empty file added Database/Migrations/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLanguageLinesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('language_lines', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('group');
$table->index('group');
$table->text('key');
$table->jsonb('text');
$table->jsonb('metadata')->nullable();
$table->string('namespace')->default('*');
$table->index('namespace');
$table->softDeletes();
$table->timestamps();
});

if (!Schema::hasColumn('users', 'lang')) {
Schema::table('users', function (Blueprint $table) {
$table->string('lang')->default('en');
});
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('language_lines');

if (Schema::hasColumn('users', 'lang')) {
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('lang');
});
}
}
}
Empty file added Database/Seeders/.gitkeep
Empty file.
21 changes: 21 additions & 0 deletions Database/Seeders/TranslationsDatabaseSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Modules\Translations\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class TranslationsDatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();

// $this->call("OthersTableSeeder");
}
}
Empty file added Database/factories/.gitkeep
Empty file.
Empty file added Entities/.gitkeep
Empty file.
59 changes: 59 additions & 0 deletions Entities/Translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Modules\Translations\Entities;

use Spatie\TranslationLoader\LanguageLine;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;



class Translation extends LanguageLine
{
use HasFactory;
use SoftDeletes;

public $translatable = ['text'];

/** @var array */
public $guarded = ['id'];

/** @var array */
protected $casts = ['text' => 'array'];

protected $table = "language_lines";

protected $fillable = [
"group",
"key",
"text"
];


public static function getTranslatableLocales(): array
{
return config('filament-translations.locals');
}

public function getTranslation(string $locale, string $group = null): string
{
if ($group === '*' && !isset($this->text[$locale])) {
$fallback = config('app.fallback_locale');

return $this->text[$fallback] ?? $this->key;
}
return $this->text[$locale] ?? '';
}

public function setTranslation(string $locale, string $value): self
{
$this->text = array_merge($this->text ?? [], [$locale => $value]);

return $this;
}

protected function getTranslatedLocales(): array
{
return array_keys($this->text);
}
}
45 changes: 45 additions & 0 deletions Exports/TranslationsExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Modules\Translations\Exports;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Modules\Translations\Entities\Translation;
use Maatwebsite\Excel\Concerns\FromCollection;

class TranslationsExport implements FromCollection, WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
$transaltions = Translation::all();

foreach ($transaltions as $item) {
foreach ($item->text as $key => $lang) {
$item->{$key} = $lang;
}
unset($item->group);
unset($item->metadata);
unset($item->namespace);
unset($item->text);
unset($item->created_at);
unset($item->updated_at);
unset($item->deleted_at);
}

return $transaltions;
}

public function headings(): array
{

$loadLocals = [];
$getLocals = config('translations.locals');
foreach ($getLocals as $key => $value) {
array_push($loadLocals, $key);
}

return array_merge(["id", "key"], $loadLocals);
}
}
Empty file added Http/Controllers/.gitkeep
Empty file.
Empty file added Http/Middleware/.gitkeep
Empty file.
18 changes: 18 additions & 0 deletions Http/Middleware/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Modules\Translations\Http\Middleware;

use Closure;

class Language
{
public function handle($request, Closure $next)
{
if (in_array($request->header('Language'), ['ar', 'en'])) {
app()->setLocale($request->header('Language'));
} else {
app()->setLocale('ar');
}
return $next($request);
}
}
30 changes: 30 additions & 0 deletions Http/Middleware/LanguageMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Modules\Translations\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;


class LanguageMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if ($request->user()) {
if (Cookie::get('lang')) {
app()->setLocale(Cookie::get('lang'));
} else {
app()->setLocale('en');
}
}
return $next($request);
}
}
Empty file added Http/Requests/.gitkeep
Empty file.
Loading

0 comments on commit d4da406

Please sign in to comment.