yiqun
yiqun

Reputation: 347

The I18N is abnormal when my rails app in production environment

I write a rails app in my local machine(OSX 10.6.6,Rails 3.0.1,WEBrick),All I18n files work fine.when i push the app to my web server(Centos5.4,Rails 3.0.1,Phusion Passenger version 3.0.2) I get a error"Translation missing: en, username_exist"

My model code:

validates_uniqueness_of :username, :on => :create, :message => I18n.t("username_exist")

My i18n file(config/locales/en.yml):

en:
   username_exist: "username exist"

I think the model not found i18n file?

Upvotes: 0

Views: 636

Answers (2)

John
John

Reputation: 30235

When you use I18n.t at class scope, as you are doing in the call to validates_uniqueness_of, it will be evaluated when the file is loaded. That's probably not what you want, because it means the message will always appear in the default locale, rather than using the locale of the user making each request. It may also be causing the problem you are seeing, if the file is loaded before I18n is configured.

Instead, use a symbol:

validates_uniqueness_of :username, ..., :message => :username_exist

And see the documentation for ActiveModel::Errors#generate_message to see where to place the translation in the locale file.

In fact, you don't even need to provide the :message key if you follow the ActiveModel naming convention:

en:
  activemodel:
    errors:
      models:
        [your model name, e.g. user]:
          attributes:
            username:
              taken: "username exists"

Upvotes: 2

Spyros
Spyros

Reputation: 48706

Do you set the locale in your application.rb ? Like :

config.i18n.default_locale = :en  

Upvotes: 0

Related Questions