在Laravel中如何以不同语言显示模型名称

huangapple go评论60阅读模式
英文:

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(&#39;employees&#39;, function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string(&#39;picture&#39;);
            $table-&gt;string(&#39;name&#39;);
            $table-&gt;string(&#39;name_ar&#39;);
            $table-&gt;string(&#39;name_fr&#39;);
            $table-&gt;string(&#39;position&#39;);
            $table-&gt;string(&#39;position_ar&#39;);
            $table-&gt;string(&#39;position_fr&#39;);
            $table-&gt;timestamps();
        });
    }

And i want to display the correct name in the language depending on the localization, for example if the localization is &#39;fr&#39; i should display name_fr or name_ar for &#39;ar&#39;,
i am using a localization package.

for now i came up with this:

@if (Lang::locale() == &#39;ar&#39;)

            &lt;h4 class=&quot;text-white&quot;&gt; {{ $employee-&gt;name_ar }} &lt;/h4&gt;
            &lt;div class=&quot;flex items-center gap-x-1&quot;&gt;
                &lt;img src=&quot;{{ asset(&#39;assets/eye-icon.svg&#39;) }}&quot; alt=&quot;eye&quot;&gt;
                &lt;p class=&quot;text-white text-sm&quot;&gt;{{ $employee-&gt;position_ar }}&lt;/p&gt;
            &lt;/div&gt;

        @elseif(Lang::locale() == &#39;fr&#39;)

            &lt;h4 class=&quot;text-white&quot;&gt; {{ $employee-&gt;name_fr }} &lt;/h4&gt;
            &lt;div class=&quot;flex items-center gap-x-1&quot;&gt;
                &lt;img src=&quot;{{ asset(&#39;assets/eye-icon.svg&#39;) }}&quot; alt=&quot;eye&quot;&gt;
                &lt;p class=&quot;text-white text-sm&quot;&gt;{{ $employee-&gt;position_fr }}&lt;/p&gt;
            &lt;/div&gt;

        @else

            &lt;h4 class=&quot;text-white&quot;&gt; {{ $employee-&gt;name }} &lt;/h4&gt;
            &lt;div class=&quot;flex items-center gap-x-1&quot;&gt;
                &lt;img src=&quot;{{ asset(&#39;assets/eye-icon.svg&#39;) }}&quot; alt=&quot;eye&quot;&gt;
                &lt;p class=&quot;text-white text-sm&quot;&gt;{{ $employee-&gt;position }}&lt;/p&gt;
            &lt;/div&gt;

@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-&gt;{&#39;name_&#39; . app()-&gt;getLocale()} ?? $employee-&gt;name }}

thanks to @Tim Lewis for mentioning it.

huangapple
  • 本文由 发表于 2023年2月14日 06:05:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定