PHP code example of mfn / phpstan-lost-in-translation

1. Go to this page and download the library: Download mfn/phpstan-lost-in-translation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mfn / phpstan-lost-in-translation example snippets


$key = 'foo';
__($key);

foreach (['foo', 'bar'] as $key) {
    __($key);
}

// this one seems to not be working atm :shrug:
/** @return "foo"|"bar" */
function getKey(): string {}
__(getKey());

const KEY = 'foo';
__(KEY);

/** @var array{foo: mixed, bar: mixed} $map */
foreach ($map as $key => $value) {
    __($key);
}



__('missing translation string');



view('sample', [
    'var' => 'val'
]);



/** @var \Illuminate\Contracts\Translation\Translator $translator */
/** @var string $dynamic */
$translator->get($dynamic);

/** @var "foo"|"bar"|\Exception $craycray */
$translator->get($craycray);



__('foo.bar', [], 'en');



/* has a replacement that doesn't exist in the translation key */
__('exists in all locales', ['foo' => 'bar', 'bar' => 'bat'], 'en');

/* has multiple replacement variants */
__(':foo :FOO', ['foo' => 'bar'], 'en');



trans_choice('{0} There are none|{1} There is one|[2] There are :count', 3, [], 'en');

 return [
    'this_value_is_not_allowed' => 3,
];



__('foobar', [], 'invalid_locale');

 return [
  "\xf0\x28\x8c\xbc" => "\xf0\x28\x8c\xbc",
];




__("messages.\xf0\x28\x8c\xbc", [], 'ja');
bladehtml
@lang('blade at directive')
{{ __('blade double underscore') }}
{{ __('exists in all locales') }}
{{ __('only in ja') }}

@php
    // these may slation\Translator::class)->get('via app function with class');
@endphp
console
$ phpstan analyse --configuration=e2e/phpstan-e2e.neon --no-progress -v e2e/src/invalid-choice.php
 ------ ------------------------------------------------------------------------------------------------------------------
  Line   invalid-choice.php
 ------ ------------------------------------------------------------------------------------------------------------------
  3      Translation choice does not cover all possible cases for number of type: 3
         🪪  lostInTranslation.invalidChoice.missingCase
         💡 Locale: "en", Key: "{0} There are none|{1} There is one|[2] There are :count", Value: "{0} There are none|{1}
            There is one|[2] There are :count"
 ------ ------------------------------------------------------------------------------------------------------------------