langTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Lang测试
  13. * @author liu21st <liu21st@gmail.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use think\Config;
  17. use think\Lang;
  18. class langTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function testSetAndGet()
  21. {
  22. Lang::set('hello,%s', '欢迎,%s');
  23. $this->assertEquals('欢迎,ThinkPHP', Lang::get('hello,%s', ['ThinkPHP']));
  24. Lang::set('hello,%s', '歡迎,%s', 'zh-tw');
  25. $this->assertEquals('歡迎,ThinkPHP', Lang::get('hello,%s', ['ThinkPHP'], 'zh-tw'));
  26. Lang::set(['hello' => '欢迎', 'use' => '使用']);
  27. $this->assertEquals('欢迎', Lang::get('hello'));
  28. $this->assertEquals('欢迎', Lang::get('HELLO'));
  29. $this->assertEquals('使用', Lang::get('use'));
  30. Lang::set('hello,{:name}', '欢迎,{:name}');
  31. $this->assertEquals('欢迎,liu21st', Lang::get('hello,{:name}', ['name' => 'liu21st']));
  32. }
  33. public function testLoad()
  34. {
  35. Lang::load(__DIR__ . DS . 'lang' . DS . 'lang.php');
  36. $this->assertEquals('加载', Lang::get('load'));
  37. Lang::load(__DIR__ . DS . 'lang' . DS . 'lang.php', 'test');
  38. $this->assertEquals('加载', Lang::get('load', [], 'test'));
  39. }
  40. public function testDetect()
  41. {
  42. Config::set('lang_list', ['zh-cn', 'zh-tw']);
  43. Lang::set('hello', '欢迎', 'zh-cn');
  44. Lang::set('hello', '歡迎', 'zh-tw');
  45. Config::set('lang_detect_var', 'lang');
  46. Config::set('lang_cookie_var', 'think_cookie');
  47. $_GET['lang'] = 'zh-tw';
  48. Lang::detect();
  49. $this->assertEquals('歡迎', Lang::get('hello'));
  50. $_GET['lang'] = 'zh-cn';
  51. Lang::detect();
  52. $this->assertEquals('欢迎', Lang::get('hello'));
  53. }
  54. public function testRange()
  55. {
  56. $this->assertEquals('zh-cn', Lang::range());
  57. Lang::set('hello', '欢迎', 'test');
  58. Lang::range('test');
  59. $this->assertEquals('test', Lang::range());
  60. $this->assertEquals('欢迎', Lang::get('hello'));
  61. }
  62. }