0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

laravel-enum:v2の日本語化

Posted at

laravel-enumのv2の日本語化の書き方が他の記事と違ったので備忘録的に書き残す

環境


% sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H2

% docker version
Client: Docker Engine - Community
 Cloud integration: 1.0.1
 Version:           19.03.13
 API version:       1.40

# php -v
PHP 7.4.7 (cli) (built: Jun 11 2020 18:41:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans

# php artisan -v     
Laravel Framework 7.26.1

# mysql --version
mysql  Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using  EditLine wrapper

composer require bensampo/laravel-enum:2.2

最初何も考えずにcomposer require bensampo/laravel-enum叩いたら「最新のlaravel-enumはlaravel7.*に対応してない」と怒られた
公式docによるとlaravel7.*に対応してるのはbensampo/laravel-enum:2.2らしいのでバージョン指定でcomposer requireする
公式doc読むの大事ほんと

composer require bensampo/laravel-enum:2.2

ここからはv2のdocを参照する

enum定義

enumファイル作成

php artisan make:enum UserType

enum定義

UserType.php
use BenSampo\Enum\Enum;
use BenSampo\Enum\Contracts\LocalizedEnum;

final class UserType extends Enum implements LocalizedEnum
{
    const Administrator = 1;
    const SuperAdministrator = 2;
}

ちなみにmysqlのenum型は0を''として予約されているので1から定義する

日本語化

多言語化のインターフェースを有効にする

# php artisan vendor:publish --provider="BenSampo\Enum\EnumServiceProvider"
Copied Directory [/vendor/bensampo/laravel-enum/resources/lang] To [/resources/lang/vendor/laravelEnum]
Publishing complete.

resouces/ja配下にenums.phpを作成して定義

resources/lang/ja/enums.php
<?php

use App\Enums\UserType;

return [

    UserType::class => [
        UserType::Administrator => '管理者',
        UserType::SuperAdministrator => 'スーパー管理者',
    ],

];

これで日本語化されます

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?