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

192 views Asked by At

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:

#post_show
    %h1= @post.title
    %p.username
        before
        = time_ago_in_words(@post.created_at)
    .clearfix
        .post_image_description
            = image_tag @post.image.url(:medium)
            .description= simple_format(@post.description)
        .post_data
            = link_to "Project link", @post.link, class: "btn btn-warning btn-block"
            = link_to like_post_path(@post), method: :get, class: "data" do
                %i.fa.fa-check
                = pluralize(@post.get_upvotes.size, "Like")
            %p.data
                %i.fa.fa-comments.o
                = 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

1

There are 1 answers

5
Vlad On

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

en:
  models:
    posts:
      votes: Like
      comments: Comment

Then in config/locales/ro.yml

ro:
  models:
    posts:
      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: www.myapp.com?lang=en Rails will use the en.yml locales. Whereas www.myapp.com?lang=rowill use the ro.yml locales.

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