Is either of these possible (and if so how):

1.) Use a previously defined entry in the localization file, in another place in the localization file

en:
  account:
    label: "MegaCorp"
  user:
    creation: "Welcome aboard user of {{en.account.label}}"

 # => Welcome aboard user of MegaCorp

2.) Use a globally available method.

class Account
  def self.to_s
    "MegaCorp"
  end
end

and yml

en:
  user:
    creation: "Welcome aboard user of {{Account.to_s}}"

 # => Welcome aboard user of MegaCorp

Or any other variable interpolation possibilities inside localization files. I know the standard format.

<%=t 'user.creation', account_name: "MegaCorp" %>

en:
  user:
    creation: "Welcome aboard user of %{account_name}"

But i need more than that and I wonder if there are any other undocumented tricks.

有帮助吗?

解决方案

Admittedly I'm probably stretching the scope of the question too far but ... what about using .rb files for the translations? You can do both of your points then.

1) Instead of config/locales/en.yml (say), you can have config/locales/en.rb:

account_name = 'Megacorp'

{
  en: {
    account: {
      label: account_name
    },
    user: {
      creation: 'Welcome aboard user of ' + account_name
    }
  }
}

2) Similarly you can use a class variable in en.rb:

{
  en: {
    user: {
      creation: 'Welcome aboard user of ' + Account.to_s
    }
  }
}

You don't have to use .rb files for all your translations if you prefer yaml (and most people do) but you could use it for those translations that you want to do this kind of thing on.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top