• laravel5.2总结--本地化以及常量的使用


    1.本地化

    Laravel 的本地化功能提供方便的方法来获取多语言的字符串,让你的网站可以简单的支持多语言。
    语言包存放在 resources/lang 文件夹的文件里。每一个子目录应该对应一种语言
    最初的目录结构,里面包含了验证类的一些提示内容
     
    下面我们添加一个中文的语言文件,以test文件举例
    语言包简单地返回键值和字符串数组,例如:
    <?php return array(
    'first'=>'this is the first test' ,
    'second'=>'this is the second test'
    );
     
    接下来我们切换语言包,进入config/app.php 配置文件
    更改如下:
    'locale' => 'zh', //设置使用的语言包
    'fallback_locale' => 'en', //设置 "备用语言",它将会在当现有语言没有指定语句时被使用
     
    使用方法:
    使用 trans 辅助函数来获取语言字符串,trans 函数的第一个参数接受文件名和键值名称
    控制器中测试代码
    echo trans('test.first');
    echo '<br/>';
    //由于我们没有在zh文件夹下创建validation.php文件,所以在备用语言中寻找
    echo trans('validation.accepted');
    exit;
     
    结果如下:
    this is the first test
    The :attribute must be accepted.
     
    如果改动config/app.php 配置文件中的 'locale' => 'en',
    发现结果发现:
    test.first
    The :attribute must be accepted.
    扩展:
    如果需要,你也可以在语言包中(这里可以在test.php文件中)定义占位符,占位符使用 : 开头,
    'welcome' => 'Welcome, :name',
    接着,传入替代用的第二个参数给 trans 方法:
    echo trans('test.welcome', ['name' => 'Archer']);
     

    2.常量的使用

    在app/config文件夹下新建一个php文件,这里我们建立constants.php文件,内容如下
    <?php
    return array(
    //成交信息
    'FIRST' => '测试常量信息111',
    'SECOND' => array('NEXT' => '测试常量信息第二层')
    );
     
    使用方法
    echo Config::get('constants.FIRST');
    echo Config::get('constants.SECOND.NEXT');
    或者
    echo config(sprintf('constant.%s', 'FIRST'), null);
    echo config(sprintf('constant.%s', 'SECOND.NEXT'), null);
  • 相关阅读:
    Netty应用
    原生JDK网络编程- NIO之Reactor模式
    Kafka入门教程
    Java CAS
    Java读写锁
    遍历map的四种方法
    java selector
    Range Sum Query
    Increasing Triplet Subsequence
    Count Numbers with Unique Digits
  • 原文地址:https://www.cnblogs.com/redirect/p/7457024.html
Copyright © 2020-2023  润新知