How Do I Use Localization (i18n) with Haml in Middleman - localization

The examples on Middleman's web page are in ERB, but I like to use HAML.
http://middlemanapp.com/advanced/localization/
This is their example localization YAML file:
---
es:
hello: "Hola"
And this is how they use ERB to access it.
<%= I18n.t(:hello) %> World
But I prefer HAML to ERB. How does the above line translate into HAML?

This would simply be :
= I18n.t(:hello)
World
You might want to put the greeting in your localization file though :
es:
hello: "Hola %{name}"
and call it from your HAML like this
= I18n.t(:hello, :name => 'World')

Related

In my haml file, I want to include a link within a string that is coming from a yml file

Here's my situation:
I have this in my haml file, ...it's a start:
.image-credits
%div
%h2
= t(:image_credits)
%ul
%li
%p
= t(:photographer_1)
And then in my yml file I have these strings defined:
image_credits : "Image Credits"
photographer_1 : "\"Miss Kitty\" by Mike Henderson"
photographer_1_link : "https://www.flickr.com/photos/mikehenderson/3750940230"
Ideally, I would like it if it was rendered like this:
"Miss Kitty" by Mike Henderson
Any suggestions on how to do that? I've been googling around but I don't think I'm searching for the right thing.
You need to do two things:
Your photographer_1 string needs to be rendered as HTML, which you signal to Rails by appending _html to the key (so it's now photographer_1_html (http://guides.rubyonrails.org/i18n.html#using-safe-html-translations)
You need to pass a parameter to this key, which would be the link you want inserted. Because links are the same in all locales, the link can be in your HAML. The parameters are passed as options in the t() method.
So you end up with:
# in en.yml
photographer_1_html : "<a href='%{link}'>\"Miss Kitty\" by Mike Henderson</a>"
# in yourview.html.haml
= t(:photographer_1_html,
link:"https://www.flickr.com/photos/mikehenderson/3750940230")

How to use the HAML t() helper in RoR

I'm developing an application with Rails 4 and I want to use the helper t() with HAML, but I can not
Example usage:
=model_name = #article.class.model_name.human
%h1 Create t(:model_name)
My first line shows me the correct article, but when I use t() it shows me t(:model_name). Thanks.
hope in translational file you have en.yml
en:
article: MyArticle
Then
%h2= t 'article'
if you want dynamically
%h2= t '#{model_name}'

Rails: i18n, variable gets printed if translation is not there

I am facing a weird problem. In my rails 3 app I am supporting internationalization with english and french. Here in my template I wrote something like this
<%= t "Hi %{person}!", :person => "Simpson" %>
When I set locale as french everything works fine, since it has got translation for this but when I set locale as english then it gives output as
Hi %{person}!
in my browser. When I add translation to en.yml, it works fine. I don't understand why there is need to add translation in en.yml for this. Moreover I don't want this to happen, is there any work around for this?
Thanks
The first argument givent to the t method should be a key, so your view should have something like this :
<%= t :greetings, :person => "Simpson" %>
Your config/locales/en.yml would look like this :
en:
greetings: Hi %{person}
and your config/locales/fr.yml something like this :
fr:
greetings: Bonjour %{person}

Rails: render ruby logic in i18n string

I'm currently translating a application and i've stumbled upon a problem. I'm using this in my views:
t("page.text")
and i have this is my yaml file:
page:
text: "This is my tekst with a #{link_to "pages index, pages_path}"
This string gets outputted without the link_to logic in it, like this: "This is my tekst with a #{link_to "pages index, pages_path}". This is not what i want, i want the string parsed with the link_to function like this: "This is my tekst with a pages index" where pages index links to the /pages route...
Thanks.
Rails's i18n API allows you to use variable-based interpolation in your YAML file. To do what you're trying to do:
# config/en.yml
page:
text: "This is my test with a %{link}"
# view.html.erb
<%= t("page.text", :link => link_to("pages index", pages_path))
You can read more about it in the official Rails Guide: http://guides.rubyonrails.org/i18n.html#interpolation

i18n on Ruby on Rails, < and > gets replaced by &gt ; &lt ; when not intended

I am creating locale files for internationalization in a rails app, and have a url that I want translated with tags included , for example
html.erb
<%= t(foo.bar.xxxx) %>
yml file
foo: bar:
xxxx: "xxxx"
result
&lt ;a href=
"/info/index.html"&gt ;xxxx</a&gt ;
which breaks my links. I do not have an h on the ruby part, so shouldn't this work?
Or should I just not have html tags within the yml file?
Rails version is 3.0.1
Ruby version is 1.8.7 p249
Your HTML YAML keys need to have a _html suffix:
foo:
bar:
xxxx_html: "<strong>Some HTML Here</strong>"
Doing this Rails will mark the string has html_safe and will render out the HTML instead of converting it to > and <.
You need to reference it with the full key name as well, Rails doesn't automatically see the _html suffix when you call xxxx.
<%= t 'foo.bar.xxxx_html' %>
Rails is preventing injection attacks by preventing model data from being displayed as actual markup. The raw function prevents that conversion.
Does
<%= raw t(foo.bar.xxxx) %>
work?

Resources