Home > Backend Development > PHP Tutorial > laravel5 switch Chinese language

laravel5 switch Chinese language

不言
Release: 2023-03-25 18:30:02
Original
2664 people have browsed it

This article mainly introduces the switching of Chinese language in laravel5. It has certain reference value. Now I share it with you. Friends in need can refer to it

Laravel-lang

Laravel 5 Language pack, including 52 languages, based on caouecs/Laravel-lang.

Installation

composer require "overtrue/laravel-lang:~3.0"
Copy after login

Laraval 5.*

After completing the above operation, copy the project file ## Replace the next line <div class="code" style="position:relative; padding:0px; margin:0px;"><pre style="font-family:'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.6px;margin-bottom:0px;padding-top:16px;padding-bottom:16px;line-height:1.45;background-color:rgb(246,248,250);" class="brush:php;toolbar:false;">Illuminate\Translation\TranslationServiceProvider::class,</pre><div class="contentsignin">Copy after login</div></div> in #config/app.php

with:

Overtrue\LaravelLang\TranslationServiceProvider::class,
Copy after login
Lumen

in

bootstrap/app.php Add the following line:

$app->register(Overtrue\LaravelLang\TranslationServiceProvider::class);
Copy after login
Configuration

Laravel

Modify the project language

config/app.php

&#39;locale&#39; => &#39;zh-CN&#39;,
Copy after login

Lumen

Modify the language in the

.env file:

APP_LOCALE=zh-CN
Copy after login

使用

和正常使用一样,你如果需要额外添加语言项,请在 resources/lang/zh-CN/ 下建立你自己的文件即可,也可以建立同样的文件来替换掉默认的语言部分。

添加自定义语言项

例如创建文件 resources/lang/zh-CN/demo.php:

<?php
return [    &#39;user_not_exists&#39;    => &#39;用户不存在&#39;,    &#39;email_has_registed&#39; => &#39;邮箱 :email 已经注册过!&#39;,];
Copy after login

然后在任何地方:

echo trans(&#39;demo.user_not_exists&#39;); // 用户不存在
echo trans(&#39;demo.email_has_registed&#39;, [&#39;email&#39; => &#39;anzhengchao@gmail.com&#39;]);// 邮箱 
anzhengchao@gmail.com 已经注册过!
Copy after login

替换掉默认的语言项

我们假设想替换掉密码重围成功的提示文字为例,创建 resources/lang/zh-CN/passwords.php:

<?php
return [    &#39;reset&#39; => &#39;您的密码已经重置成功了,你可以使用新的密码登录了!&#39;,];
Copy after login

只放置你需要替换的部分即可。

将翻译文件拷贝到你的项目 resources/lang/ 目录下:

$ php artisan lang:publish [LOCALES] {--force}
Copy after login

examples:

$ php artisan lang:publish zh-CN,zh-HK,th,tk
Copy after login

以上就是本篇文章的全部内容了,更多相关内容请关注PHP中文网。

The above is the detailed content of laravel5 switch Chinese language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template