英文:
how to display model name in different languages in laravel
问题
以下是代码部分的翻译:
有一个名为"employees"的模型,它具有拉丁文和阿拉伯文的名字,所以我在我的迁移中使用了以下代码:
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('picture');
$table->string('name');
$table->string('name_ar');
$table->string('name_fr');
$table->string('position');
$table->string('position_ar');
$table->string('position_fr');
$table->timestamps();
});
}
我想根据本地化来显示正确的名字,例如,如果本地化为'fr',我应该显示'name_fr',或者如果本地化为'ar',则应该显示'name_ar'。我正在使用一个本地化包。
目前我想到了以下方法:
@if (Lang::locale() == 'ar')
<h4 class="text-white"> {{ $employee->name_ar }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_ar }}</p>
</div>
@elseif(Lang::locale() == 'fr')
<h4 class="text-white"> {{ $employee->name_fr }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_fr }}</p>
</div>
@else
<h4 class="text-white"> {{ $employee->name }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position }}</p>
</div>
@endif
是否有更好的方法来显示正确语言的名字,例如在控制器中或在Lang目录中?
英文:
i have a model called employees whiche has a name in latin and arabic, so i used in my migration this:
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('picture');
$table->string('name');
$table->string('name_ar');
$table->string('name_fr');
$table->string('position');
$table->string('position_ar');
$table->string('position_fr');
$table->timestamps();
});
}
And i want to display the correct name in the language depending on the localization, for example if the localization is 'fr'
i should display name_fr
or name_ar
for 'ar'
,
i am using a localization package.
for now i came up with this:
@if (Lang::locale() == 'ar')
<h4 class="text-white"> {{ $employee->name_ar }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_ar }}</p>
</div>
@elseif(Lang::locale() == 'fr')
<h4 class="text-white"> {{ $employee->name_fr }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position_fr }}</p>
</div>
@else
<h4 class="text-white"> {{ $employee->name }} </h4>
<div class="flex items-center gap-x-1">
<img src="{{ asset('assets/eye-icon.svg') }}" alt="eye">
<p class="text-white text-sm">{{ $employee->position }}</p>
</div>
@endif
is there a better way to display the name in the correct language, for example in a controller or in the Lang directory ?.
答案1
得分: 1
你可以通过一行代码来实现这个,就像这样:
{{ $employee->{'name_' . app()->getLocale()} ?? $employee->name }}
感谢 @Tim Lewis 提到它。
英文:
you could achieve that with a one-line like this
{{ $employee->{'name_' . app()->getLocale()} ?? $employee->name }}
thanks to @Tim Lewis for mentioning it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论