I'm using Rails 2.3.6 and I18n. Everything works fine except that I18n is using the wrong locale when sending emails. In fact it renders the following code using en.yml instead of it.yml, although my default locale is it.
Can you help me?
class UserMailer < ActionMailer::Base
default_url_options[:host] = GAME_CONFIG["domain"]
def password_reset_instructions(user)
subject I18n.translate("email_messages.lost_password.subject")
from "#{GAME_CONFIG["name"]} <no-reply##{GAME_CONFIG["domain"]}>"
recipients user.email
sent_on Time.now
body :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
end
end
In my production server if I try to check for the current locale I get:
I18n.default_locale
:it
I18n.locale
:it
I discovered that this is normal behavior and I can fix this specifing :locale => ":it"
Related
I can't seem to get mail_room working in a Rails app.
When testing the logger delivery method, it works correctly with this
config/mail_room.yml
---
:mailboxes:
-
:email: "some#gmail.com"
:password: "password"
:name: "inbox"
:delivery_method: logger
:log_path: "email.log"
but the postback delivery method doesn't seem to work at all
config/mail_room.yml
---
:mailboxes:
-
:email: "some#gmail.com"
:password: "password"
:name: "inbox"
:delivery_method: postback
:delivery_url: "http://global-or-local-ip/inbox"
:delivery_token: "abcdefg"
config/routes.rb
post 'inbox', :to => 'users#inbox', :as => :users_inbox
app/controllers/users_controller.rb
class UsersController < ApplicationController
def inbox
puts "Check your inbox..."
end
end
Not sure if it's because of mail_room or something missing from the Rails app. I've tried different verbs in the routes. Using Rails 4.0.2 and tried both 0.1.0 and github source for the gem.
After some research, we found that Faraday 0.8.8 was having some issues with ruby 2.0 as we were using it in mail_room. Released a new gem, and made notes in the README that users should install >= 0.8.9 of Faraday.
I'm using Rails 3 with Globalize3 0.2.0.beta4
Ideally I need :fr to fallback to :en and vice versa.
There are cases when only a French translation is available and I need to show it even if the locale is :en.
I tried
config.i18n.fallbacks = { :fr => :en, :en => :fr }
but somewhat unsurprisingly it causes a stack level too deep error.
I'm changing my answer.
To enable fallbacks, add the following to your environment.rb file:
#support for locale fallbacks
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
Then, you can enable circular fallbacks like you were trying to, eg:
config.i18n.fallbacks = {'en' => 'fr', 'fr' => 'en'}
In this case, if something is missing in the en locale, it'll check the fr locale, and then the other way around. I don't get any errors running this.
Source: http://batsov.com/articles/2012/09/12/setting-up-fallback-locale-s-in-rails-3/
If you pass an array of locales they will be set as default fallbacks for all locales.
config.i18n.fallbacks = [:en, :fr]
Unfortunately, I haven't found a way to set up just two locales to fall back to each other.
In the end I monkey patched Globalize3. Not great as I have to update the patch whenever the site needs a new locale, but hey, it worked.
module Globalize
class << self
def fallbacks(locale = self.locale)
case locale
when :en then [:en, :fr]
when :fr then [:fr, :en]
end
end
end
end
This seems to have changed to this:
Globalize.fallbacks = {:en => [:en, :fr], :fr => [:fr, :en]}
Got from the official docs:
https://github.com/globalize/globalize#fallback-locales-to-each-other
In latest i18n gem (0.7.0) I have found it necessary to define fallback locales like this (in config/application.rb):
# Custom I18n fallbacks
config.after_initialize do
I18n.fallbacks = I18n::Locale::Fallbacks.new(at: :"de-DE", ch: :"de-DE", gb: :"en-US")
end
You also need to set config.i18n.fallbacks = true in all config/environments/*.rb files.
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?
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"
Do you set the locale in your application.rb ? Like :
config.i18n.default_locale = :en
I'm having trouble getting i18n to work on heroku.
I set:
I18n.default_locale = :de
in my environment.rb
and the translation is in config/locales/de.yml
works perfect on my local machines but not so on Heroku.
On heroku everything is in english.
I don't think I need a special gem like i18n gem, cause I don't have it on my local machine either.
Maybe someone has a solution to this?
Try to set the default local like this in your config.environment.rb:
Rails::Initializer.run do |config|
# ...
config.i18n.default_locale = :de
end
The only solution I found that worked for me, on heroku, was setting it manually in the application controller.
application_controller.rb
before_filter :set_locale
def set_locale
I18n.locale = 'fr-QC' || I18n.default_locale
end
cheers
Is it possible to use ActionMailer in a web framework like Ramaze, or do I need to use Rails?
You can use ActionMailer without Rails quite easily. I'm not familiar with Ramaze, but here's plain ruby, which should be easy to integrate into whatever framework you wish:
PATH/mailer.rb
require 'rubygems'
require 'action_mailer'
class Mailer < ActionMailer::Base
def my_email
recipients "recipient#their_domain.com"
from "me#my_domain.com"
subject "my subject"
body :variable1 => 'a', :variable2 => 'b'
end
end
Mailer.template_root = File.dirname(__FILE__)
Mailer.delivery_method = :sendmail
Mailer.logger = Logger.new(STDOUT)
# this sends the email
Mailer.deliver_my_email
Then put the email templates in a directory named after the your ActionMailer class
PATH/mailer/my_email.html.erb
variable 1: <%= #variable1 %>
variable 2: <%= #variable2 %>
Check out the API Docs for more configuration options, but those are the basics