Search code examples
ruby-on-railsinternationalizationhamlyaml

haml, link_to helper, and I18n in the same sentence


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?


Solution

  • 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.