初始化载入语言包

Testing Is Documentation

tests/Kernel/Bootstrap/LoadI18nTest.php初始化载入语言包 - 图1

QueryPHP 在内核执行过程中会执行初始化,分为 4 个步骤,载入配置、载入语言包、注册异常运行时和遍历服务提供者注册服务。

内核初始化,包括 \Leevel\Kernel\IKernel::bootstrap\Leevel\Kernel\IKernelConsole::bootstrap 均会执行上述 4 个步骤。

Uses

  1. <?php
  2. use Leevel\Di\Container;
  3. use Leevel\Di\IContainer;
  4. use Leevel\Filesystem\Helper;
  5. use Leevel\Kernel\App as Apps;
  6. use Leevel\Kernel\Bootstrap\LoadI18n;
  7. use Leevel\Kernel\IApp;
  8. use Leevel\Option\Option;

基本使用方法

  1. public function testBaseUse(): void
  2. {
  3. $bootstrap = new LoadI18n();
  4. $container = Container::singletons();
  5. $app = new App($container, $appPath = __DIR__.'/app');
  6. $this->assertInstanceof(IContainer::class, $container);
  7. $this->assertInstanceof(Container::class, $container);
  8. $this->assertInstanceof(IApp::class, $app);
  9. $this->assertInstanceof(Apps::class, $app);
  10. $option = new Option([
  11. 'app' => [
  12. ':composer' => [
  13. 'i18ns' => [
  14. 'extend',
  15. ],
  16. ],
  17. ],
  18. 'i18n' => [
  19. 'default' => 'en-US',
  20. ],
  21. ]);
  22. $container->singleton('option', function () use ($option) {
  23. return $option;
  24. });
  25. $this->assertSame('en-US', $container['option']['i18n\\default']);
  26. $this->assertSame($appPath.'/bootstrap/i18n/en-US.php', $app->i18nCachedPath('en-US'));
  27. $this->assertFalse($app->isCachedI18n('en-US'));
  28. $this->assertSame($appPath.'/i18n', $app->i18nPath());
  29. $this->assertNull($bootstrap->handle($app));
  30. $i18n = $container->make('i18n');
  31. $this->assertSame('Bad Request', $i18n->gettext('错误请求'));
  32. $this->assertSame('Unprocessable Entity', $i18n->gettext('无法处理的实体'));
  33. $this->assertSame('Total 5', $i18n->gettext('共 %d 条', 5));
  34. $this->assertSame('Go to', $i18n->gettext('前往'));
  35. }

语言支持缓存

语言支持缓存,通过缓存可以降低开销提高性能,适合生产环境。

fixture 定义

语言缓存文件 tests/Kernel/Bootstrap/app/assert/en-US.php

  1. <?php
  2. /*
  3. * This file is part of the ************************ package.
  4. * _____________ _______________
  5. * ______/ \__ _____ ____ ______ / /_ _________
  6. * ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
  7. * __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
  8. * \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
  9. * \_\ /_/_/ /_/
  10. *
  11. * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
  12. * (c) 2010-2020 http://queryphp.com All rights reserved.
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  17. return [
  18. '上一页' => 'Previous',
  19. '下一页' => 'Next',
  20. '共 %d 条' => 'Total %d',
  21. '前往' => 'Go to',
  22. '页' => 'Page',
  23. '太多请求' => 'Too Many Requests',
  24. '对于需要登录的网页,服务器可能返回此响应' => 'For web pages that need to be logged in, the server may return this response',
  25. '方法禁用' => 'Method Not Allowed',
  26. '无法处理的实体' => 'Unprocessable Entity',
  27. '服务器不理解请求的语法' => 'The server does not understand the syntax of the request',
  28. '服务器内部错误' => 'Internal Server Error',
  29. '服务器拒绝请求' => 'Server refusal the request',
  30. '服务器遇到错误,无法完成请求' => 'Could not complete request',
  31. '未授权' => 'Unauthorized',
  32. '用户发出的请求针对的是不存在的页面' => 'The user’s request is for a page that does not exist',
  33. '用户在给定的时间内发送了太多的请求' => 'The user sends too many requests within a given time',
  34. '禁止' => 'Forbidden',
  35. '禁用请求中指定的方法' => 'Disable the method specified in the request',
  36. '请求格式正确,但是由于含有语义错误,无法响应' => 'The request format is correct, but because of semantic errors, it cannot respond',
  37. '重试' => 'Retry',
  38. '错误请求' => 'Bad Request',
  39. '页面未找到' => 'Page Not Found',
  40. '首页' => 'Home',
  41. ];
  1. public function testLoadCached(): void
  2. {
  3. $bootstrap = new LoadI18n();
  4. $container = Container::singletons();
  5. $app = new App($container, $appPath = __DIR__.'/app');
  6. $this->assertInstanceof(IContainer::class, $container);
  7. $this->assertInstanceof(Container::class, $container);
  8. $this->assertInstanceof(IApp::class, $app);
  9. $this->assertInstanceof(Apps::class, $app);
  10. $option = new Option([
  11. 'app' => [
  12. ':composer' => [
  13. 'i18ns' => [
  14. 'extend',
  15. ],
  16. ],
  17. ],
  18. 'i18n' => [
  19. 'default' => 'en-US',
  20. ],
  21. ]);
  22. $container->singleton('option', function () use ($option) {
  23. return $option;
  24. });
  25. $this->assertSame('en-US', $container['option']['i18n\\default']);
  26. $this->assertSame($appPath.'/bootstrap/i18n/en-US.php', $app->i18nCachedPath('en-US'));
  27. $this->assertFalse($app->isCachedI18n('en-US'));
  28. $this->assertSame($appPath.'/i18n', $app->i18nPath());
  29. mkdir($appPath.'/bootstrap/i18n', 0777, true);
  30. file_put_contents($appPath.'/bootstrap/i18n/en-US.php', file_get_contents($appPath.'/assert/en-US.php'));
  31. $this->assertTrue($app->isCachedI18n('en-US'));
  32. $this->assertNull($bootstrap->handle($app));
  33. $i18n = $container->make('i18n');
  34. $this->assertSame('Bad Request', $i18n->gettext('错误请求'));
  35. $this->assertSame('Unprocessable Entity', $i18n->gettext('无法处理的实体'));
  36. $this->assertSame('Total 5', $i18n->gettext('共 %d 条', 5));
  37. $this->assertSame('Go to', $i18n->gettext('前往'));
  38. Helper::deleteDirectory($appPath.'/bootstrap');
  39. }