I've built a validation package for Laravel >= 5.4 and it uses JSON translation files for the validation message .
I want to let the application to override that message translations.
Laravel provides namespacing for trans()
. However, It looks like only the regular .php
translations allow namespacing.
I haven't been able to achieve this with JSON translations.
Currently, this is how I'm returning the validation message :
public function message($message, $attribute, $rule, $parameters)
{
return __('The domain for :attribute is not allowed. Please use another email address.', ['attribute' => $attribute]);
}
This is how I'm loading the translation files in Service Provider :
public function boot()
{
$this->loadJSONTranslationsFrom(__DIR__.'/../../../lang', 'email-domain-blacklist');
$this->publishes([
__DIR__.'/../../../lang' => resource_path('lang/vendor/email-domain-blacklist'),
]);
$this->publishConfig();
// Add custom validation rules
Validator::extend('blacklist', "Alariva\EmailDomainBlacklist\Validator@validate");
// Add custom validation messages
Validator::replacer('blacklist', "Alariva\EmailDomainBlacklist\Validator@message");
}
If I publish the translation files and alter them under resources/lang/vendor/email-domain-blacklist/*.json
I will see no changes, since the package will load the translation from the package directory inside vendor/
.
If I loaded the translations from the app level, I would require the application to always publish the translation files in order to correctly load them. I want to avoid this extra step.
Which would be an elegant way to go around this?
I will edit accordingly to provide as much clear info as possible.
Thanks in advance.
After some research I found the way to go around this that Laravel proposes.
Key overrides in the main project JSON translation file (not in the lang/vendor
, but directly in lang/
) will take precedence.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.