0

In my system there is a part to list competitions. When a competition signup opens later then today there should be like this: "Signup opens 2022-08-29" The program row in the app/models/competition.php looks like this:

252        elseif($this->status == 'not_opened'):
253            return _('Signup opens %s', $this->signups_opening_date);

But when I open the list I got an error as this:

(ArgumentCountError(code: 0): _() expects exactly 1 argument, 2 given at /Users/ralph/laravel9-final/Webshooter_web_Laravel9/app/Models/Competition.php:253)

It suppose to get the date from the database and show after Signup opens.....

What can be wrong?

But... In other places the date appears as here:

<tr>
   <td width="40%">{{_('Opens for signup')}}</td>
   <td><% competitions.competition.signups_opening_date %></td>
</tr>

Shows like this: Opens for signup 2022-08-22

raffocat
  • 11
  • 6
  • put `sprintf('Signup opens %s', $this->signups_opening_date)` into _() in your first case – Gino Pane Aug 10 '22 at 09:30
  • The error describes exactly the problem - [`gettext()` (and the shortcut version `_()`) only accepts 1 parameter](https://stackoverflow.com/questions/5445815/gettext-placeholders). If you want to use substitutions you will need another solution - eg maybe [Laravel's own localisation features](https://laravel.com/docs/9.x/localization). – Don't Panic Aug 10 '22 at 09:40
  • Does this answer your question? [Gettext placeholders](https://stackoverflow.com/questions/5445815/gettext-placeholders) – Don't Panic Aug 10 '22 at 09:40

1 Answers1

-1

Hey what are you try to don in this line ?

_('Signup opens %s', $this->signups_opening_date);

if you try to combine text with date try do like this below

"Signup opens $this->signups_opening_date";

also you can use sprintf function like below

sprintf('Signup opens %s', $this->signups_opening_date)

if you try to use laravel language system you can do like this below

_('Signup_opens').$this->signups_opening_date;
  • Thank you Aryan, This did it: return _("Signup opens $this->signups_opening_date"); – raffocat Aug 10 '22 at 09:50
  • This works too: return _('Signup_opens ').$this->signups_opening_date; – raffocat Aug 10 '22 at 10:17
  • This misses the whole point of substitutions in localisation, to handle cases where the word order changes between languages. Also, "laravel language system" uses `__()`, not `_()` which is [`gettext()`](https://www.php.net/manual/en/function.gettext.php). – Don't Panic Aug 10 '22 at 10:22
  • Well, Don't Panic, return __("Signup opens $this->signups_opening_date"); works also. – raffocat Aug 10 '22 at 12:04
  • Really? You have a set of translation strings somewhere with every possible date? :-) – Don't Panic Aug 10 '22 at 15:27