Smart localisation

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!

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.

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.

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:

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

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.

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

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