Created
February 2, 2025 15:21
-
-
Save egyjs/3bb2e05f4bcecc3b39543293aac92b94 to your computer and use it in GitHub Desktop.
Laravel Import translations from lang files to database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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