Skip to content

Instantly share code, notes, and snippets.

@nmfzone
Last active March 18, 2023 17:25
Show Gist options
  • Save nmfzone/84865ca9efbde277550999cd37455df8 to your computer and use it in GitHub Desktop.
Save nmfzone/84865ca9efbde277550999cd37455df8 to your computer and use it in GitHub Desktop.
Laravel language files on custom directory
// Create new Lang custom directory
// example in : app/Lang/
// Create lang directory for each lang in that directory
// example lang : id
// so i create : app/Lang/id
//
// DON'T forget change locale in config/app.php to 'id'
//
// then added new alias in aliases array in that file also
<?php
.......
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => 'id',
.......
'aliases' => [
...
'Message' => App\Services\MessagesTranslator::class,
]
......
// Create php file for place your messages
// example : app/Lang/id/messages.php
<?php
return [
'awesome' => [
'index' => 'I\'m Awesome.'
],
'wonderfull' => [
'success' => 'It\'s success, wonderfull.'
]
];
// Create MessagesTranslationServiceProvider
// example in : app/Providers/MessagesTranslationServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\MessagesTranslator;
class MessagesTranslationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->app->singleton('App\Services\MessagesTranslator', function () {
return new MessagesTranslator;
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
// Create MessagesTranslator class
// example in : app/Services/MessagesTranslator.php
<?php
namespace App\Services;
class MessagesTranslator
{
/**
* The translator custom location.
*
* @var string
*/
protected $location = null;
/**
* The translator base key.
*
* @var string
*/
protected $base;
/**
* Set the translator custom location.
*
* @param string $location
* @return $this
*/
public function setLocation($location)
{
$this->location = $location;
return $this;
}
/**
* Set the translator base key.
*
* @param string $base
* @return $this
*/
public function setBase($base)
{
$this->base = $base;
return $this;
}
/**
* Get the translation for a given key.
*
* @param string $key
* @return string
*/
public function shout($key)
{
$key = $this->base . '.' . $key;
if (null !== $this->location) {
app('translator')->addNamespace('custom', base_path() . '/' . $this->location);
return app('translator')->get('custom::' . $key);
}
return trans($key);
}
}
// How to use
// in this example i show you how to use in routes.php
// you can use this anywhere you like
Route::get('trans', function(Message $message) {
$message->setLocation('app/Lang');
$message->setBase('messages.awesome');
echo $message->shout('index');
$message->setBase('messages.wonderfull');
echo $message->shout('success');
});
@mq321
Copy link

mq321 commented Nov 6, 2022

👍

@mra-dev
Copy link

mra-dev commented Mar 18, 2023

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment