aligurbuz / lingua
此包的最新版本(v1.07)没有可用的许可信息。
v1.07
2018-08-08 12:36 UTC
Requires
- symfony/finder: ^4.0
- symfony/yaml: 2.*
This package is not auto-updated.
Last update: 2024-09-22 17:46:26 UTC
README
轻松集成和管理您的语言文件。
简介
这是一个包,您可以使用yaml格式广泛管理语言文件。在这个包下,您可以轻松地将语言文件包含到应用程序代码中,如包使用说明中定义的那样。
安装
$ composer require aligurbuz/lingua
使用方法
包含您的Composer供应商自动加载文件,并按以下方式使用Lingua类。
require_once '../vendor/autoload.php'; use Lingua\Lingua;
数据检索
包含您的Composer供应商自动加载文件,并按以下方式使用Lingua类。分配给Lingua类构造函数对象的目录路径是您的语言文件的主要路径。如果您没有指定locale方法,则“en”目录将自动附加到主要路径。
//it is set to the as "en" value of default locale in the langdir directory. $lang=(new Lingua('path/to/langDir')); //It prints all the keys in message.yaml (in that path/to/langDir/en) echo $lang->get('message'); //it is set to the as fr in the langdir directory. //It prints all the keys in message.yaml (in that path/to/langDir/fr) echo $lang->locale('fr')->get('message'); //It prints key called foo in message.yaml (in that path/to/langDir/en) echo $lang->get('message.foo'); //It prints keys specified in array as parameters in message.yaml (in that path/to/langDir/en) echo $lang->get('message',['foo','bar']); //if specified exclude as key in array as parameters in message.yaml (in that path/to/langDir/en) //In this case, the strings in the exclude array are removed from the called file. echo $lang->get('message',['exclude'=>['foo']);
有时在语言文件中搜索一个键时,您可能希望确定它是否也包含在自动包含的文件中。如果调用键在指定文件中不存在,它将检查它是否在自动包含的文件中自动存在。实际上,lingua将在这里自动合并这两个文件。您要查找的键将在单个数组中搜索。
//It prints all the keys in message.yaml and default.yaml (in that path/to/langDir/en) echo $lang->include(['default'])->get('message'); //It prints key that in default.yaml if there is no key specified in message.yaml (in that path/to/langDir/en) echo $lang->include(['default'])->get('message.foo');
此外,在某些情况下,您可能希望自动上传的文件位于目录中。在这种情况下,您可以使用includeDir方法,如您所见,搜索数组将包括目录中的所有文件。
//It prints key that in both default.yaml and all files in load directory if there is no key specified in message.yaml (in that path/to/langDir/en) echo $lang->includeDir(['load'])->include(['default'])->get('message.foo');