iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 20
0
Modern Web

如何用Laravel寫一個簡單的部落格網站系列 第 20

[Day 20] 建立註冊的畫面及功能(四) - 錯誤訊息語系設定

  • 分享至 

  • twitterImage
  •  

昨天我們看到,
Laravel預設是顯示英文,
我們現在假設要看這個網站的人是台灣人,
習慣看繁體中文,
所以我們要改成顯示繁體中文的錯誤提示.

我們找到 config/app.php 這個檔案,
相關的設定在這個檔案裏面,
我們看到裡面包含以下內容,
可以看到預設的時區是UTC,
預設的語系是英文.

return [
    //時區
    'timezone' => 'UTC',
    //語系
    'locale' => 'en',
    //語系設定失敗時的預設語系
    'fallback_locale' => 'en',
];

我們將時區改成Asia/Taipei(台北時間),
語系改成zh-TW(繁體中文).

return [
    //時區
    'timezone' => 'Asia/Taipei',
    //語系
    'locale' => 'zh-TW',
    //語系設定失敗時的預設語系
    'fallback_locale' => 'zh-TW',
];

除此之外,
我們還要定義語系的內容,
Laravel語系檔案設定都放在resources/lang/目錄中,
我們把語系設定為zh-TW繁體中文的時候,
我們必須在語系目錄下新增對應的資料夾名稱zh-TW,
而表單驗證的內容是在validation.php裡面,
https://ithelp.ithome.com.tw/upload/images/20200929/20105694yktsVFdFuR.png

我們把resources/lang/en/validation.php的內容複製到resources/lang/zh-TW/validation.php,
然後修改內容如下

<?php

return [
    'accepted' => '必須接受 :attribute。',
    'active_url' => ':attribute 並非一個有效的網址。',
    'after' => ':attribute 必須要在 :date 之後。',
    'after_or_equal' => ':attribute 必須要在 :date 之後或相等。',
    'alpha' => ':attribute 只能以字母組成。',
    'alpha_dash' => ':attribute 只能以字母、數字及斜線組成。',
    'alpha_num' => ':attribute 只能以字母及數字組成。',
    'array' => ':attribute 必須為陣列。',
    'before' => ':attribute 必須要在 :date 之前。',
    'before_or_equal' => ':attribute 必須要在 :date 之前或相等。',
    'between' => [
        'numeric' => ':attribute 必須介於 :min 和 :max 之間。',
        'file' => ':attribute 必須介於 :min 和 :max KB 之間。',
        'string' => ':attribute 必須介於 :min 和 :max 個字元 之間。',
        'array' => ':attribute 必須介於 :min 和 :max 個元素 之間。',
    ],
    'boolean' => ':attribute 必須為布林值。',
    'confirmed' => ':attribute 確認欄位的輸入並不相符',
    'date' => ':attribute 並非一個有效的日期。',
    'date_equals' => ':attribute 必須和 :date 相等。',
    'date_format' => ':attribute 與 :format 格式不相符。',
    'different' => ':attribute 與 :other 必須不同。',
    'digits' => ':attribute 必須是 :digits 位數字。',
    'digits_between' => ':attribute 必須介於 :min 至 :max 位數字。',
    'dimensions' => ':attribute 不符合圖片尺寸',
    'distinct' => ':attribute 已經存在。',
    'email' => ':attribute 不是有效的E-mail格式',
    'ends_with' => ':attribute 必須以 :values 作為結束。',
    'exists' => '所選擇的 :attribute 選項無效',
    'file' => ':attribute 必須是一個檔案。',
    'filled' => ':attribute 不能為空。',
    'gt' => [
        'numeric' => ':attribute 必須大於 :value。',
        'file' => ':attribute 必須大於 :value KB。',
        'string' => ':attribute 必須大於 :value 個字元。',
        'array' => ':attribute 必須大於 :value 個元素。',
    ],
    'gte' => [
        'numeric' => ':attribute 必須大於等於 :value。',
        'file' => ':attribute 必須大於等於 :value KB。',
        'string' => ':attribute 必須大於等於 :value 個字元。',
        'array' => ':attribute 必須大於等於 :value 個元素。',
    ],
    'image' => ':attribute 必須是一張圖片。',
    'in' => '所選擇的 :attribute 選項無效。',
    'in_array' => ':attribute 沒有在 :other 中。',
    'integer' => ':attribute 必須是一個整數。',
    'ip' => ':attribute 不是一個有效的IP網址。',
    'ipv4' => ':attribute 不是一個有效的IPv4網址。',
    'ipv6' => ':attribute 不是一個有效的IPv6網址。',
    'json' => ':attribute 不是一個有效的JSON字串。',
    'lt' => [
        'numeric' => ':attribute 必須小於 :value。',
        'file' => ':attribute 必須小於 :value KB。',
        'string' => ':attribute 必須小於 :value 個字元。',
        'array' => ':attribute 必須小於 :value 個元素。',
    ],
    'lte' => [
        'numeric' => ':attribute 必須小於等於 :value。',
        'file' => ':attribute 必須小於等於 :value KB。',
        'string' => ':attribute 必須小於等於 :value 個字元。',
        'array' => ':attribute 必須小於等於 :value 個元素。',
    ],
    'max' => [
        'numeric' => ':attribute 不能超過 :max。',
        'file' => ':attribute 不能超過 :max KB。',
        'string' => ':attribute 不能超過 :max 個字元。',
        'array' => ':attribute 不能超過 :max 個元素。',
    ],
    'mimes' => ':attribute 必須為 :values 的檔案。',
    'mimetypes' => ':attribute 必須為 :values 的檔案。',
    'min' => [
        'numeric' => ':attribute 不能小於 :min。',
        'file' => ':attribute 不能小於 :min KB。',
        'string' => ':attribute 不能小於 :min 個字元。',
        'array' => ':attribute 不能小於 :min 個元素。',
    ],
    'not_in' => '所選擇的 :attribute 選項無效。',
    'not_regex' => ':attribute 格式錯誤。',
    'numeric' => ':attribute 必須是一個數字。',
    'present' => ':attribute 必須是百分比。',
    'regex' => ':attribute 格式錯誤。',
    'required' => ':attribute 不能為空。',
    'required_if' => '當 :other 是 :value 時, :attribute 不能為空。',
    'required_unless' => '當 :other 不是 :value 時, :attribute 不能為空。',
    'required_with' => '當 :values 出現時 :attribute 不能為空。',
    'required_with_all' => '當 :values 出現時 :attribute 不能為空。',
    'required_without' => '當 :values 為空時 :attribute 不能為空。',
    'required_without_all' => '當 :values 都沒出現時 :attribute 不能為空。',
    'same' => ':attribute 與 :other 必須相同。',
    'size' => [
        'numeric' => ':attribute 的大小必須是 :size。',
        'file' => ':attribute 的大小必須是 :size KB。',
        'string' => ':attribute 必須是 :size 個字元。',
        'array' => ':attribute 必須有 :size 個元素。',
    ],
    'starts_with' => ':attribute 必須以 :values 其中之一開始。',
    'string' => ':attribute 必須是一個字串。',
    'timezone' => ':attribute 必須是一個正確的時區值。',
    'unique' => ':attribute 已經存在。',
    'uploaded' => ':attribute 上傳失敗。',
    'url' => ':attribute 格式錯誤。',
    'uuid' => ':attribute 必須是一個有效的UUID值。',

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

    'attributes' => [],

];

?>

再回到網頁送出表單,
發現錯誤訊息已經變成中文了.
https://ithelp.ithome.com.tw/upload/images/20200929/20105694bJbDBd3ldQ.png

但是欄位名稱還是英文,
欄位名稱一樣是要寫在validation.php裡面,
我們在resources/lang/zh-TW/validation.php的最後面attributes的地方修改如下:

<?php

return [
    'attributes' => [
        'name' => '暱稱',
        'account' => '帳號',
        'password' => '密碼',
        'password_confirm' => '密碼確認',
    ],
];
?>

再回到網頁送出表單,
發現錯誤訊息欄位也變成中文了.
https://ithelp.ithome.com.tw/upload/images/20200929/20105694ejQyVJXSD5.png


上一篇
[Day 19] 建立註冊的畫面及功能(三) - 資料驗證與錯誤訊息
下一篇
[Day 21] 建立註冊的畫面及功能(五) - 表單資料保留
系列文
如何用Laravel寫一個簡單的部落格網站25
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言