<article><h1>Laravel 的本地化功能</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#defining-translation-strings">定义翻译语句</a><ul><li><a href="#using-short-keys">使用短键</a></li><li><a href="#using-translation-strings-as-keys">使用翻译语句作为键</a></li></ul></li><li><a href="#retrieving-translation-strings">提取翻译语句</a><ul><li><a href="#replacing-parameters-in-translation-strings">翻译语句中的参数替换</a></li><li><a href="#pluralization">复数</a></li></ul></li><li><a href="#overriding-package-language-files">重写扩展包的语言包</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Laravel 的本地化功能提供方便的方法来获取多语言的字符串,让你的网站可以简单的支持多语言。</p><p>语言包存放在 <code class=" language-php">resources<span class="token operator">/</span>lang</code> 目录下的文件里。在此目录中应该有应用对应支持的语言并将其对应到每一个子目录:</p><pre class=" language-php"><code class=" language-php"><span class="token operator">/</span>resources
<span class="token operator">/</span>lang
<span class="token operator">/</span>en
messages<span class="token punctuation">.</span>php
<span class="token operator">/</span>es
messages<span class="token punctuation">.</span>php</code></pre><p>语言包简单地返回键值和字符串数组,例如:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span>
<span class="token string">'welcome'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Welcome to our application'</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span></code></pre><h3>切换语言</h3><p>应用的默认语言保存在 <code class=" language-php">config<span class="token operator">/</span>app<span class="token punctuation">.</span>php</code> 配置文件中。当然,你可以根据需求自由的修改当前设置,可以使用 <code class=" language-php">App</code> facade 的 <code class=" language-php">setLocale</code> 方法动态地更改现有语言:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'welcome/{locale}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">setLocale<span class="token punctuation">(</span></span><span class="token variable">$locale</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以设置 「备用语言」 ,它将会在当现有语言没有指定语句时被使用。就像默认语言那样,备用语言也可以在 <code class=" language-php">config<span class="token operator">/</span>app<span class="token punctuation">.</span>php</code> 配置文件设置:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'fallback_locale'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'en'</span><span class="token punctuation">,</span></code></pre><h4>指定当前语言</h4><p>你可以使用 <code class=" language-php">App</code> facade 的 <code class=" language-php">getLocale</code> 及 <code class=" language-php">isLocale</code> 方法指定当前的语言环境或者检验当前语言是否是给定的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$locale</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">getLocale<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token scope">App<span class="token punctuation">::</span></span><span class="token function">isLocale<span class="token punctuation">(</span></span><span class="token string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><p><a name="defining-translation-strings"></a></p><h2><a href="#defining-translation-strings">定义翻译语句</a></h2><p><a name="using-short-keys"></a></p><h3>使用短键</h3><p>通常,语言包存放在 <code class=" language-php">resources<span class="token operator">/</span>lang</code> 目录下的文件里。在此目录中应该有应用对应支持的语言并将其对应到每一个子目录:</p><pre class=" language-php"><code class=" language-php"><span class="token operator">/</span>resources
<span class="token operator">/</span>lang
<span class="token operator">/</span>en
messages<span class="token punctuation">.</span>php
<span class="token operator">/</span>es
messages<span class="token punctuation">.</span>php</code></pre><p>语言包简单地返回键值和字符串数组,例如:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token comment" spellcheck="true">
// resources/lang/en/messages.php
</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span>
<span class="token string">'welcome'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Welcome to our application'</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span></code></pre><p><a name="using-translation-strings-as-keys"></a></p><h3>使用翻译语句作为键</h3><p>对于有大量翻译需求的应用, 如果每一句翻译语句都使用 「短键」 来定义,那么当你在视图中尝试去引用这些 「短键」 的时候,很容易变得混乱,分不清哪个对应哪个。因此,Laravel 也提供支持使用 「默认语言」 的翻译语句作为键,来定义其他语言的翻译语句。</p><p>使用翻译语句作为键的语言包需要在 <code class=" language-php">resources<span class="token operator">/</span>lang</code> 目录下保存为 JSON 文件。例如,如果你的应用中有西班牙语的语言包,你应该新建一个 <code class=" language-php">resources<span class="token operator">/</span>lang<span class="token operator">/</span>es<span class="token punctuation">.</span>json</code> 文件:</p><pre class=" language-php"><code class=" language-php"><span class="token punctuation">{</span>
<span class="token string">"I love programming."</span><span class="token punctuation">:</span> <span class="token string">"Me encanta la programación."</span>
<span class="token punctuation">}</span></code></pre><p><a name="retrieving-translation-strings"></a></p><h2><a href="#retrieving-translation-strings">获取翻译语句</a></h2><p>你可以使用 <code class=" language-php"><span class="token constant">__</span></code> 辅助函数来获取翻译语句,<code class=" language-php"><span class="token constant">__</span></code> 方法接受文件名和键值作为其第一个参数。例如,让我们提取 <code class=" language-php">resources<span class="token operator">/</span>lang<span class="token operator">/</span>messages<span class="token punctuation">.</span>php</code> 中的 <code class=" language-php">welcome</code> :</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">__<span class="token punctuation">(</span></span><span class="token string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token function">__<span class="token punctuation">(</span></span><span class="token string">'I love programming.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>当然,如果你使用 <a href="/docs/5.4/blade">Blade 模板引擎</a>, 那么你可以在视图文件中使用 <code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code> 语法或者使用 <code class=" language-php">@lang</code> 指令来输出语句:</p><pre class=" language-php"><code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">__<span class="token punctuation">(</span></span><span class="token string">'messages.welcome'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>
@<span class="token function">lang<span class="token punctuation">(</span></span><span class="token string">'messages.welcome'</span><span class="token punctuation">)</span></code></pre><p>如果指定的语句不存在,<code class=" language-php"><span class="token constant">__</span></code> 方法则会简单的返回这个键名。所以,如果上述示例中的键不存在,那么 <code class=" language-php"><span class="token constant">__</span></code> 方法则会返回 <code class=" language-php">messages<span class="token punctuation">.</span>welcome</code> 。</p><p><a name="replacing-parameters-in-translation-strings"></a></p><h3>翻译语句中的参数替换</h3><p>如果需要,你也可以在翻译语句中定义占位符。所有的占位符都使用的 <code class=" language-php"><span class="token punctuation">:</span></code> 开头。例如,你可以自定义一则欢迎消息的占位符:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'welcome'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Welcome, :name'</span><span class="token punctuation">,</span></code></pre><p>你可以在 <code class=" language-php"><span class="token constant">__</span></code> 方法中传递一个数组作为第二个参数,它会将数组的值替换到语言内容的占位符中:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">__<span class="token punctuation">(</span></span><span class="token string">'messages.welcome'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果你的占位符中包含了首字母大写或者全体大写,翻译过来的内容也会相应的做相应的处理:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'welcome'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Welcome, :NAME'</span><span class="token punctuation">,</span><span class="token comment" spellcheck="true"> // Welcome, DAYLE
</span><span class="token string">'goodbye'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Goodbye, :Name'</span><span class="token punctuation">,</span><span class="token comment" spellcheck="true"> // Goodbye, Dayle</span></code></pre><p><a name="pluralization"></a></p><h3>复数</h3><p>复数是个复杂的问题,不同语言对于复数有不同的规则。使用管道符 <code class=" language-php"><span class="token operator">|</span></code> ,可以区分单复数字符串格式:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'apples'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'There is one apple|There are many apples'</span><span class="token punctuation">,</span></code></pre><p>你甚至可以创建使用更复杂的复数规则,例如根据数量的范围不同来指定不同翻译语句:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'apples'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'{0} There are none|[1,19] There are some|[20,*] There are many'</span><span class="token punctuation">,</span></code></pre><p>当你定义完复数语句条件的时候,你可以使用 <code class=" language-php">trans_choice</code> 方法来设置「总数」以获取符合对应条件的复数翻译语句。例如,在这个例子中,设置「总数」为 10 ,符合数量范围 1 至 19,所以会得到 <code class=" language-php">There are some</code> 这条复数语句:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">trans_choice<span class="token punctuation">(</span></span><span class="token string">'messages.apples'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="overriding-package-language-files"></a></p><h2><a href="#overriding-package-language-files">重写扩展包的语言包</a></h2><p>部分扩展包带有自己的语言包,你可以通过在 <code class=" language-php">resources<span class="token operator">/</span>lang<span class="token operator">/</span>vendor<span class="token operator">/</span><span class="token punctuation">{</span>package<span class="token punctuation">}</span><span class="token operator">/</span><span class="token punctuation">{</span>locale<span class="token punctuation">}</span></code> 放置文件来重写它们,而不是直接修改扩展包的核心文件。</p><p>例如,你需要重写 <code class=" language-php">skyrim<span class="token operator">/</span>hearthfire</code> 扩展包的英文语言包 <code class=" language-php">messages<span class="token punctuation">.</span>php</code> ,则需要把文件放置在 <code class=" language-php">resources<span class="token operator">/</span>lang<span class="token operator">/</span>vendor<span class="token operator">/</span>hearthfire<span class="token operator">/</span>en<span class="token operator">/</span>messages<span class="token punctuation">.</span>php</code> 。在这个文件中定义你想要重写的翻译语句,所有没有重写的语句将会加载扩展包的语言包中原来的语句。</p></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度