Skip to content

Instantly share code, notes, and snippets.

@makkinga
Last active November 1, 2021 08:39
Show Gist options
  • Save makkinga/440592a2e1388e4cb255c3388b7eac56 to your computer and use it in GitHub Desktop.
Save makkinga/440592a2e1388e4cb255c3388b7eac56 to your computer and use it in GitHub Desktop.
Laravel Pluralisation for Tranlation Strings

Laravel Pluralisation for Tranlation Strings

Add this to your helpers file

if (! function_exists('__choice')) {
    /**
     * @param string $key
     * @param int $count
     *
     * @return string
     */
    function __choice(string $key, int $count) : string
    {
        return trans_choice(__($key), $count);
    }
}

Use it in your views like this

{{ __choice('results for', $total) }}

In your JSON translations, you can still use all default parameters and constructions

{
    "results for": "{0} Geen resultaten voor|{1} Eén resultaat voor|[2,*] :count resultaten voor"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment