Question

I'm developing a plugin for redmine and ran into problems at localizing my views. I'm a newbie in rails and redmine plugin development, so perhaps there's something I'm missing here. I'm using the following environment (taken from redmine info):

Environment:
  Redmine version                2.3.3.stable
  Ruby version                   1.9.3-p448 (2013-06-27) [x86_64-linux]
  Rails version                  3.2.13
  Environment                    production
  Database adapter               Mysql2

For what I know I can use the *human_attribute_name(:attribute)* method to get a localized string for my attributes, when my locale-file contains entries in activerecord.attributes.Model.xxx . I have 2 models Company and Client.

In my views I'm using for example:

  ...
  <h4><%= link_to company.name, company_path(company.id)  %> </h4>

  <b><%= Company.human_attribute_name(:branch) %></b> :
  <%= company.branch %>
  <br>
  <b><%= Company.human_attribute_name(:url) %></b>  :
  <%= company.url %>
  ...

My config/locales/de.yml looks like this:

de:
  activerecord:
    attributes:
      client:
        department: Abteilung
        email: E-mail
        fax: Fax
        first_name: Vorname
        last_name: Nachname
        phone: Telefon
        salutation: Anrede
        salutation_letter: Briefanrede
        title: Titel
      company:
        branch: Branche
        extra_information: Zusatzinformation
        mail: E-mail
        name: Name
        organisation: Organisation
        province: Ort
        state: Bundesland
        street: Straße
        url: Web
        zip_code: PLZ
    models:
      client:
        one: Kunde
        other: Kunden
      company:
        one: Unternehmen
        other: Unternehmen
  helpers:
    label:
      client:
        department: Abteilung
        email: E-mail
        fax: Fax
        first_name: Vorname
        last_name: Nachname
        phone: Telefon
        salutation: Anrede
        salutation_letter: Briefanrede
        title: Titel
      company:
        branch: Branche
        extra_information: Zusatzinformation
        mail: E-mail
        name: Name
        organisation: Organisation
        province: Ort
        state: Bundesland
        street: Straße
        url: Web
        zip_code: PLZ
    submit:
      create: "Erstelle %{model}"
      search: Suchen
      update: "Aktualisieren %{model}"
  search: Suchen
  show_all: "Alles anzeigen"
  • The activerecord.attributes part doesn't work. *human_attribute_name* just returns the standard attribute name (i.e. :branch => "branch").
  • The activerecord.models part works.. so i.e. a submit button in companies/new is labeled "Erstelle Unternehmen"
  • The label.helpers part of the locale works, so in form_for tags the attributes are localized (i.e. :branch => "Branche").
  • The "general" part (i.e. show_all => "Alles anzeigen") works, too.
  • Without the label.helpers portion, the form_for tags stop being localized.

The translations seems to work, but the attributes part must have some error I can't locate. I threw my de.yml at http://yamllint.com/ and it seems to be valid yaml (so no yaml formatting issue).

Can anybody tell me what is wrong with my view/translation?

Was it helpful?

Solution

Solved it by using a helper: app/helpers/companies_helper.rb:

module CompaniesHelper
  def t_att(model, attribute)
    t("activerecord.attributes.#{model}.#{attribute}")
  end
end

so in the view you can use

<p>
<%= t_att(:company, :branch) %> :
<%= company.branch %>
</p>

So you get Branche : xyz

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top