Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get whole language file array

I'm newbie in laravel 4.0.

  • How to get the whole array from lang/en/texts.php?
  • Is there a Lang::getAll() method?

My goal is to generate keywords/description in my base controller, to fill them into the the meta tags and other places in the DOM in the master blade template. If my approach is completely wrong, please tell me!

Generating the keywords and description from an associative array is NOT the problem, but the lack of knowledge about the framework. And, I was googling for quite a time before ending up here...

Working with blade templates: This is my BaseController:

class HomeController extends BaseController {

    protected $layout = 'layouts.master';
    private $keyWords = array();

    private function getKeyWords () {
        // ???

    }

    public function getIndex() {
        return View::make('home')
            ->with('errorcanvas', trans('texts.canvas'))
            ->with('errortextwebgl', trans('texts.webgl'))
            ...;
    }

    ...

}

I found something in the API:

Illuminate\Translation\FileLoader load() which loads the messages with a given locale ...

like image 483
toesslab Avatar asked Dec 21 '13 11:12

toesslab


3 Answers

You can get the entire array with Lang::get().

$array = Lang::get('pagination'); // return entire array
$text  = Lang::get('pagination.next'); // return single item
like image 198
Justin Avatar answered Oct 20 '22 10:10

Justin


Lets say, a language file: lang/en/countries.php

return [
 'afg' => 'Afghanistan',
 'ala' => 'Aland',
 'alb' => 'Albania',
 'dza' => 'Algeria',
 'asm' => 'American Samoa'
];

Retrieving lines from the language file with Lang::get() method

$array = Lang::get('countries'); // return entire array
$text  = Lang::get('countries.afg'); // return single item

for Laravel 5.0 & above, You may also use the trans helper function, which is an alias for the Lang::get() method.

$array = trans('countries'); // return entire array
$text = trans('countries.afg'); // return single item

Find out more on Laravel docs...

like image 17
codeye Avatar answered Oct 20 '22 12:10

codeye


Here's how yo can load them:

Route::get('test', function() 
{
    $a = File::getRequire(base_path().'/app/lang/en/pagination.php');

    foreach($a as $key => $value)
    {
        echo "$key => $value<br>";
    }
});

If you need to load them all, you can use:

$languages = File::directories(base_path().'/app/lang/');

I had to find a way to create an language import command in my Glottos package: https://github.com/antonioribeiro/glottos.

like image 6
Antonio Carlos Ribeiro Avatar answered Oct 20 '22 11:10

Antonio Carlos Ribeiro