Rails 3 translations within models in production
Your I18n.t()
call is evaluated at compile time since you are defining class variables, not instance variables. You need to put your call to I18n.t where they will be evaluated at runtime.
But if you want to translate ActiveRecord field names, use human_attribute_name
and provide your translations via YML. You do not need to manually provide translations, Rails handles it all for you automatically.
The respective documentation is at http://guides.rubyonrails.org/i18n.html Chapter 5.1.
Rails model using only default locale
Solved by adding the following method in application controller:
def default_url_options(options={})
{ locale: I18n.locale }
end
Rails3 using i18n strings in model file
The answer is simpler (and as I expected, rather succinct)
def description
I18n.t('products.name', :locale => user_language)
end
Note the capital 'I' in I18n. I stayed stuck on this for quite a while. Most references I've seen use 1st character lowercase => i18n, but that generates an
undefined local variable or method `i18n'
Specific activerecord attributes becomes translation missing on production with Rails i18n
I've found the solution.
There was a locale file which has no content on attributes.
ja:
activerecord:
models:
authentication: SNS連携
attributes:
This file affects other files.
I18n translated values do not show up if changed from helper method rails
Because you declared the translations as a constant in your model, so the translations were loaded just one time when Answer
model was loaded in the first time.
In production
environment, the model will be loaded after starting server. In development
environment, the model will be loaded when the file content is changed, ...
In order to solve your issue, you can add the translations in your helper instead:
module AnswerHelper
def get_employment_type_list
[
['Employed', I18n.t('self_evaluation.self_evaluation_form.employment_status.employed')],
['Self-Employed', I18n.t('self_evaluation.self_evaluation_form.employment_status.self_employed')],
['Unemployed / Retired', I18n.t('self_evaluation.self_evaluation_form.employment_status.unemployed')]
]
end
end
I usually use this approach, please have a look!
class Answer < ActiveRecord::Base
EMPLOYMENT_TYPE = [:employed, :self_employed, :unemployed_or_retired]
end
module AnswerHelper
def get_employment_type_list
i18n_scope = 'self_evaluation.self_evaluation_form.employment_status'
Answer::EMPLOYMENT_TYPE.map do |type|
[type, t(type, scope: i18n_scope)]
end
end
end
Automate translations of dynamic content within DB on Rails
Here's a quick way of doing it:
In a helper module:
def pretranslate(str)
md = str.match(/\[translate\[([^\]]*)\]\]/)
str.gsub(md[0], translate(md[1]))
end
In your controller:
@text = %Q{
And the following piece of text will be translated using the helper: [translate[mary had a little lamb]]
}
In your view:
<%= pretranslate(@text) %>
This is obviously pretty hacky. It's not really safe by nature, but shouldn't be too dangerous, depending on what you do with the input in your translate
method.
Related Topics
Capistrano Deploy Fails After I Changed the Repository Url
Listen Error: Unable to Monitor Directories for Changes
Programming a Simple Irc (Internet-Relay-Chat) Client
Sorting an Array of Objects in Ruby by Object Attribute
Rspec Controller Testing - Blank Response.Body
Seahorse::Client::Networkingerror Amazon S3 File Upload with Rails
How to Know If an Io Is Empty Without Reading It
Encrypt Data Bag from Inside of Ruby Without Relying on Knife
Watir and Text Field Inside Iframe
Convert This Xml Request to a Proper Savon Request
Nomethoderror: Undefined Method '[]' for #<Activerecord::Migration:0X000000033Fedc0>
Ruby on Rails View Rendering Db Info on Page
Regex to Remove the Webpage Part of a Url in Ruby
Rspec - How to Test Activerecord::Recordnotfound