【问题标题】:laravel localization with conditionslaravel 本地化条件
【发布时间】:2018-03-15 12:13:28
【问题描述】:

我们能否为 transLang 方法提供除复数以外的条件,以便它检查语言环境和条件以提供所需的翻译

例如:我们为组织 1 提供了一些英文翻译。并且 组织 2 的不同英文翻译。现在根据组织的用户登录,应显示翻译。 记住语言环境是一样的。

【问题讨论】:

  • 请提供您需要的示例

标签: laravel laravel-localization


【解决方案1】:

为什么不使用类似的东西:

@lang("organistation.$organisationId.text")

在资源/lang/en/organisation.php 中:

<?php 
return [
    'first_organization_id' => [
        'text' => 'This text belongs to first organisation!',
        'text2' => 'Other text belongs to first organisation!'
    ],
    'second_organization_id' => [
        'text' => 'This text belongs to second organisation!',
        'text2' => 'Other text belongs to second organisation!'
    ]
];

等等

【讨论】:

  • 是否可以对@lang 使用三个参数。我只知道使用 group & key
  • @P.R.我没有使用三个参数。我只是把它放到我的文件结构中。通过使用 dot,您可以将 lang 文件构建为数组树。所以"organistation.$organisationId.text" 将查找文件organisation.php,然后查找键值为$organisationId 的数组,然后查找键'text'。因此,您可以在$organisationId 下放置任何值并将其用作数组的键。请记住,您需要使用" 而不是' 来替换$organisationId 变量。
【解决方案2】:

我认为您不应该对组织的名称使用翻译,而应创建一个直接输出的变量。但是,您可能会误用 trans_choice 与常量结合使用该数字来更改输出。

abstract class Organisation
{
    const Organisation1 = 1;
    const Organisation2 = 2;
    const Organisation3 = 3;
    // etc
}

翻译

// en/organisations.php
'organisation' =>  '{1} Coca Cola|{2} 1 Pesi|[3, Inf] Unilever :org'    
// in your views
trans_choice('organisations.organisation', ['org' => Organisation::Organisation1 ])

所以重新回顾一下:“数量”现在只是一个代表组织的数字,就像 Enum 一样。

【讨论】:

    猜你喜欢
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2021-04-03
    • 2014-06-26
    • 2021-01-09
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多