# Smart localisation

**URL:** <https://www.ruby-forum.com/t/smart-localisation/187256>\
**Category:** Rails\
**Created:** [April 20, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/smart-localisation/187256 "2010-04-20T17:25:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 20, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/smart-localisation/187256/1 "2010-04-20T17:25:55Z")

</div>

Hi,

I have some trouble using Il8n. I’ve had these problems for a while  
now and always used rough workarounds, but as my productivity needs to  
go up another notch, I was wondering whether there was any way of  
avoiding these workarounds.

## For example: When I’m inside a view (say ‘home/index’) and use t(‘title’), I’d like Rails to know I’m looking for the last line in this en.yml file:

## en: home: index: title: “Home”

I’ve achieved this by using a plugin, but the plugin doesn’t always do  
the trick (for example, it can’t be used in a layout file). Is my  
piece of YAML above correct? And should it work?

Also, I’d like to be able to set a default title that can be  
overridden by defining it in a controller action translation. So there  
should also be a “base” title variable.

Furthermore, I’d also like Rails to know that, when I do this:

\<% form\_for @user do |f| %\>  
\<%= f.label :name %\>  
\<%= f.text\_field :name %\>  
\<%- end -%\>

## It should look for the following translation to use as the label value:

## en: activerecord: attributes: user: name: “Full Name”

Why isn’t it doing this? Isn’t that what Il8n was meant to do?

I’m probably making mistakes, so feel free to point out to me where  
I’m making them.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 20, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/smart-localisation/187256/2 "2010-04-20T21:03:44Z")

</div>

Oh an to be complete about the first thing: I know I can get the  
controller/action translations by doing  
t(‘controller.action.translation’), but I’d like to integrate my  
localisation more into my application. Of course I could write a  
method to extend t(), but I’ve done stuff like that before only to  
find out Rails already has functionality for it. I just don’t know  
where it is.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 21, 2010, 12:13am UTC](https://www.ruby-forum.com/t/smart-localisation/187256/3 "2010-04-21T00:13:27Z")

</div>

## If I should believe the things I find on the internet, this is the correct approach for form labels:

## en: activerecord: attributes: user: last\_name: Surname

And then:

\<% form\_for @user do |f| %\>  
\<%= f.label :last\_name %\>  
\<%= f.text\_field :last\_name %\>  
\<% end %\>

However, this results in a form label “Last name” instead of  
“Surname”. What am I doing wrong? I’m on Rails 2.3.5.

## Also, to expand on my first problem a bit, I’d like to have the following en.yml:

## en: title: “My Website” home: index: title: “Home” users: new: title: “New User”

Now, if I go to /home/index, I’d like my title to be “Home”. If I got  
to /users/new, I’d like my title to be “New User”. If I go to any  
other page, the title’d be “My Website”. I’ve read up on this and I  
thought I had the right approach by simply putting t(:title) in the  
title tag of my layout, but that doesn’t seem to work. What is the  
right approach?

Of course, I could always make a helper:

def display\_title  
title = t("#{params[:controller]}.#{params[:action]}.title")  
title = t(:title) if title.blank?  
end

However, Rails is so friggin’ powerful, I can’t believe there’s  
nothing built in for both my problems.

I’d really appreciate it if anyone could think with me. Thanks in  
advance.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 21, 2010, 10:07am UTC](https://www.ruby-forum.com/t/smart-localisation/187256/4 "2010-04-21T10:07:35Z")

</div>

Hi,

> \<% form\_for @user do |f| %\>  
> \<%= f.label :last\_name %\>  
> \<%= f.text\_field :last\_name %\>  
> \<% end %\>
> 
> However, this results in a form label “Last name” instead of  
> “Surname”. What am I doing wrong? I’m on Rails 2.3.5.

For the moment I think this is the normal behavior but it will be  
available with Rails3:

> **[rails.info](http://guides.rails.info/3_0_release_notes.html#internationalization)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

For the moment, what I do is :

\<% form\_for @user do |f| %\>  
\<%= f.label t(‘activerecord.attributes.user.last\_name’) %\>  
\<%= f.text\_field :last\_name %\>  
\<% end %\>

Not so clean but it works…

@+  
@lex

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 21, 2010, 12:21pm UTC](https://www.ruby-forum.com/t/smart-localisation/187256/5 "2010-04-21T12:21:04Z")

</div>

Hmmm, thank you both. A fix for the labels appears to be in the latest  
version of the Rails 2-3-stable branch. Is there any way to integrate  
that version of Rails into my application? Please mind that I won’t  
have access to the Rails installation on the target server, so I need  
to overwrite the Rails library locally somehow.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 21, 2010, 11:45am UTC](https://www.ruby-forum.com/t/smart-localisation/187256/6 "2010-04-21T11:45:22Z")

</div>

For the label problem I have been using the I18n-label plugin in the  
past:

> **[GitHub - iain/i18n\_label: Adds translated form labels to Rails 2.2](https://github.com/iain/i18n_label)**
>
> Adds translated form labels to Rails 2.2. Contribute to iain/i18n\_label development by creating an account on GitHub.

This functionality may be integrated into the latest version of I18n,  
I dont know.
