0

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

hekmat
  • 93
  • 2
  • 8

1 Answers1

1

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.

Vlad
  • 902
  • 4
  • 14
  • This sounds great! However, the langauge i am using is arabic. When there is 0 or 2 and more comments there is still an S for pluralization at the end of the word. For example, ###s. I do not want the S to appear with the arabic word. – hekmat Jan 04 '17 at 21:34
  • I don't think that's possible out of the box. You'll most likely have to create a helper that doesn't pluralize your string if language is arabic. – Vlad Jan 05 '17 at 09:20
  • Ok. How about if i use the string without the pluralizer `{post.comments.count} Comment` ? – hekmat Jan 05 '17 at 10:54
  • Well, it will work. But you will have, most likely, strings like: `3 Comment` or `0 Comment` .. Why is it that you don't want pluralization if the language is Arabic? – Vlad Jan 05 '17 at 12:46
  • The thing is that I don't know how to create a helper that doesn't pluralize the string. I'm pretty new to rails. – hekmat Jan 05 '17 at 14:45