I\'d like to i18n a text that looks like this:
Already signed up? Log in!
Note that there is a link on the text. On this example
Separating text and link in locale.yml file works for a while but with longer text those are hard to translate and maintain as the link is in separate translation-item (as in Simones answer). If you start having many strings/translations with links you can dry it a bit more.
I made one helper in my application_helper.rb:
# Converts
# "string with __link__ in the middle." to
# "string with #{link_to('link', link_url, link_options)} in the middle."
def string_with_link(str, link_url, link_options = {})
match = str.match(/__([^_]{2,30})__/)
if !match.blank?
raw($` + link_to($1, link_url, link_options) + $')
else
raise "string_with_link: No place for __link__ given in #{str}" if Rails.env.test?
nil
end
end
In my en.yml:
log_in_message: "Already signed up? __Log in!__"
And in my views:
<%= string_with_link(t('.log_in_message'), login_path) %>
This way it's easier to translate messages as also the link text is clearly defined in the locale.yml-files.