日期和时间本地化不适用于 Laravel-mix

Date and Time localization not works in Laravel-mix

我的服务器上安装了Laravel mix。网站上有一个聊天部分,我使用某种 class :

class ActivityCell extends Component {
    getTimestamp() {
        const {message} = this.props;
        return (
            <span className="font-weight-semi-bold">
                {utcDateCalendarTime(message.created_at)}
            </span>
        );
    }

这是我的 AppServiceProvider.php 文件:

<?php

namespace App\Providers;

use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */

public function boot()
{
    setlocale(LC_ALL, Config::get('app.lc_all'));
    Carbon::setLocale(Config::get('app.locale'));
}
    public function register()
    {
        $this->registerPlugins();
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->bootDatabase();
        $this->bootResource();
    }

    /**
     * Boot database schema
     *
     * @return void
     */
    private function bootDatabase()
    {
        Schema::defaultStringLength(191);
    }

    /**
     * Boot resource
     *
     * @return void
     */
    private function bootResource()
    {
        Resource::withoutWrapping();
    }

    /**
     * Register plugins
     *
     * @return void
     */
    private function registerPlugins()
    {
        $pluginDirs = File::directories(base_path('app/Plugins'));

        foreach ($pluginDirs as $pluginDir) {
            $class = "App\Plugins\" . basename($pluginDir) . "\PluginServiceProvider";

            if (class_exists($class) && is_subclass_of($class, ServiceProvider::class)) {
                $this->app->register($class);
            }
        }
    }
}

我试图将 setlocale(LC_TIME, 'tr'); 放在 class 文件之上,但没有成功。然后尝试使用碳,以便在我更改网站语言时以不同的语言查看日期。

我在app/config.php中添加了以下代码:

'locale' => env('APP_LOCALE', 'az'),
'lc_all' => env('APP_LC_ALL', 'az_AZ.UTF-8'),

并在 env 文件中添加了以下内容:

APP_LOCALE = az
APP_LC_ALL = az_AZ.UTF-8

这两种方法,我都没有成功。我很确定我在某个地方犯了一个错误,但找不到确切的位置。也许我缺少添加其他内容。任何帮助将不胜感激。

编辑:添加Chat.php:

<?php

namespace App\Models;

use App\Events\ChatParticipationChanged;
use App\Events\ChatUpdated;
use App\Http\Resources\ChatMessage as ChatMessageResource;
use App\Http\Resources\MarketplaceTrade as MarketplaceTradeResource;
use ArrayObject;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use JSsVPSDioNXpfRC;
use DateTimeInterface;

class Chat extends Model
{
    protected $lastMessageAttribute;
    protected $lastMarketplaceTradeAttribute;

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */

protected $guarded = [];

    /**
     * The event map for the model.
     *
     * @var array
     */
    protected $dispatchesEvents = [
        'updated' => ChatUpdated::class
    ];

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * Get the route key for the model.
     *
     * @return string
     */
protected function serializeDate(DateTimeInterface $date)
{
    return $date->translatedFormat('A B M');
}

    public function getRouteKeyName()
    {
        return 'id';
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'creator_id', 'id');
    }

    /**
     * Participants for this chat
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function participants()
    {
        return $this->hasMany(ChatParticipant::class, 'chat_id', 'id');
    }

    /**
     * Messages for this chat
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function messages()
    {
        return $this->hasMany(ChatMessage::class, 'chat_id', 'id');
    }

    /**
     * Update user's participation record
     *
     * @param User $user
     */
    public function updateParticipation($user)
    {
        $this->participants()->where('user_id', $user->id)
            ->update(['last_read_at' => now()]);

        broadcast(new ChatParticipationChanged($this, $user));
    }

    /**
     * All marketplace trades hosted by this chat
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function marketplaceTrades()
    {
        return $this->hasMany(MarketplaceTrade::class, 'chat_id', 'id')
            ->has('buyer')->has('seller');
    }

    /**
     * @return Model|\Illuminate\Database\Eloquent\Relations\HasMany|mixed|object|null
     */
    public function getLatestMarketplaceTrade()
    {
        if (!isset($this->lastMarketplaceTradeAttribute)) {
            $trade = $this->marketplaceTrades()->latest()->first();

            $this->lastMarketplaceTradeAttribute = new MarketplaceTradeResource($trade);
        }
        return $this->lastMarketplaceTradeAttribute;
    }

    /**
     * Last chat message
     *
     * @return ChatMessageResource|ArrayObject|mixed
     */
    public function getLatestMessage()
    {
        if (!isset($this->lastMessageAttribute)) {
            $message = $this->messages()->latest()->first();

            if ($message) {
                $this->lastMessageAttribute = new ChatMessageResource($message);
            } else {
                $this->lastMessageAttribute = new ArrayObject();
            }
        }
        return $this->lastMessageAttribute;
    }

    /**
     * @param User $user
     * @return array
     */
    public function getParticipation($user)
    {
        $participant = $this->participants()
            ->where('user_id', $user->id)->without('user')
            ->first();

        $unreadMessagesCount = ($participant && $participant->last_read_at) ?
            $this->messages()->where('user_id', '!=', $user->id)
                ->where('created_at', '>', $participant->last_read_at)
                ->count() :
            $this->messages()->where('user_id', '!=', $user->id)
                ->count();

        return [
            'user_id'               => $user->id,
            'unread_messages_count' => $unreadMessagesCount
        ];
    }

    /**
     * If user should be allowed in this chat
     *
     * @param User $user
     * @return bool
     */
    public function shouldAllowUser($user)
    {
        $isParticipant = $this->participants()
            ->where('user_id', $user->id)->exists();
        return (
            $isParticipant ||
            $user->can('moderate_chats')
        );
    }

    /**
     * @return string
     */
    public function attachmentsDir()
    {
        return "chats/{$this->id}/message-attachments";
    }



}

问题出在您的命名空间上:

// Using PHP callable syntax
use Carbon\Carbon;

或者,

// Using string syntax
\Carbon\Carbon::setLocale('ru');

您还需要在 blade 上使用 translatedFormat() 方法来使用翻译格式,例如:

{{ Carbon\Carbon::now()->translatedFormat('A B M') }} // утра 428 фев

您可以在模型上使用 serializeDate() 方法,将时间戳列更改为转换后的数据时间格式:

use DateTimeInterface;

protected function serializeDate(DateTimeInterface $date)
{
    return $date->translatedFormat('A B M');
}