zhaojs
2023-10-07 6c7bba2e05c011a3d640b6565a113204228e92e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
 
/**
 * Lang测试
 * @author    liu21st <liu21st@gmail.com>
 */
 
namespace tests\thinkphp\library\think;
 
use think\Config;
use think\Lang;
 
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'));
 
        Lang::set('hello,{:name}', '欢迎,{:name}');
        $this->assertEquals('欢迎,liu21st', Lang::get('hello,{:name}', ['name' => 'liu21st']));
    }
 
    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');
 
        Config::set('lang_detect_var', 'lang');
        Config::set('lang_cookie_var', 'think_cookie');
 
        $_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()
    {
        $this->assertEquals('zh-cn', Lang::range());
        Lang::set('hello', '欢迎', 'test');
        Lang::range('test');
        $this->assertEquals('test', Lang::range());
        $this->assertEquals('欢迎', Lang::get('hello'));
    }
}