Variable use and interpolation inside Rails localization file

mastaBlasta

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.

Shadwell

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Vue - Variable inside interpolation

From Dev

Use variable in localization strings

From Dev

Variable interpolation inside Map directive

From Dev

use javascript variable inside <% %> in erb - Ruby on Rails

From Dev

Rails - passing a variable to string interpolation

From Java

Use variables inside variable file in Ansible

From Dev

MATLAB Integration variable inside interpolation function

From Dev

Rails - Variable inside the find()

From Dev

how do I use unix variable inside an HTML file

From Dev

Use Defined Variable with egrep to Match Contents Inside Text File

From Dev

Access local template variable using interpolation inside *ngIf, Angular 2

From Dev

Use variable inside regex

From Dev

Ruby: how to use gsub method on symbol inside string interpolation #{}

From Dev

Localization inside JavaScript

From Dev

Localization inside JavaScript

From Java

C#6.0 string interpolation localization

From Dev

Localization of numeric number in Rails

From Dev

How can I delay variable interpolation in a regex to the point of use?

From Dev

use variable inside regex python

From Dev

Use variable inside Hugo content

From Dev

Use public variable in inside function

From Dev

Rails use of yml file

From Dev

Batch file variable inside if block

From Dev

Replacing a variable inside a netCDF file

From Dev

block inside interpolation with ruby

From Dev

Interpolation inside of ng-if

From Dev

How to use the variable inside another variable

From Dev

Use a variable reference "inside" another variable

From Dev

How to use an awk variable inside an awk variable?