haml, link_to helper, and I18n in the same sentenc

2019-07-20 22:14发布

Here is the markup I'm trying to get:

<p>View more <a href="/clinics/integratedclinic">clinic information</a> 
including physicians, locations, directions, documents, and more.</p>

I'm using haml, yaml, and Rails (oh my!). In order to do this with localization, I have to have this yaml:

en:
  view_more: View more
  link: clinic information
  details: including physicians, locations, directions, documents, and more.

And put my haml on 3 lines like:

%p
  = I18n.t('view_more')
  = link_to I18n.t('link'), clinic
  = I18n.t('details')

It seems like there's got to be a better way. The first issue is that this wouldn't work for languages with different syntax, in which the link might occur at the end of the sentence because of grammatical word order.

Isn't there a way to pass the link in as a parameter? But then I have to interpolate it in the yaml, and maybe put markup in there? That doesn't seem great, either. Is there an elegant way to do this that I'm missing?

2条回答
劫难
2楼-- · 2019-07-20 22:49

You find some more ideas at a similar question. My favourite is to use just two translations together.

In your locale:

  view_more_details_html: "View more %{link} including physicians, locations, directions, documents, and more."
  link: "clinic information"

And just one view:

%p
  = I18n.t('view_more_details_html', link: link_to(I18n.t('link'), clinic))
查看更多
手持菜刀,她持情操
3楼-- · 2019-07-20 22:51

The simplest way is using Localized Views. At first replace

%p
  = I18n.t('view_more')
  = link_to I18n.t('link'), clinic
  = I18n.t('details')

with

= render partial: 'view_more', locals: { url: some_path }

Subsequently you can create a file _view_more.en.html.haml with

%p
  View more
  = link_to 'clinic information', url
  including physicians, locations, directions, documents, and more.
查看更多
登录 后发表回答