[英]Laravel using translations inside translations
假設我在App\resources\lang\en\validation.php
中有以下翻譯:
'different' => 'The attribute "' . __('strings.:attribute') . '" cannot be the same as the ":other" attribute',
我的問題在這里:
__('strings.:attribute')
這個就不翻譯了。 如果我手動執行以下操作來測試:
__('strings.something')
翻譯確實有效。 我的問題是,如何讓它與__('strings.:attribute')
一起工作,為什么它不像我嘗試的那樣工作?
您必須記住,有兩種不同的情況:
當框架實際需要您的翻譯時
當實際使用翻譯字符串並替換屬性時
屬性需要是要替換的結果字符串的一部分,它們不能是您的 php 代碼的一部分
所以像:
__('strings.:attribute')
不能工作。
如果您想做的是重命名驗證消息中的屬性,您可以使用 laravel 來實現:
驗證.php
'different' => 'The attribute ":attribute" cannot be the same as the ":other" attribute',
'attributes' => [
// your attributes
'something' => 'MySomething'
]
您將遇到如下所示的錯誤:
{"something.different": "The attribute \"MySomething\" cannot be the same as the \"MyOther\" attribute"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.