{"id":22487,"date":"2024-11-21T11:36:26","date_gmt":"2024-11-21T03:36:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22487\/"},"modified":"2024-11-21T11:36:26","modified_gmt":"2024-11-21T03:36:26","slug":"laravel%e6%80%8e%e4%b9%88%e5%b0%86%e8%8b%b1%e6%96%87%e7%ac%a6%e5%8f%b7%e8%bd%ac%e4%b8%ad%e6%96%87%e7%ac%a6%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22487\/","title":{"rendered":"laravel\u600e\u4e48\u5c06\u82f1\u6587\u7b26\u53f7\u8f6c\u4e2d\u6587\u7b26\u53f7"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u4eba\u4eec\u7684\u4ea4\u6d41\u65b9\u5f0f\u4e5f\u8d8a\u6765\u8d8a\u591a\u5143\u5316\u3002\u5728\u4f7f\u7528\u7535\u5b50\u90ae\u4ef6\u3001\u5fae\u4fe1\u3001\u77ed\u4fe1\u7b49\u5de5\u5177\u8fdb\u884c\u6c9f\u901a\u65f6\uff0c\u5e38\u5e38\u4f1a\u51fa\u73b0\u82f1\u6587\u7b26\u53f7\uff0c\u5982 \u201c@\u201d\u3001\u201c#\u201d\u3001\u201c&amp;\u201d\u7b49\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528\u4e2d\u6587\u8fdb\u884c\u4ea4\u6d41\u65f6\uff0c\u82f1\u6587\u7b26\u53f7\u5f80\u5f80\u4f1a\u7ed9\u4eba\u5e26\u6765\u56f0\u6270\u3002\u7279\u522b\u662f\u5728\u4f7f\u7528laravel\u8fd9\u6837\u7684\u5f00\u53d1\u6846\u67b6\u8fdb\u884c\u7f16\u7a0b\u65f6\uff0c\u82f1\u6587\u7b26\u53f7\u7684\u51fa\u73b0\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u548c\u7406\u89e3\u3002\u4e3a\u6b64\uff0claravel\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u597d\u7684\u89e3\u51b3\u65b9\u6848\u2014\u2014\u82f1\u6587\u7b26\u53f7\u8f6c\u4e2d\u6587\u7b26\u53f7\u3002<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53eb\u505a\u201cLaravel Localization\u201d\u7684\u7ec4\u4ef6\u6765\u5b9e\u73b0\u82f1\u6587\u7b26\u53f7\u8f6c\u4e2d\u6587\u7b26\u53f7\u3002Localization\uff08\u672c\u5730\u5316\uff09\u662f\u6307\u5c06\u7a0b\u5e8f\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u548c\u6587\u5316\u73af\u5883\u7684\u8fc7\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528Laravel Localization\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u82f1\u6587\u7b26\u53f7\u8f6c\u6362\u4e3a\u4e2d\u6587\u7b26\u53f7\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/p>\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528Laravel Localization\u8fdb\u884c\u82f1\u6587\u7b26\u53f7\u5230\u4e2d\u6587\u7b26\u53f7\u7684\u8f6c\u6362\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Laravel Localization\u3002\u53ef\u4ee5\u901a\u8fc7Composer\u6765\u5b89\u88c5\uff1a<\/p>\n<pre>composer require mcamara\/laravel-localization<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728 config\/app.php \u4e2d\u6ce8\u518cServiceProvider\uff1a<\/p>\n<pre>'providers' =&gt; [\n    \/\/ ...\n    McamaraLaravelLocalizationLaravelLocalizationServiceProvider::class,\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u7740\uff0c\u5728 config\/app.php \u4e2d\u6ce8\u518cFacades\uff1a<\/p>\n<pre>'aliases' =&gt; [\n    \/\/ ...\n    'LaravelLocalization' =&gt; McamaraLaravelLocalizationFacadesLaravelLocalization::class,\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 routes.php \u6587\u4ef6\u4e2d\u5b9a\u4e49\u6240\u6709\u652f\u6301\u7684\u8bed\u8a00\uff1a<\/p>\n<pre>Route::group([\n    'prefix' =&gt; LaravelLocalization::setLocale()], function () {\n         \/\/ ...\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u5b9a\u4e49\u8bed\u8a00\u540e\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u8bed\u8a00\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u4fdd\u5b58\u4e86\u6240\u6709\u7684\u82f1\u6587\u7b26\u53f7\u4ee5\u53ca\u5bf9\u5e94\u7684\u4e2d\u6587\u7b26\u53f7\u3002\u5728Laravel Localization\u4e2d\uff0c\u8bed\u8a00\u6587\u4ef6\u901a\u5e38\u5b58\u50a8\u5728 resources\/lang \u76ee\u5f55\u4e0b\uff0c\u547d\u540d\u4e3a en.php\uff08\u82f1\u6587\uff09\u548c cn.php\uff08\u4e2d\u6587\uff09\u3002<\/p>\n<p>\u4ee5\u4f7f\u7528\u7684\u8bed\u8a00\u4e3a\u4e2d\u6587\u4e3a\u4f8b\uff0c\u6211\u4eec\u5728 cn.php \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>return [\n    '@' =&gt; '\uff20', \n    '#' =&gt; '\uff03', \n    '&amp;' =&gt; '\uff06', \n    \/\/ ...\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u5b9a\u4e49\uff0c\u6211\u4eec\u5c06 @ \u7b26\u53f7\u8f6c\u6362\u4e3a \uff20 \u7b26\u53f7\uff0c# \u7b26\u53f7\u8f6c\u6362\u4e3a \uff03 \u7b26\u53f7\uff0c&amp; \u7b26\u53f7\u8f6c\u6362\u4e3a \uff06 \u7b26\u53f7\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 trans \u51fd\u6570\u8fdb\u884c\u7b26\u53f7\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u4f7f\u7528 trans \u51fd\u6570\u5c06 @ \u7b26\u53f7\u8f6c\u6362\u4e3a \uff20 \u7b26\u53f7\uff1a<\/p>\n<pre>$email = 'example@mail.com';\n$converted_email = trans($email, ['@' =&gt; '\uff20']);\necho $converted_email; \/\/ \u5c06\u8f93\u51fa example\uff20mail.com<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cLaravel Localization\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u4e14\u6613\u4e8e\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u5f88\u597d\u5730\u89e3\u51b3\u82f1\u6587\u7b26\u53f7\u5728\u4f7f\u7528\u4e2d\u6587\u8fdb\u884c\u4ea4\u6d41\u65f6\u7684\u95ee\u9898\u3002<\/p>\n<p>\u603b\u7ed3\u6765\u8bf4\uff0cLaravel Localization\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5728\u4f7f\u7528Laravel\u6846\u67b6\u8fdb\u884c\u7f16\u7a0b\u65f6\u8fdb\u884c\u82f1\u6587\u7b26\u53f7\u5230\u4e2d\u6587\u7b26\u53f7\u7684\u8f6c\u6362\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u80fd\u591f\u907f\u514d\u82f1\u6587\u7b26\u53f7\u5e26\u6765\u7684\u56f0\u6270\uff0c\u8ba9\u7a0b\u5e8f\u66f4\u52a0\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u600e\u4e48\u5c06\u82f1\u6587\u7b26\u53f7\u8f6c\u4e2d\u6587\u7b26\u53f7\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>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u4eba\u4eec\u7684\u4ea4\u6d41\u65b9\u5f0f\u4e5f\u8d8a\u6765\u8d8a\u591a\u5143\u5316\u3002\u5728\u4f7f\u7528\u7535\u5b50\u90ae\u4ef6\u3001\u5fae\u4fe1\u3001\u77ed\u4fe1\u7b49\u5de5\u5177\u8fdb\u884c\u6c9f\u901a\u65f6\uff0c\u5e38\u5e38\u4f1a\u51fa\u73b0\u82f1\u6587\u7b26\u53f7\uff0c\u5982 \u201c@\u201d\u3001\u201c#\u201d\u3001\u201c&amp;\u201d\u7b49\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528\u4e2d\u6587\u8fdb\u884c\u4ea4\u6d41\u65f6\uff0c\u82f1\u6587\u7b26\u53f7\u5f80\u5f80\u4f1a\u7ed9\u4eba\u5e26\u6765\u56f0\u6270\u3002\u7279\u522b\u662f\u5728\u4f7f\u7528laravel\u8fd9\u6837\u7684\u5f00\u53d1\u6846\u67b6\u8fdb\u884c\u7f16\u7a0b\u65f6\uff0c\u82f1\u6587\u7b26\u53f7\u7684\u51fa\u73b0\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u548c\u7406\u89e3\u3002\u4e3a\u6b64\uff0claravel\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u597d\u7684\u89e3\u51b3\u65b9\u6848\u2014\u2014\u82f1\u6587\u7b26\u53f7\u8f6c\u4e2d\u6587\u7b26\u53f7\u3002 \u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53eb\u505a\u201cLaravel Localization\u201d\u7684\u7ec4\u4ef6\u6765\u5b9e\u73b0\u82f1\u6587\u7b26\u53f7\u8f6c\u4e2d\u6587\u7b26\u53f7\u3002Localization\uff08\u672c\u5730\u5316\uff09\u662f\u6307\u5c06\u7a0b\u5e8f\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u548c\u6587\u5316\u73af\u5883\u7684\u8fc7\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528Laravel Localization\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u82f1\u6587\u7b26\u53f7\u8f6c\u6362\u4e3a\u4e2d\u6587\u7b26\u53f7\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002 \u4e0b\u9762\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528Laravel Localization\u8fdb\u884c\u82f1\u6587\u7b26\u53f7\u5230\u4e2d\u6587\u7b26\u53f7\u7684\u8f6c\u6362\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Laravel Localization\u3002\u53ef\u4ee5\u901a\u8fc7Composer\u6765\u5b89\u88c5\uff1a composer require mcamara\/laravel-localization \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728 config\/app.php \u4e2d\u6ce8\u518cServiceProvider\uff1a &#8216;providers&#8217; =&gt; [ \/\/ &#8230; McamaraLaravelLocalizationLaravelLocalizationServiceProvider::class, ]; \u767b\u5f55\u540e\u590d\u5236 \u63a5\u7740\uff0c\u5728 config\/app.php \u4e2d\u6ce8\u518cFacades\uff1a &#8216;aliases&#8217; =&gt; [ \/\/ &#8230; &#8216;LaravelLocalization&#8217; =&gt; McamaraLaravelLocalizationFacadesLaravelLocalization::class, ]; \u767b\u5f55\u540e\u590d\u5236 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 routes.php \u6587\u4ef6\u4e2d\u5b9a\u4e49\u6240\u6709\u652f\u6301\u7684\u8bed\u8a00\uff1a Route::group([ &#8216;prefix&#8217; =&gt; LaravelLocalization::setLocale()], function () { \/\/ &#8230; }); \u767b\u5f55\u540e\u590d\u5236 \u5728\u5b9a\u4e49\u8bed\u8a00\u540e\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u8bed\u8a00\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u4fdd\u5b58\u4e86\u6240\u6709\u7684\u82f1\u6587\u7b26\u53f7\u4ee5\u53ca\u5bf9\u5e94\u7684\u4e2d\u6587\u7b26\u53f7\u3002\u5728Laravel Localization\u4e2d\uff0c\u8bed\u8a00\u6587\u4ef6\u901a\u5e38\u5b58\u50a8\u5728 resources\/lang \u76ee\u5f55\u4e0b\uff0c\u547d\u540d\u4e3a [&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-22487","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22487","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=22487"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22487\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}