Modify or delete Laravel lang files content in controller.
Tags: laravel, php, webdev, beginners
Published: 2020-01-12 17:36:02

In my current project, I need to allow the users to modify langauge files content in admin panel. My first thought was making it with mySql but I changed it to modify the files itselves to keep things at the same place.

##What can it do?##
1. It can modify a certain key.
2. It can add a new key.
3. It can delete a certain key.

##How is it work?##
We will creat a controller named <u>LocaleFileController</u>
1. We read the lang file to an array.
2. We make the desired changes.
3. We write back the file.

##Limitations##
1. Tested in Laravel6.
2. It works if you don't have nested content in your localization files.

So, let's get started...

##1. Step - Preparations##
First, we create a controller. Open the command line processor and type:

```
C:\www\projectDierectory> php artisan make:controller LocaleFileController
```
Open the file and write some lines in it as bellow.

```php
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
use Lang;

class LocaleFileController extends Controller
{

	private $lang = '';
	private $file;
	private $key;
	private $value;
	private $path;
	private $arrayLang = array();
```
**<u>Let's see the variables:</u>**
$lang: Which language will be modified.
$file: The file name containing the required key.
$key: The key to modify or delete.
$value: The new value of the key.
$path: The path of the file. (Maybe you have subdirectories.)
$arrayLang: The array of key and value reading from the file.

##2. Step - Make a function to add or modify a key.##
We make a function to modify or add a new key.
If the key exists, it will be modified, and if it doesn't exist, it will be added as a new key.
<small>(The read() and save() functions will be created little later.)</small>

Put it in your <u>LocaleFileController.php</u>.
```php
//------------------------------------------------------------------------------
// Add or modify lang files content
//------------------------------------------------------------------------------

    private function changeLangFileContent($lang, $file, $key, $value) 
    {
		$this->read();
		$this->arrayLang[$this->key] = $this->value;
		$this->save();
	}
```
##3. Step - Make a function to delete a key.##
This function to delete a key by using the php unset function.

Put it in your <u>LocaleFileController.php</u>.

```php
//------------------------------------------------------------------------------
// Delete from lang files
//------------------------------------------------------------------------------

    private function deleteLangFileContent() 
    {
		$this->read();
		unset($this->arrayLang[$this->key]);
		$this->save();
	}
```

##4. Step - Open and read the file to array##

Put it in your <u>LocaleFileController.php</u>.

```php
//------------------------------------------------------------------------------
// Read lang file content
//------------------------------------------------------------------------------

    private function read() 
    {
		if ($this->lang == '') $this->lang = App::getLocale();
		$this->path = base_path().'/resources/lang/'.$this->lang.'/'.$this->file.'.php';
		$this->arrayLang = Lang::get($this->file);
		if (gettype($this->arrayLang) == 'string') $this->arrayLang = array();
	}
```
As you can see, if you don't add value to $lang variable, it will be the current locale.
The <u>Lang::get('filename')</u> reads the whole file in an array, so it's really ease for us. <small>(Thanks Laravel.)</small>

The last line may be interesting. If the file doesn't exist, then the Laravel put a string in the variable with the file name. So when it happens, we redeclare the array and the <u>save()</u> function will create the missing file. 

##5. Step - Save the file##

Put it in your <u>LocaleFileController.php</u>.

```php
//------------------------------------------------------------------------------
// Save lang file content
//------------------------------------------------------------------------------

    private function save() 
    {
		$content = "<?php\n\nreturn\n[\n";

		foreach ($this->arrayLang as $this->key => $this->value) 
		{
			$content .= "\t'".$this->key."' => '".$this->value."',\n";
		}

		$content .= "];";

		file_put_contents($this->path, $content);
	}
```
We write back the file conserving its format by tab and new line chars.

##6. Step - Put it together##

We create a public function in our controller (e.g. <u>changeLang</u>) what we can call from route.php and where we prepare the request data end where we call the functions we made before. Maybe we want modify only one key or maybe more. It depends on our project.

Finally, this is our <u>LocaleFileController.php<u>.


```php
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
use Lang;

class LocaleFileController extends Controller
{
	private $lang = '';
	private $file;
	private $key;
	private $value;
	private $path;
	private $arrayLang = array();
	
//------------------------------------------------------------------------------
// Add or modify lang files content
//------------------------------------------------------------------------------

    public function changeLang(Request $request) 
    {
// Process and prepare you data as you like.

		$this->lang = '';
		$this->file = 'shop';
		$this->key = 'productGroup';
		$this->value = 'whatever';

// END - Process and prepare your data
		
		$this->changeLangFileContent();
		//$this->deleteLangFileContent();
		
		return view('admin/index'); 
	}
	
//------------------------------------------------------------------------------
// Add or modify lang files content
//------------------------------------------------------------------------------

    private function changeLangFileContent() 
    {
		$this->read();
		$this->arrayLang[$this->key] = $this->value;
		$this->save();
	}
	
//------------------------------------------------------------------------------
// Delete from lang files
//------------------------------------------------------------------------------

    private function deleteLangFileContent() 
    {
		$this->read();
		unset($this->arrayLang[$this->key]);
		$this->save();
	}
	
//------------------------------------------------------------------------------
// Read lang file content
//------------------------------------------------------------------------------

    private function read() 
    {
		if ($this->lang == '') $this->lang = App::getLocale();
		$this->path = base_path().'/resources/lang/'.$this->lang.'/'.$this->file.'.php';
		$this->arrayLang = Lang::get($this->file);
		if (gettype($this->arrayLang) == 'string') $this->arrayLang = array();
	}
	
//------------------------------------------------------------------------------
// Save lang file content
//------------------------------------------------------------------------------

    private function save() 
    {
		$content = "<?php\n\nreturn\n[\n";

		foreach ($this->arrayLang as $this->key => $this->value) 
		{
			$content .= "\t'".$this->key."' => '".$this->value."',\n";
		}

		$content .= "];";

		file_put_contents($this->path, $content);

	}
}

```
##7. Step - Enjoy your work.##