2

Can we provide conditions to trans or Lang method other than pluralization ones, so it checks locale & the condition both to provide the required translations

For Example: We have some translations in English for Organisation 1. And different translations in English for Organisation 2. Now according to user login for organisation, the translations should be shown. Remember the locale is same.

2 Answers2

4

Why not go with something like that:

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

And in resources/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!'
    ]
];

etc.

zloter
  • 333
  • 3
  • 9
  • Is it possible to use three arguments with @lang. I am only aware of using **group** & **key**. – P. Raghuvanshi Mar 15 '18 at 13:08
  • @P.R. I'm not using three argument. I'm just putting this to the structure of my file. By using dot you can build your lang file as tree of arrays. So `"organistation.$organisationId.text"` will be looking for file `organisation.php`, then for array with key of value `$organisationId` and then for key `'text'`. So, you can put any value under `$organisationId` and use it as key for your array. Remember, that you need use `"` instead of `'` to replace `$organisationId` variable. – zloter Mar 15 '18 at 13:20
1

I think you shouldn't use translations for the name of an Organisation, but just create a variable that you directly output. However you could misuse trans_choice in combination with a Constant to use that number to change the output.

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

Translation

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

So to recapture: the "amount" is now just a number that represents an Organisation like an Enum does.

online Thomas
  • 8,864
  • 6
  • 44
  • 85