I'm newbie in laravel 4.0.
lang/en/texts.php
?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
...
You can get the entire array with Lang::get()
.
$array = Lang::get('pagination'); // return entire array
$text = Lang::get('pagination.next'); // return single item
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With