techouse / intl-date-time

此包已被废弃,不再维护。未建议替代包。

Laravel Nova 的国际日期选择器字段。

v1.7.0 2022-02-13 15:30 UTC

README

Latest Version on Packagist Total Downloads Licence PHP version Codacy Badge GitHub stars

针对 Laravel Nova 的国际日期选择器

将本地化应用于 71 个不同的区域设置,以支持 Laravel Nova 默认的 DateTime 字段,该字段目前不支持开箱即用的本地化。

International DateTime

安装

您可以通过 composer 将此包安装到使用 Nova 的 Laravel 应用中。

composer require techouse/intl-date-time

使用方法

API 是从 Nova 的默认 DateTime 字段 适配的。

该模块本身提供了一些可选的配置

  • locale - 可选 - 设置模块的区域。如果不设置,将自动使用应用程序的 config('app.locale')。如果您手动定义了不受支持的区域,它将抛出异常!
  • dateFormat - 可选 - 设置日期格式。如果不提供,模块将自动使用相应区域的日期格式。格式必须是 MomentJS 兼容的
  • timeFormat - 可选 - 设置时间格式。格式必须是 MomentJS 兼容的!如果您手动定义了不受支持的时间格式,它将抛出异常!
  • minDate - 可选 - 设置允许选择的最早日期(包括)。必须是 \DateTimeCarbon\Carbon 的实例。默认为 null
  • maxDate - 可选 - 设置允许选择的最新日期(包括)。必须是 \DateTimeCarbon\Carbon 的实例。默认为 null
  • placeholder - 可选 - 设置占位符。如果您不希望设置占位符,请将其设置为 false
  • errorMessage - 可选 - 在日期格式无效的情况下设置自定义错误消息。如果没有设置,它将显示当前区域的错误消息。
  • errorMessageLocale - 可选 - 设置自定义错误消息的区域。如果没有设置,它等于设置的 locale 或应用程序的 config('app.locale')。如果您手动定义了不受支持的区域,它将抛出异常!这里是所有支持的区域列表
  • hideUserTimeZone - 可选 - 应用时隐藏表单字段旁边的用户时区。默认为 false
  • withShortcutButtons - 可选 - 应用时在日历下方显示用户“昨天”、“今天”和“明天”的3个快捷按钮。按钮全部为英文,要翻译它们,请编辑位于 resources/lang/vendor/nova 目录的语言 JSON 文件。
  • withTime - 可选 - 快捷方式用于显示完整的本地时间,例如 HH:mm:ss。注意:timeFormat 选项优先于 withTime。
  • withTimeShort - 可选 - 展示短时区本地时间的快捷方式,例如 HH:mm。注意:timeFormat 选项比 withTimeShort 优先。
  • userTimeZone - 可选 - 定义特定字段的自定义时区 - 默认使用 Nova.config.userTimezone。

简单使用 IntlDateTime 类代替直接使用 DateTime 或像下面示例中那样别名它,这样您就不必对现有代码进行太多重构。

<?php

namespace App\Nova;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Techouse\IntlDateTime\IntlDateTime as DateTime;

class User extends Resource
{
    /**
     *  This is how you use and configure this module
     */
    public function fields(Request $request)
    {
        return [
            DateTime::make(__('Updated at'), 'updated_at')
                    /**
                     * The module automatically uses your app's locale 
                     * from config('app.locale'), however you can manually
                     * override this by setting it like this.
                     * 
                     * IMPORTANT: Check the list of supported locales below in this readme!
                     * 
                     * NOTE: If the automatic locale is not supported by MomentJS 
                     * the module defaults to 'en-gb' (British English).
                     */
                    ->locale('sl'),
                    
            DateTime::make(__('Created at'), 'created_at')
                    /**
                      * You can optionally set a custom DATE format.
                      * 
                      * It has to be compatible with MomentJS!!!
                      * https://moment.js.cn/docs/#/displaying/format/
                      */
                    ->dateFormat('DD.MM.YYYY'),   
                    
            DateTime::make(__('Deleted at'), 'deleted_at')
                    /**
                      * You can optionally set a custom TIME format
                      * 
                      * It has to be compatible with MomentJS!!!
                      * https://moment.js.cn/docs/#/displaying/format/
                      */
                    ->timeFormat('HH:mm:ss'),
                    
            DateTime::make(__('Packaged on'), 'packaged_on')
                    /**
                      * You can optionally set a placeholder, otherwise
                      * it will default to your timezone's date format 
                      */
                    ->placeholder('DD.MM.LLLL'),
                    
            DateTime::make(__('Shipped on'), 'shipped_on')
                    /**
                      * You can disable the placeholder by setting it to false
                      */
                    ->placeholder(false),
                    
            DateTime::make(__('Birthday'), 'birthday')
                    /**
                      * You can override the default date invalid error message
                      */
                    ->errorMessage("I don't think you were born on that day mate :D"),
                    
            DateTime::make(__('Day of graduation'), 'graduated_on')
                    /**
                      * Unless you override the error message locale it equals the locale setting
                      */
                    ->errorMessageLocale('de'),
                    
            DateTime::make(__('Takes place at'), 'takes_place_at')
                    /**
                     * Set a minimum/earliest date (inclusively) allowed for selection.
                     */
                    ->minDate(Carbon::parse('1990-05-30'))
                    /**
                     * Set a maximum/latest date (inclusively) allowed for selection.
                     */
                    ->maxDate(Carbon::today()),
            
            DateTime::make(__('Day you got married'), 'day_you_got_married')
                    /**
                     * Hide the user time zone next to the form input field.
                     */
                    ->hideUserTimeZone(),

            DateTime::make(__('Date of travel'), 'date_of_travel')
                    /**
                     * Display shortcut buttons for "yesterday", "today" and "tomorrow".
                     * Translate them in your language's JSON file located in resources/lang/vendor/nova.
                     */
                    ->withShortcutButtons(),

            DateTime::make(__('Time of arrival'), 'time_of_arrival')
                    /**
                     * Shortcut for displaying the full locale time, e.g. HH:mm:ss.
                     * 
                     * NOTE: The timeFormat option has precedence over withTime.
                     */
                    ->withTime(),

            DateTime::make(__('Time of reservation'), 'time_of_reservation')
                    /**
                     * Shortcut for displaying the short locale time, e.g. HH:mm.
                     * 
                     * NOTE: The timeFormat option has precedence over withTimeShort.
                     */
                    ->withTimeShort(),

            DateTime::make(__('Time of reservation'), 'deleted_at')
                    /**
                     * Set default hour of time selector
                     */
                    ->defaultHour(9),

            DateTime::make(__('Time of reservation'), 'uploaded_at')
                    /**
                     * Set default minute of time selector
                     */
                    ->defaultMinute(30),
        ];
    }

    /**
     * The rest of the Resource ... bla bla bla :)
     */
}

程序填充

像许多 Laravel Nova 字段一样,此字段实现了程序填充。您可以通过 Laravel Nova JavaScript 事件在更新表单中填充字段。

Nova.$emit('my-field-name-value', '29/06/1998')

请确保发出的值对应于您的本地日期。

支持的区域列表

此模块只支持同时由 MomentJSFlatpickr 支持的区域!

目前支持71 个区域是

语言 区域
阿尔巴尼亚语 sq
阿拉伯语 ar
阿塞拜疆语 az
孟加拉语 bn
白俄罗斯语 be
波斯尼亚语 bs
保加利亚语 bg
缅甸语 my
加泰罗尼亚语 ca
中文(中国) zh-cn
中文(香港) zh-hk
中文(台湾) zh-tw
克罗地亚语 hr
捷克语 cs
丹麦语 da
荷兰语 nl
英语 en
英语(澳大利亚) en-au
英语(加拿大) en-ca
英语(爱尔兰) en-ie
英语(新西兰) en-nz
英语(英国) en-gb
世界语 eo
爱沙尼亚语 et
法罗语 fo
芬兰语 fi
法语 fr
法语(加拿大) fr-ca
法语(瑞士) fr-ch
格鲁吉亚语 ka
德语 de
德语(奥地利) de-at
德语(瑞士) de-ch
希腊语 el
希伯来语 he
印地语 hi
匈牙利语 hu
印度尼西亚语 id
冰岛语 is
爱尔兰盖尔语 ga
意大利语 it
日语 ja
哈萨克语 kk
柬埔寨语 km
韩语 ko
拉脱维亚语 lv
立陶宛语 lt
马其顿语 mk
马来语 ms
蒙古语 mn
挪威语 nb
波斯语 fa
波兰语 pl
葡萄牙语 pt
葡萄牙语(巴西) pt-br
旁遮普语 pa-in
罗马尼亚语 ro
俄语 ru
塞尔维亚语 sr
塞尔维亚语(西里尔文) sr-cyrl
僧伽罗语 si
斯洛伐克语 sk
斯洛文尼亚语 sl
西班牙语 es
西班牙语(多米尼加共和国) es-do
西班牙语(美国) es-us
瑞典语 sv
泰语 th
土耳其语 tr
乌克兰语 uk
越南语 vi
威尔士语 cy
注意

这是我对原始的斯洛文尼亚日期时间的改进