{"id":24756,"date":"2024-11-21T09:08:55","date_gmt":"2024-11-21T01:08:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24756\/"},"modified":"2024-11-21T09:08:55","modified_gmt":"2024-11-21T01:08:55","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e5%9b%bd%e9%99%85%e5%8c%96%e6%94%af%e6%8c%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24756\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u56fd\u9645\u5316\u652f\u6301"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169793365037775.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u56fd\u9645\u5316\u652f\u6301\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u56fd\u9645\u5316\u652f\u6301\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u56fd\u9645\u5316\u652f\u6301<\/p>\n<p>\u968f\u7740\u5168\u7403\u5316\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5f88\u591a\u5e94\u7528\u90fd\u9700\u8981\u5177\u5907\u591a\u8bed\u8a00\u652f\u6301\u7684\u529f\u80fd\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u56fd\u5bb6\u548c\u5730\u533a\u7528\u6237\u7684\u9700\u6c42\u3002Hyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u6b3e\u8f7b\u91cf\u7ea7\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u56fd\u9645\u5316\u652f\u6301\u7684\u529f\u80fd\uff0c\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5b9e\u73b0\u591a\u8bed\u8a00\u5e94\u7528\u7684\u5f00\u53d1\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Hyperf\u6846\u67b6\u4e2d\u4f7f\u7528\u56fd\u9645\u5316\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u914d\u7f6e\u591a\u8bed\u8a00\u652f\u6301<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5728Hyperf\u7684\u914d\u7f6e\u6587\u4ef6 config\/autoload\/i18n.php \u4e2d\u8fdb\u884c\u76f8\u5173\u914d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528 php bin\/hyperf.php vendor:publish hyperf\/i18n \u547d\u4ee4\uff0c\u5c06\u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6\u62f7\u8d1d\u5230 config\/autoload \u76ee\u5f55\u4e0b\u3002\u7136\u540e\u5728 i18n.php \u6587\u4ef6\u4e2d\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre>return [\n    \/\/ \u9ed8\u8ba4\u7684\u8bed\u8a00\u73af\u5883\n    'locale' =&amp;gt; 'zh_CN',\n    \/\/ \u8bed\u8a00\u6587\u4ef6\u7684\u5b58\u653e\u4f4d\u7f6e\n    'fallback_locale' =&amp;gt; 'en',\n    \/\/ \u652f\u6301\u7684\u8bed\u8a00\u5217\u8868\n    'locale_list' =&amp;gt; [\n        'zh_CN',\n        'en',\n    ],\n    \/\/ \u81ea\u52a8\u68c0\u6d4b\u6d4f\u89c8\u5668\u7684\u8bed\u8a00\u8bbe\u7f6e\n    'detect_locale' =&amp;gt; true,\n    \/\/ \u8bed\u8a00\u6587\u4ef6\u7684\u6269\u5c55\u540d\n    'ext' =&amp;gt; '.php',\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0clocale \u662f\u9ed8\u8ba4\u7684\u8bed\u8a00\u73af\u5883\uff0cfallback_locale \u662f\u5f53\u8bf7\u6c42\u7684\u8bed\u8a00\u73af\u5883\u4e0d\u5b58\u5728\u65f6\u7684\u5907\u9009\u8bed\u8a00\u73af\u5883\u3002locale_list \u6307\u5b9a\u4e86\u9879\u76ee\u652f\u6301\u7684\u8bed\u8a00\u5217\u8868\u3002detect_locale \u8bbe\u7f6e\u4e3a true \u8868\u793a\u81ea\u52a8\u68c0\u6d4b\u6d4f\u89c8\u5668\u7684\u8bed\u8a00\u8bbe\u7f6e\u3002ext \u6307\u5b9a\u4e86\u8bed\u8a00\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff0c\u9ed8\u8ba4\u4e3a .php\u3002<\/p>\n<p>\u4e8c\u3001\u7f16\u5199\u8bed\u8a00\u6587\u4ef6<\/p>\n<p>\u5728 resources\/lang \u76ee\u5f55\u4e0b\u521b\u5efa\u5bf9\u5e94\u7684\u8bed\u8a00\u6587\u4ef6\u5939\uff0c\u7136\u540e\u5728\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e0d\u540c\u8bed\u8a00\u73af\u5883\u7684\u8bed\u8a00\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa zh_CN \u548c en \u4e24\u4e2a\u6587\u4ef6\u5939\uff0c\u5206\u522b\u5b58\u653e\u4e2d\u6587\u548c\u82f1\u6587\u7684\u8bed\u8a00\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u6bcf\u4e2a\u8bed\u8a00\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u7ffb\u8bd1\u5185\u5bb9\u5bf9\u5e94\u7684\u952e\u503c\u3002\u4f8b\u5982\uff0c\u5728 zh_CN \u6587\u4ef6\u5939\u4e0b\u521b\u5efa messages.php \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>return [\n    'welcome' =&amp;gt; '\u6b22\u8fce\u4f7f\u7528Hyperf\u6846\u67b6',\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 en \u6587\u4ef6\u5939\u4e0b\u521b\u5efa messages.php \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>return [\n    'welcome' =&amp;gt; 'Welcome to Hyperf framework',\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u4f7f\u7528\u8bed\u8a00\u5305<\/p>\n<p>\u5728\u63a7\u5236\u5668\u6216\u670d\u52a1\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 HyperfUtilsApplicationContext::getContainer()-&gt;get(&#8216;translator&#8217;) \u6765\u83b7\u53d6\u7ffb\u8bd1\u5668\uff08translator\uff09\u5b9e\u4f8b\uff0c\u7136\u540e\u901a\u8fc7\u7ffb\u8bd1\u5668\u7684 trans \u65b9\u6cd5\u6765\u83b7\u53d6\u5bf9\u5e94\u8bed\u8a00\u73af\u5883\u7684\u7ffb\u8bd1\u5185\u5bb9\u3002<\/p>\n<pre>&lt;?php namespace AppController;\n\nuse HyperfHttpServerAnnotationController;\nuse HyperfHttpServerAnnotationRequestMapping;\nuse HyperfHttpServerContractRequestInterface;\nuse HyperfUtilsApplicationContext;\n\n\/**\n * @Controller()\n *\/\nclass IndexController\n{\n    \/**\n     * @RequestMapping(\"\/\")\n     *\/\n    public function index(RequestInterface $request)\n    {\n        $translator = ApplicationContext::getContainer()-&gt;get('translator');\n        \n        \/\/ \u83b7\u53d6\u5f53\u524d\u8bed\u8a00\u73af\u5883\n        $locale = $translator-&amp;gt;getLocale();\n        \n        \/\/ \u83b7\u53d6\u8bed\u8a00\u5305\u4e2d\u7684\u7ffb\u8bd1\u5185\u5bb9\n        $welcome = $translator-&amp;gt;trans('welcome');\n        \n        return [\n            'locale' =&amp;gt; $locale,\n            'welcome' =&amp;gt; $welcome,\n        ];\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528 $translator-&gt;getLocale() \u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u8bed\u8a00\u73af\u5883\u3002\u7136\u540e\u901a\u8fc7 $translator-&gt;trans(&#8216;welcome&#8217;) \u83b7\u53d6\u5bf9\u5e94\u7684\u7ffb\u8bd1\u5185\u5bb9\u3002<\/p>\n<p>\u56db\u3001\u5207\u6362\u8bed\u8a00\u73af\u5883<\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u7528\u6237\u7684\u9009\u62e9\u6216\u5176\u5b83\u6761\u4ef6\u6765\u5207\u6362\u8bed\u8a00\u73af\u5883\u3002Hyperf\u6846\u67b6\u63d0\u4f9b\u4e86 HyperfUtilsContext \u7c7b\u6765\u5b9e\u73b0\u4e0a\u4e0b\u6587\uff0c\u53ef\u4ee5\u901a\u8fc7 Context::getContainer()-&gt;set(&#8216;locale&#8217;, $locale) \u6765\u8bbe\u7f6e\u8bed\u8a00\u73af\u5883\u3002<\/p>\n<pre>&lt;?php use HyperfUtilsContext;\n\n\/\/ \u5207\u6362\u5230\u4e2d\u6587\u73af\u5883\nContext::getContainer()-&gt;set('locale', 'zh_CN');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 set(&#8216;locale&#8217;, $locale) \u5c06\u8bed\u8a00\u73af\u5883\u8bbe\u7f6e\u4e3a\u4e2d\u6587\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Hyperf\u6846\u67b6\u4e2d\u6210\u529f\u5b9e\u73b0\u56fd\u9645\u5316\u652f\u6301\u7684\u529f\u80fd\u3002\u9996\u5148\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8fdb\u884c\u76f8\u5173\u914d\u7f6e\uff0c\u7136\u540e\u7f16\u5199\u8bed\u8a00\u6587\u4ef6\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u7ffb\u8bd1\u5668\u6765\u83b7\u53d6\u7ffb\u8bd1\u5185\u5bb9\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u4f7f\u7528\u4e0a\u4e0b\u6587\u6765\u5207\u6362\u8bed\u8a00\u73af\u5883\u3002<\/p>\n<p>\u901a\u8fc7Hyperf\u6846\u67b6\u7684\u56fd\u9645\u5316\u652f\u6301\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u591a\u8bed\u8a00\u5e94\u7528\uff0c\u4e3a\u5168\u7403\u7528\u6237\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u56fd\u9645\u5316\u652f\u6301\u7684\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u56fd\u9645\u5316\u652f\u6301\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u56fd\u9645\u5316\u652f\u6301 \u968f\u7740\u5168\u7403\u5316\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5f88\u591a\u5e94\u7528\u90fd\u9700\u8981\u5177\u5907\u591a\u8bed\u8a00\u652f\u6301\u7684\u529f\u80fd\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u56fd\u5bb6\u548c\u5730\u533a\u7528\u6237\u7684\u9700\u6c42\u3002Hyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u6b3e\u8f7b\u91cf\u7ea7\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u56fd\u9645\u5316\u652f\u6301\u7684\u529f\u80fd\uff0c\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5b9e\u73b0\u591a\u8bed\u8a00\u5e94\u7528\u7684\u5f00\u53d1\u3002 \u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Hyperf\u6846\u67b6\u4e2d\u4f7f\u7528\u56fd\u9645\u5316\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u914d\u7f6e\u591a\u8bed\u8a00\u652f\u6301 \u9996\u5148\uff0c\u9700\u8981\u5728Hyperf\u7684\u914d\u7f6e\u6587\u4ef6 config\/autoload\/i18n.php \u4e2d\u8fdb\u884c\u76f8\u5173\u914d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528 php bin\/hyperf.php vendor:publish hyperf\/i18n \u547d\u4ee4\uff0c\u5c06\u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6\u62f7\u8d1d\u5230 config\/autoload \u76ee\u5f55\u4e0b\u3002\u7136\u540e\u5728 i18n.php \u6587\u4ef6\u4e2d\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a return [ \/\/ \u9ed8\u8ba4\u7684\u8bed\u8a00\u73af\u5883 &#8216;locale&#8217; =&amp;gt; &#8216;zh_CN&#8217;, \/\/ \u8bed\u8a00\u6587\u4ef6\u7684\u5b58\u653e\u4f4d\u7f6e &#8216;fallback_locale&#8217; =&amp;gt; &#8216;en&#8217;, \/\/ \u652f\u6301\u7684\u8bed\u8a00\u5217\u8868 &#8216;locale_list&#8217; =&amp;gt; [ &#8216;zh_CN&#8217;, &#8216;en&#8217;, ], \/\/ \u81ea\u52a8\u68c0\u6d4b\u6d4f\u89c8\u5668\u7684\u8bed\u8a00\u8bbe\u7f6e &#8216;detect_locale&#8217; =&amp;gt; true, \/\/ \u8bed\u8a00\u6587\u4ef6\u7684\u6269\u5c55\u540d &#8216;ext&#8217; =&amp;gt; &#8216;.php&#8217;, ]; \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0clocale \u662f\u9ed8\u8ba4\u7684\u8bed\u8a00\u73af\u5883\uff0cfallback_locale \u662f\u5f53\u8bf7\u6c42\u7684\u8bed\u8a00\u73af\u5883\u4e0d\u5b58\u5728\u65f6\u7684\u5907\u9009\u8bed\u8a00\u73af\u5883\u3002locale_list \u6307\u5b9a\u4e86\u9879\u76ee\u652f\u6301\u7684\u8bed\u8a00\u5217\u8868\u3002detect_locale \u8bbe\u7f6e\u4e3a true \u8868\u793a\u81ea\u52a8\u68c0\u6d4b\u6d4f\u89c8\u5668\u7684\u8bed\u8a00\u8bbe\u7f6e\u3002ext [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24756","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24756"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24756\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}