php 获取整个语言文件数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20718291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:27:49  来源:igfitidea点击:

Get whole language file array

phplaravellaravel-4

提问by toesslab

I'm newbie in laravel 4.0.

我是laravel 4.0 的新手。

  • How to get the whole array from lang/en/texts.php?
  • Is there a Lang::getAll()method?
  • 如何从中获取整个数组lang/en/texts.php
  • Lang::getAll()方法吗?

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!

我的目标是在我的基本控制器中生成关键字/描述,将它们填充到元标记和主刀片模板中 DO​​M 中的其他位置。如果我的方法完全错误,请告诉我!

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:

使用刀片模板:这是我的 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:

我在API 中找到了一些东西:

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

Illuminate\Translation\FileLoader load()它使用给定的locale...加载消息

回答by Justin

You can get the entire arraywith Lang::get().

你可以得到整个阵列Lang::get()

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

回答by k.t

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

比方说,一个语言文件: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

使用Lang::get()方法从语言文件中检索行

$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 transhelper function, which is an alias for the Lang::get()method.

对于Laravel 5.0及以上版本,您还可以使用trans辅助函数,它是Lang::get()方法的别名。

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

Find out more on Laravel docs...

了解更多关于 Laravel 文档...

回答by Antonio Carlos Ribeiro

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.

我必须找到一种方法在我的 Glottos 包中创建语言导入命令:https: //github.com/antonioribeiro/glottos

回答by Andreyco

Antonio answered loading of language file perfectly, but I don't like your approach.
It can be done much easier. Why not to do it this way?

安东尼奥完美地回答了语言文件的加载,但我不喜欢你的方法。
它可以更容易地完成。为什么不这样做呢?

// view file
<html>
  <head>
    <meta name="description" content="{{ trans('texts.description') }}">
    ...
  </head>
  ...
</html>

You do not need to send all the content thru controller, which makes your controller clean.

您不需要通过控制器发送所有内容,这使您的控制器干净。

回答by Pezhvak

you can actually use the helper function __('lang_file')as well.

您实际上也可以使用辅助函数__('lang_file')