Rails 3 Translations Within Models in Production

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



Leave a reply



Submit