phpGrace 封装了语言包机制,可以方便地进行多语言切换;
使用流程
1 添加语言包文件
<?php
/*
文件位置 : appLanguage/站点或分组目录/语言.php
文件命名 : zh.php、en.php
命名规则 : 语种.php
语言数据 :
*/
return array(
键 => 值
);
#1.1 appLanguage/站点或分组目录/zh.php
<?php
return array(
'appName' => 'phpGrace 极简、高效、低代码框架'
);
#1.2 appLanguage/站点或分组目录/en.php
<?php
return array(
'appName' => 'phpgrace is a minimalist, efficient and low code framework'
);
2、语种设置 garce\tool\language::setType($type)
// 工具类 : garce\tool\language
// 方法 : setType($type)
// 参数 : $type 语种类型
3、获取语言 garce\tool\language::get($key);
// 工具类 : garce\tool\language
// 方法 : get($key)
// 参数 : 语言数组键名称
完整示例
<?php
namespace grace\controller;
use grace\grace;
use grace\tool\language;
class index extends grace{
// 展示语言示例
public function index(language $language){
// $language 工具对象以依赖注入方式自动实例化
echo $language->get('appName');
}
// 设置语言
// 如 http://localhost/index/setLanguage/type/en
public function setLanguage($type = 'zh'){
$type = strtolower($type);
// 允许的设置
$allowTypes = array('zh','en');
// $language 工具对象以 new 方式手动实例化
$language = new language();
$language->setType($type);
echo '设置成功';
}
}