leastbad / optimism

The missing drop-in solution for realtime remote form validation in Rails.
MIT License
361 stars 42 forks source link

Added internationalization support #4

Closed andryusha closed 4 years ago

leastbad commented 4 years ago

Hey, Andrey! Thanks for taking a look at this.

Could you help me understand your approach, here? I have an admittedly Anglo-centric linguistic outlook, making me spoiled and privileged. I'd love to make this library easy for everyone to work with.

What I know is that Humanize does a reasonably good job on English words for most cases. Plus with the way Inflector is set up, you can add additional words. I have no idea what the situation is for different languages, much less different alphabets or Cyrillic character sets.

The issue is for attributes such as "ssl" or "user id". You want the display to be SSL or User, not Ssl or User id.

So I guess my question is... what about the full_message() method makes it a better candidate for internationalization?

Could we do something like this?

message = "#{model.errors.full_message(attribute.humanize, model.errors.messages[attribute.to_sym].first)}#{Optimism.suffix}"

Would that accomplish the same outcome, or will humanize mess up with other character sets?

leastbad commented 4 years ago

FWIW, I just went for it. 😁

andryusha commented 4 years ago

Your solution did not use attribute names from localization files (config/locales/ru.yml for example). And also it did not allow to set a custom format for the error message.

Unfortunately, attribute.humanize and Inflector helps only for English.

andryusha commented 4 years ago

Attribute name still in-english in error message. Solution: attribute.to_sym

https://apidock.com/rails/v6.0.0/ActiveModel/Errors/full_message

leastbad commented 4 years ago

Damn, well... I tried! We will get this working, I promise. I quite literally need your help, though. I had to push a new release yesterday and hoped my fix would work. We can take our time getting this working properly now.

I didn't realize that Inflector didn't work with non-English applications. That honestly really sucks. Weird, too, considering that English is DHH's 2nd language.

So here's what I propose: I will add a new configuration option. (What should I call it? humanize? inflector? english? i18n?) Let's assume for discussion that it will be humanize:

self.humanize = true
...
message = "#{model.errors.full_message(Optimism.humanize ? attribute.humanize : attribute.to_sym, model.errors.messages[attribute.to_sym].first)}#{Optimism.suffix}"

Will that fix the problem? I don't think I use any other inflector methods... I was worried about pluralize but I don't see it used.

Is there anything else I have to do to get it pulling from config/locales?

andryusha commented 4 years ago

Hey. I am sure that in this case no additional option is needed.

My solution is universal and can be used both with internationalization and without it. Just use attribute.to_sym as the first parameter for full_message.

For a deeper understanding of how full_message works, I would recommend you to look at the source code of the method. Btw, .humanize used inside full_message.

leastbad commented 4 years ago

The problem is that you’re not considering what humanize does for us English speakers:

If i call to_sym on the user_id attribute it will say “User id” which is not what we want. Humanize will give “User”.

If i set up “SSL” as an inflector word, to_sym gives “Ssl” and humanize gives “SSL”.

Does that make sense? or am i missing something?

On Wed, Apr 8, 2020 at 3:17 AM Andrey Maslov notifications@github.com wrote:

Hey. I am sure that in this case no additional option is needed.

My solution is universal and can be used both with internationalization and without it. Just use attribute.to_sym as the first parameter for full_message.

For a deeper understanding of how full_message works, I would recommend you to look at the source code of the method. Btw, .humanize used inside full_message.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/leastbad/optimism/pull/4#issuecomment-610793733, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDCCQFGMXFF5L6TO7MLXJDRLQQJNANCNFSM4L32JXNA .

andryusha commented 4 years ago

Do not worry about it. Just use my code

In this case I use user_id and SSL as an inflector acronym.

andryusha commented 4 years ago

For english

leastbad commented 4 years ago

🤷‍♂

Well, that was embarrassing. You are, of course, 100% right. Thanks for being patient while I was being stubborn and clever.

I will push 0.2.13 soon.