Question: How to remove or edit the pluralization for comments and likes in rails


How to remove or edit the pluralization for comments and likes in rails

Answers 1
Added at 2017-01-04 17:01

I am new to rails, and i am working on a rtl website! I am trying to fix the pluralization in comment.count and get_upvotes.size to be replaced by the other language words. I have heard that I can do it with Rails Internationalization (I18n) but I could not find a clear answer to my question.

Here's my code in the show.html.erb page:

    %h1= @post.title
        = time_ago_in_words(@post.created_at)
            = image_tag @post.image.url(:medium)
            .description= simple_format(@post.description)
            = link_to "Project link",, class: "btn btn-warning btn-block"
            = link_to like_post_path(@post), method: :get, class: "data" do
                = pluralize(@post.get_upvotes.size, "Like")
                = pluralize(@post.comments.count, "Comment")
            - if @post.user == current_user
                = link_to "Edit", edit_post_path(@post), class: "data"
                = link_to "Delete", post_path(@post), method: :delete, data: { confirm: "Sure?" },class: "data"

Now all I need is to cancel the pluralization so when 2 comments or more shows up it does not pluralize and change to comments. Instead it stays the same as comment

I would appreciate the help. Let me know if more information is needed! Thanks

nr: #1 dodano: 2017-01-04 18:01

If I understood correctly, you want to translate Like and Comment using I18n while also keeping the pluralization?

In your views, you need to change:

pluralize(@post.get_upvotes.size, t('models.posts.votes')) pluralize(@post.comments.count, t('models.posts.comments'))

Then in config/locales/en.yml

      votes: Like
      comments: Comment

Then in config/locales/ro.yml

      votes: Aprecieri
      comments: Comentarii

Rails will automatically take the appropriate Language 'pack' based on whatever variable you set.

So for example, if you wished to pass the language in the URL like so: Rails will use the en.yml locales. Whereas use the ro.yml locales.

It's up to you on how you choose the language.

Source Show
◀ Wstecz