简体   繁体   English

如何使用您的打包语言覆盖Laravel的语言文件

[英]How to override Laravel's language files with your package language

I am trying to override the default laravel english language package under resources/lang/en with my languages from my package. 我试图用我的包中的语言覆盖resources/lang/en下的默认laravel英语包。

The package just publishes its own language files to eg resources/lang/vendor/*package-name*/de and has the same files in it as the laravel standard lang file like validation.php or auth.php . 软件包仅将其自己的语言文件发布到resources/lang/vendor/*package-name*/de中,并且与laravel标准lang文件(如validation.phpauth.php具有相同的文件。

Is there a way to tell Laravel to use these package translations? 有没有办法告诉Laravel使用这些软件包翻译?

From the documentation , 文档中

Package translation files are typically referenced using a double-colon syntax. 软件包翻译文件通常使用双冒号语法引用。

While loading your translations in your package's Service Provider's boot method, you need to tell Laravel about where it's located. 在使用程序包的服务提供商的boot方法加载翻译内容时,您需要告知Laravel它的位置。 So let's say your package's name is acme , you would have something like this. 因此,假设您的包裹名称为acme ,那么您将得到类似的内容。

public function boot()
{
    $this->loadTranslationsFrom(__DIR__.'/path/to/translations', 'acme');
}

Then to load the translation, you would do something like this: 然后加载翻译,您将执行以下操作:

trans('acme::file.line');

Edit: If you want to provide Custom Validation Messages, you would need to add a messages() method in your Form Request . 编辑:如果要提供自定义验证消息,则需要在Form Request中添加messages()方法。 For example: 例如:

public function messages() 
{
    return [
        'field' => trans('acme::validation.field')
    ];
}

If you are manually creating Validator instances, you could just do this: 如果要手动创建Validator实例,则可以执行以下操作:

$messages = [
    'field' => trans('acme::validation.field')
];

$validator = Validator::make($input, $rules, $messages);

Edit: Since resources/lang/en/validation.php contains an array of key value pairs, you could just replace the contents of the file with this: 编辑:由于resources/lang/en/validation.php包含一个键值对数组,因此您可以使用以下内容替换文件的内容:

<?php

return trans('acme::validation');

and assuming that your package's validation file is returning an array of key-value pairs, that should do the trick. 并假设您包的验证文件返回的是键值对数组,那么就可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM