1. When the locale is en and the validated parameter does not exist, the response is: The selected theme id is invalid. See Figure 1.

{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorSessionCreate].",
"extensions": {
"validation": {
"themeId": [
"The selected theme id is invalid."
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorSessionCreate"
],
"trace": ...
}
],
"data": {
"onlineStoreThemeEditorSessionCreate": null
}
}
2. When the locale is zh_CN and the validated parameter does not exist, the response is: 所选的theme id无效。 The expected response is: 所选的主题ID无效。 See Figure 2.

{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorSessionCreate].",
"extensions": {
"validation": {
"themeId": [
"所选的theme id无效。"
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorSessionCreate"
],
"trace": ...
}
],
"data": {
"onlineStoreThemeEditorSessionCreate": null
}
}3. Review the GraphQL code implementation: /Modules/ThemeStore/Resources/graphql/theme_editor.graphql
extend type Mutation {
"创建主题编辑会话"
onlineStoreThemeEditorSessionCreate(themeId: ID! @rules(apply: ["exists:theme_asset,theme_id"])): ThemeEditorSessionCreatePayload @field(resolver: "Modules\\ThemeStore\\Resolver\\ThemeEditor\\CreateThemeEditorSessionResolver")
}
type ThemeEditorSessionCreatePayload
{
"会话"
session: Session!
}
4. Check the language file /Modules/ThemeStore/Resources/lang/zh_CN/validation.php. A custom attribute name for theme_id has been specified, but it does not take effect.
<?php
return [
'attributes' => [
'theme_id' => '主题ID',
],
];
5. Even if this configuration is placed in the language file /resources/lang/zh_CN/validation.php, it still does not take effect. The response is: 所选的theme_id无效。 However, this language file’s configuration is used by default. When the validation rule message is placed in the attributes key of validation.php for any language, the :attribute portion of the validation message is not translated (in the module context).
<?php
return [
'exists' => '所选的:attribute无效。',
'attributes' => [
'theme_id' => '主题ID',
],
];6. In Laravel v8.7.90 and nwidart/laravel-modules 8.2.0, editing /resources/lang/zh_CN/validation.php makes the attributes take effect.
/**
* 显示创建博客文章的表单
*
* @return Response
*/
public function create(Request $request)
{
$validator = Validator::make($request->all(), [
'theme_id' => 'required|exists:users,id',
]);
if ($validator->fails()) {
dd($validator->errors());
}
// 保存博客文章…
return view('blog::post.create');
}
<?php
return [
'exists' => '所选的:attribute无效。resources',
'attributes' => [
'theme_id' => '主题ID',
],
];Illuminate\Support\MessageBag {#361 ▼
#messages: array:1 [▼
"theme_id" => array:1 [▼
0 => "所选的主题ID无效。resources"
]
]
#format: ":message"
}
7. I decided to replicate the implementation from step 6 in the current versions: Laravel v6.20.43 and nwidart/laravel-modules 7.3.0. This was to determine whether the issue of attributes not taking effect in the application’s root directory was related to the framework or module package versions. I confirmed that they are unrelated. Therefore, the cause lies in LightHouse 5.
<?php
return [
'array' => ':attribute必须是一个数组。',
'attributes' => [
'filter' => '过滤器'
],
];
{
"message": "The given data was invalid.",
"errors": {
"filter": [
"过滤器必须是一个数组。"
]
},
"status_code": 422,
"debug": {
"line": 385,
"file": "E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Validation\\Validator.php",
"class": "Illuminate\\Validation\\ValidationException",
"trace": ...
}
}
8. Reference: https://github.com/nuwave/lighthouse/issues/1773 . Although translation functionality has been implemented for the @rules directive, the implementation is not thorough. Specifically, validation attributes are not supported. The developer recommends: https://lighthouse-php.com/master/security/validation.html#validator-classes . This supports defining rules via arbitrary PHP code.
9. I am not going to write a custom rule for now. I have decided to temporarily accept that attribute names are not translated into the corresponding language in non-English environments. Additionally, the language file remains in the root directory rather than in the corresponding module directory.
