修改Yii2的默认语言language为中文zh-CN的方法

如果用的语言是en-US的话,网页加载速度很慢,因为要加载国外镜像或者链接什么的,使用zh-CN就直接加载国内链接或者CDN,速度比较快;

---------------------------------------------------------------------------------------------------

在开发Yii2的情况下,有时候遇到错误的情况下(例如model下面的错误提示信息),

希望提示信息默认为中文,这个时候就需要修改Yii2的默认语言,最直观的方式是:

//Yii2的默认的配置文件里面
$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'language' => 'zh-CN',       //全局设置为中文--推荐
 
]

如果只针对局部的某一个控制器和方法下的话可以使用如下的方式

//也可以在外部设置
//Yii::$app->language = 'zh-CN';     //设定为使用中文 
 
public function actionIndex(){   
 
    Yii::$app->language = 'en';          //设定语言为英文 
 
    Yii::$app->language = 'zh-CN';       //设定为使用中文 
     
    echo Yii::t('app', 'Goodbye_flag');
 
    echo Yii::$app->language;            //得到当前的语言
}

附上yii2的默认的错误提示语中文 yiisoft\yii2\message\zh-CN\yii.php

return [
    '(not set)' => '(未设置)',
    'An internal server error occurred.' => '服务器内部错误。',
    'Are you sure you want to delete this item?' => '您确定要删除此项吗?',
    'Delete' => '删除',
    'Error' => '错误',
    'File upload failed.' => '文件上传失败。',
    'Home' => '首页',
    'Invalid data received for parameter "{param}".' => '"{param}"参数接收到无效的数据。',
    'Login Required' => '需要登录',
    'Missing required arguments: {params}' => '函数缺少参数:{params}',
    'Missing required parameters: {params}' => '缺少参数:{params}',
    'No' => '否',
    'No help for unknown command "{command}".' => '命令"{command}"发生未知的错误。',
    'No help for unknown sub-command "{command}".' => '子命令"{command}"发生未知的错误。',
    'No results found.' => '没有找到数据。',
    'Only files with these extensions are allowed: {extensions}.' => '只允许使用以下文件扩展名的文件:{extensions}。',
    'Page not found.' => '页面未找到。',
    'Please fix the following errors:' => '请修复以下错误',
    'Please upload a file.' => '请上传一个文件。',
    'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.' => '第<b>{begin, number}-{end, number}</b>条,共<b>{totalCount, number}</b>条数据.',
    'The file "{file}" is not an image.' => '文件 "{file}" 不是一个图像文件。',
    'The file "{file}" is too big. Its size cannot exceed {limit, number} {limit, plural, one{byte} other{bytes}}.' => '文件"{file}"太大。它的大小不能超过{limit, number}字节。',
    'The file "{file}" is too small. Its size cannot be smaller than {limit, number} {limit, plural, one{byte} other{bytes}}.' => '该文件"{file}"太小。它的大小不得小于{limit, number}字节。',
    'The format of {attribute} is invalid.' => '属性 {attribute} 的格式无效。',
    'The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '图像"{file}"太大。他的高度不得超过{limit, number}像素。',
    'The image "{file}" is too large. The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '图像"{file}"太大。他的宽度不得超过{limit, number}像素。',
    'The image "{file}" is too small. The height cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '图像"{file}"太小。他的高度不得小于{limit, number}像素。',
    'The image "{file}" is too small. The width cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '图像"{file}"太小。他的宽度不得小于{limit, number}像素。',
    'The verification code is incorrect.' => '验证码不正确。',
    'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.' => '总计<b>{count, number}</b>条数据。',
    'Unable to verify your data submission.' => '您提交的数据无法被验证。',
    'Unknown command "{command}".' => '未知的命令 "{command}"。',
    'Unknown option: --{name}' => '未知的选项:--{name}',
    'Update' => '更新',
    'View' => '查看',
    'Yes' => '是',
    'You are not allowed to perform this action.' => '您没有执行此操作的权限。',
    'You can upload at most {limit, number} {limit, plural, one{file} other{files}}.' => '您最多上传{limit, number}个文件。',
    'the input value' => '该输入',
    '{attribute} "{value}" has already been taken.' => '{attribute}的值"{value}"已经被占用了。',
    '{attribute} cannot be blank.' => '{attribute}不能为空。',
    '{attribute} is invalid.' => '{attribute}是无效的。',
    '{attribute} is not a valid URL.' => '{attribute}不是一条有效的URL。',
    '{attribute} is not a valid email address.' => '{attribute}不是有效的邮箱地址。',
    '{attribute} must be "{requiredValue}".' => '{attribute}必须为"{requiredValue}"。',
    '{attribute} must be a number.' => '{attribute}必须是一个数字。',
    '{attribute} must be a string.' => '{attribute}必须是一条字符串。',
    '{attribute} must be an integer.' => '{attribute}必须是整数。',
    '{attribute} must be either "{true}" or "{false}".' => '{attribute}的值必须要么为"{true}",要么为"{false}"。',
    '{attribute} must be equal to "{compareValueOrAttribute}".' => '{attribute}的值必须等于"{compareValueOrAttribute}"。',
    '{attribute} must not be equal to "{compareValueOrAttribute}".' => '{attribute}的值不得等于"{compareValueOrAttribute}"。',
    '{attribute} must be greater than "{compareValueOrAttribute}".' => '{attribute}的值必须大于"{compareValueOrAttribute}"。',
    '{attribute} must be greater than or equal to "{compareValueOrAttribute}".' => '{attribute}的值必须大于或等于"{compareValueOrAttribute}"。',
    '{attribute} must be less than "{compareValueOrAttribute}".' => '{attribute}的值必须小于"{compareValueOrAttribute}"。',
    '{attribute} must be less than or equal to "{compareValueOrAttribute}".' => '{attribute}的值必须小于或等于"{compareValueOrAttribute}"。',
    '{attribute} must be no greater than {max}.' => '{attribute}的值必须不大于{max}。',
    '{attribute} must be no less than {min}.' => '{attribute}的值必须不小于{min}。',
    '{attribute} must be repeated exactly.' => '{attribute}必须重复。',
    '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.' => '{attribute}应该包含至少{min, number}个字符。',
    '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.' => '{attribute}只能包含至多{max, number}个字符。',
    '{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.' => '{attribute}应该包含{length, number}个字符。',
    'in {delta, plural, =1{a year} other{# years}}' => '{delta}年后',
    'in {delta, plural, =1{a month} other{# months}}' => '{delta}个月后',
    'in {delta, plural, =1{a day} other{# days}}' => '{delta}天后',
    'in {delta, plural, =1{an hour} other{# hours}}' => '{delta}小时后',
    'in {delta, plural, =1{a minute} other{# minutes}}' => '{delta}分钟后',
    'in {delta, plural, =1{a second} other{# seconds}}' => '{delta}秒后',
    '{delta, plural, =1{a year} other{# years}} ago' => '{delta}年前',
    '{delta, plural, =1{a month} other{# months}} ago' => '{delta}个月前',
    '{delta, plural, =1{a day} other{# days}} ago' => '{delta}天前',
    '{delta, plural, =1{an hour} other{# hours}} ago' => '{delta}小时前',
    '{delta, plural, =1{a minute} other{# minutes}} ago' => '{delta}分钟前',
    'just now' => '刚刚',
    '{delta, plural, =1{a second} other{# seconds}} ago' => '{delta}秒前',
];

 

转载于:https://www.cnblogs.com/jerrypro/p/6704420.html

Yii 2.0 权威指南 本教程的发布遵循 Yii 文档使用许可. 版权所有 2014 (c) Yii Software LLC. 介绍 已定稿 关于 Yii 已定稿 从 Yii 1.1 升级 入门 已定稿 安装 Yii 已定稿 运行应用 已定稿 第一次问候 已定稿 使用 Forms 已定稿 玩转 Databases 已定稿 用 Gii 生成代码 已定稿 更上一层楼 应用结构 已定稿 结构概述 已定稿 入口脚本 已定稿 应用 已定稿 应用组件 已定稿 控制器(Controller) 已定稿 视图(View) 已定稿 模型(Model) 已定稿 过滤器 已定稿 小部件(Widget) 已定稿 模块(Module) 已定稿 前端资源(Asset) 已定稿 扩展(extensions) 请求处理 已定稿 运行概述 已定稿 引导(Bootstrapping) 已定稿 路由(Route)引导与创建 URL 已定稿 请求(Request) 已定稿 响应(Response) 已定稿 Sessions(会话)和 Cookies 已定稿 错误处理 已定稿 日志 关键概念 已定稿 组件(Component) 已定稿 属性(Property) 已定稿 事件(Event) 已定稿 行为(Behavior) 已定稿 配置(Configurations) 已定稿 类自动加载(Autoloading) 已定稿 别名(Alias) 已定稿 服务定位器(Service Locator) 已定稿 依赖注入容器(DI Container) 配合数据库工作 编撰中 数据访问对象(DAO) - 数据库连接、基本查询、事务和模式操作 编撰中 查询生成器(Query Builder) - 使用简单抽象层查询数据库 编撰中 活动记录(Active Record) - 活动记录对象关系映射(ORM),检索和操作记录、定义关联关系 编撰中 数据库迁移(Migration) - 在团体开发中对你的数据库使用版本控制 待定中 Sphinx 待定中 Redis 待定中 MongoDB 待定中 ElasticSearch 接收用户数据 编撰中 创建表单 已定稿 输入验证 编撰中 文件上传 待定中 多模型同时输入 显示数据 编撰中 格式化输出数据 待定中 分页(Pagination) 待定中 排序(Sorting) 编撰中 数据提供器 编撰中 数据小部件 编撰中 主题 安全 编撰中 认证(Authentication) 编撰中 授权(Authorization) 编撰中 处理密码 待定中 客户端认证 待定中 安全领域的最佳实践 缓存 已定稿 概述 已定稿 数据缓存 已定稿 片段缓存 已定稿 分页缓存 已定稿 HTTP 缓存 RESTful Web 服务 已定稿 快速入门 已定稿 资源 已定稿 路由 已定稿 格式化响应 已定稿 授权验证 已定稿 速率限制 已定稿 版本化 已定稿 错误处理 已定稿 测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值