haml, link_to helper, and I18n in the same sentence

给你一囗甜甜゛ 提交于 2019-12-11 01:28:40

问题


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?


回答1:


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.



回答2:


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))


来源:https://stackoverflow.com/questions/23001268/haml-link-to-helper-and-i18n-in-the-same-sentence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!