From 58eebe7763d8a224e538ab8c155e1227007678d1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 28 Feb 2016 13:41:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0lang=E7=B1=BB=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 3 +- library/think/Lang.php | 7 +-- library/think/Model.php | 3 +- tests/thinkphp/library/think/lang/lang.php | 4 ++ tests/thinkphp/library/think/langTest.php | 64 ++++++++++++++++++++++ 5 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 tests/thinkphp/library/think/lang/lang.php create mode 100644 tests/thinkphp/library/think/langTest.php diff --git a/library/think/App.php b/library/think/App.php index d7483012..ea01abaa 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -153,8 +153,7 @@ class App { $args = []; // 判断数组类型 数字数组时按顺序绑定参数 - $keys = array_keys($vars); - $type = array_keys($keys) === $keys ? 1 : 0; + $type = key($vars) === 0 ? 1 : 0; if ($reflect->getNumberOfParameters() > 0) { $params = $reflect->getParameters(); foreach ($params as $param) { diff --git a/library/think/Lang.php b/library/think/Lang.php index eeaa3ae8..22bccc96 100644 --- a/library/think/Lang.php +++ b/library/think/Lang.php @@ -94,7 +94,7 @@ class Lang } $key = strtolower($name); $value = isset(self::$lang[$range][$key]) ? self::$lang[$range][$key] : $name; - + // 变量解析 if (!empty($vars) && is_array($vars)) { /** @@ -104,7 +104,7 @@ class Lang */ if (key($vars) === 0) { // 数字索引解析 - array_unshift($vars, $name); + array_unshift($vars, $value); $value = call_user_func_array('sprintf', $vars); } else { // 关联索引解析 @@ -114,8 +114,7 @@ class Lang } $value = str_replace($replace, $vars, $value); } - - + } return $value; } diff --git a/library/think/Model.php b/library/think/Model.php index 8d12a161..e689b3f1 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -536,8 +536,7 @@ class Model // 根据主键查询 if (is_array($options)) { // 判断是否索引数组 - $keys = array_keys($options); - if (array_keys($keys) === $keys) { + if (key($options) === 0) { $where[$pk] = ['in', $options]; } else { return; diff --git a/tests/thinkphp/library/think/lang/lang.php b/tests/thinkphp/library/think/lang/lang.php new file mode 100644 index 00000000..044b171b --- /dev/null +++ b/tests/thinkphp/library/think/lang/lang.php @@ -0,0 +1,4 @@ +'加载', +]; \ No newline at end of file diff --git a/tests/thinkphp/library/think/langTest.php b/tests/thinkphp/library/think/langTest.php new file mode 100644 index 00000000..e9985c15 --- /dev/null +++ b/tests/thinkphp/library/think/langTest.php @@ -0,0 +1,64 @@ + +// +---------------------------------------------------------------------- + +/** + * Lang测试 + * @author liu21st + */ + +namespace tests\thinkphp\library\think; + +use think\Lang; +use think\Config; + +class langTest extends \PHPUnit_Framework_TestCase +{ + + public function testSetAndGet(){ + Lang::set('hello,%s','欢迎,%s'); + $this->assertEquals('欢迎,ThinkPHP',Lang::get('hello,%s',['ThinkPHP']) ); + Lang::set('hello,%s','歡迎,%s','zh-tw'); + $this->assertEquals('歡迎,ThinkPHP',Lang::get('hello,%s',['ThinkPHP'],'zh-tw') ); + Lang::set(['hello'=>'欢迎','use'=>'使用']); + $this->assertEquals('欢迎',Lang::get('hello') ); + $this->assertEquals('欢迎',Lang::get('HELLO') ); + $this->assertEquals('使用',Lang::get('use') ); + } + + public function testLoad(){ + Lang::load(__DIR__.DS.'lang'.DS.'lang.php'); + $this->assertEquals('加载',Lang::get('load') ); + Lang::load(__DIR__.DS.'lang'.DS.'lang.php','test'); + $this->assertEquals('加载',Lang::get('load',[],'test') ); + } + + public function testDetect(){ + + Config::set('lang_list',['zh-cn','zh-tw']); + Lang::set('hello','欢迎','zh-cn'); + Lang::set('hello','歡迎','zh-tw'); + /* + $_GET['lang'] = 'zh-tw'; + Lang::detect(); + $this->assertEquals('歡迎',Lang::get('hello') );*/ + + $_GET['lang'] = 'zh-cn'; + Lang::detect(); + $this->assertEquals('欢迎',Lang::get('hello') ); + + } + + public function testRange(){ + Lang::set('hello','欢迎','test'); + Lang::range('test'); + $this->assertEquals('欢迎',Lang::get('hello') ); + } +}