Skip to content

Instantly share code, notes, and snippets.

@egyjs
Created February 2, 2025 15:21
Show Gist options
  • Save egyjs/3bb2e05f4bcecc3b39543293aac92b94 to your computer and use it in GitHub Desktop.
Save egyjs/3bb2e05f4bcecc3b39543293aac92b94 to your computer and use it in GitHub Desktop.
Laravel Import translations from lang files to database
<?php
// Laravel/app/Console/Commands/TranslationImport.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
class TranslationImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:translation-import';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import translations from lang files to database';
/**
* Execute the console command.
*/
public function handle()
{
$languages = ['en', 'ar'];
$groups = ['enums'];
foreach ($languages as $language) {
foreach ($groups as $group) {
$path = resource_path("lang/{$language}/{$group}.php");
$translations = require $path;
$translations = Arr::dot($translations); // Flatten the array: ['user.role.admin' => 'Administrator', ...]
foreach ($translations as $key => $value) {
$translation = \App\Models\Translation::firstOrCreate([
'key' => $key,
'language' => $language,
'group' => $group,
'value' => $value,
]);
}
}
}
$this->info('Translations imported successfully');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment