Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Translator return null for missing translations #115

Open
tomaszkane opened this issue Apr 7, 2023 · 4 comments
Open

Feature: Translator return null for missing translations #115

tomaszkane opened this issue Apr 7, 2023 · 4 comments

Comments

@tomaszkane
Copy link

What steps will reproduce the problem?

Use Translator on not-existed key.

echo $translator->translate('not.existed.key'); // "not.existed.key"

What is the expected result?

I want to decide when translate() should return key or null.

$translationsForFooCategory = [
  'label.some.existed.key' => 'Hello!',
];

$someOutput =  $translator->translate('label.some.existed.key', category: 'foo');
$someOutput .= $translator->translate('description.some.not.existed.key', category: 'foo');

echo $someOutput; // "Hello!"

Throwing exception on missing translations is to complicated here.

I want to decide if given translate category return null on missing key or given TranlateInterface instance.

Additional info

Q A
Version 3.0.0
PHP version 8.0
@xepozz
Copy link
Member

xepozz commented Apr 7, 2023

What would you do with that null?

@tomaszkane
Copy link
Author

Nothing. It just "This key does not have translation, so return nothing".
I create translations for RBAC items:

return [
    'label.foo-read' => 'Foo label - it should be requred',
    'description.foo-read' => 'Foo description - its optional.',
    'label.boo-delete' => 'Boo',
//    'description.boo-delete' => 'Boo description is not necessary.',
];

Now I must set EventDispatcherInterface and catch exceptions or write dummy IF's like:

$translated = $translator->translate('description.boo-delete', category: 'foo');
if ('description.boo-delete' !== $translated) {
  $someOutput .= $translated;
}

@xepozz
Copy link
Member

xepozz commented Apr 7, 2023

I think we may add some fallback function to let users decide what to do with missing translations.
But null is impossible because of return type.

Will it be ok for you?

@tomaszkane
Copy link
Author

Empty string is ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants