1.本地化
Laravel 的本地化功能提供方便的方法來獲取多語言的字符串,讓你的網站可以簡單的支持多語言。
語言包存放在 resources/lang 文件夾的文件里。每一個子目錄應該對應一種語言
最初的目錄結構,里面包含了驗證類的一些提示內容


下面我們添加一個中文的語言文件,以test文件舉例


語言包簡單地返回鍵值和字符串數組,例如:
<?php return array(
'first'=>'this is the first test' ,
'second'=>'this is the second test'
);
接下來我們切換語言包,進入config/app.php 配置文件
更改如下:
'locale' => 'zh', //設置使用的語言包
'fallback_locale' => 'en', //設置 "備用語言",它將會在當現有語言沒有指定語句時被使用
使用方法:
使用 trans 輔助函數來獲取語言字符串,trans 函數的第一個參數接受文件名和鍵值名稱
控制器中測試代碼
echo trans('test.first');
echo '<br/>';
//由於我們沒有在zh文件夾下創建validation.php文件,所以在備用語言中尋找
echo trans('validation.accepted');
exit;
結果如下:
this is the first test
The :attribute must be accepted.
如果改動config/app.php 配置文件中的 'locale' => 'en',
發現結果發現:
test.first
The :attribute must be accepted.
擴展:
如果需要,你也可以在語言包中(這里可以在test.php文件中)定義占位符,占位符使用 : 開頭,
'welcome' => 'Welcome, :name',
接着,傳入替代用的第二個參數給 trans 方法:
echo trans('test.welcome', ['name' => 'Archer']);
2.常量的使用
在app/config文件夾下新建一個php文件,這里我們建立constants.php文件,內容如下
<?php
return array(
//成交信息
'FIRST' => '測試常量信息111',
'SECOND' => array('NEXT' => '測試常量信息第二層')
);
使用方法
echo Config::get('constants.
FIRST');
echo Config::get('constants.
SECOND.NEXT');
或者
echo config(sprintf('constant.%s', 'FIRST'), null);
echo config(sprintf('constant.%s', 'SECOND.NEXT'), null);