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