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

getDescription overwrites Localization #94

Closed
MartinP7r opened this issue Aug 26, 2019 · 4 comments
Closed

getDescription overwrites Localization #94

MartinP7r opened this issue Aug 26, 2019 · 4 comments

Comments

@MartinP7r
Copy link
Contributor

when implementing a custom getDescription() method, localization via LocalizedEnum stops working. Maybe I'm using it wrong, but I would have preferred the getDescription() implementation to work as a fallback if there's no localization for the particular language.

@BenSampo
Copy link
Owner

Hi @MartinP7r, what you've described is exactly how the default implementation of the getDescription method works.

Take a look at:
https://github.com/BenSampo/laravel-enum/blob/master/src/Enum.php#L220-L225

@MartinP7r
Copy link
Contributor Author

Sorry @BenSampo, I don't think I expressed my question clear enough (wasn't even a question, sorry for that. Not much sleep 😅)

https://github.com/BenSampo/laravel-enum#overriding-the-getdescription-method
When I implement customized getDescription strings like in the example above, the method returns early, won't go to parent::getDescription() where the implementation you referenced lies. Hence, when overriding the description strings, localization values for all overriden strings have to be supplied for all available languages (no fallback possible since parent::getDescription() is not called).

Thinking about it, that's probably hard to work around, without implementing a different method for overriding getDescription. So, I'll just supply all the string. Just thought this might be a problem in scenarios where there's a bunch of languages supported by the application and falling back on overridden description values would be preferrable.

@BenSampo
Copy link
Owner

Ah in that case, apologies for being hasty in closing the issue, will re-open it.

I'm not 100% on the problem still. Can you let me know what you're trying to achieve? Any code examples would be useful.

The way I see it is that you either:

  1. Want to override the description for a specific enum value for all languages which you can do using this method: https://github.com/BenSampo/laravel-enum#overriding-the-getdescription-method

Any enum values not explicitly defined here will fallback to the default implementation of checking localisation files or subsequently generating a friendly name.

  1. Want to override the description for each language. Can be done using localisation: https://github.com/BenSampo/laravel-enum#localization

@BenSampo BenSampo reopened this Aug 28, 2019
@MartinP7r
Copy link
Contributor Author

Sorry for my late response.
And sorry for not being able to explain myself properly.

Going over all of it, leaving the implementation as-is would is probably best, since it's probably a minor thing but I try to give an explanation of what I was having problems with, below.

Essentially, you can only use localization if there is no overridden implementation of getDescription.

BUT, if you have an application with e.g. 10 different languages, where the fallback is English
and the enum in question needs a customized description value that would use the fallback across all languages (e.g. your example "Super admin" instead of the enum-case SuperAdministrator), then you would have to supply "Super admin" in every localization file for all 10 languages regardless.

This might be a strange way of looking at it. So I really don't mind closing this issue again if you feel like it makes little sense.
let me know if it's still unclear. Then I'll supply a concrete example.

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