Send notification messages in the target user's language.
authorTom Hughes <tom@compton.nu>
Sun, 31 May 2009 16:10:53 +0000 (16:10 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 31 May 2009 16:10:53 +0000 (16:10 +0000)
app/models/notifier.rb
app/models/user.rb
config/initializers/action_mailer.rb

index c2ff4c53a740edef47fb067436120eaed14bd936..b291c2b57de027d6dbc3d89b5b5f06f24df5bf3a 100644 (file)
@@ -108,6 +108,7 @@ private
 
   def common_headers(recipient)
     recipients recipient.email
+    locale recipient.preferred_language_from(I18n.available_locales)
     from "webmaster@openstreetmap.org"
     headers "return-path" => "bounces@openstreetmap.org",
             "Auto-Submitted" => "auto-generated"
index f38994e534596a71a9abf34bf3599698aab68502..683fcc96baceea7cb6453212e7c9ac912b964971 100644 (file)
@@ -90,6 +90,10 @@ class User < ActiveRecord::Base
     languages.find { |l| Language.find(:first, :conditions => { :code => l }) }
   end
 
+  def preferred_language_from(array)
+    (languages & array.collect { |i| i.to_s }).first
+  end
+
   def nearby(radius = 50, num = 10)
     if self.home_lon and self.home_lat 
       gc = OSM::GreatCircle.new(self.home_lat, self.home_lon)
index db6c1b9b7bcff789595643063f35ede3941521a3..37510ac27890195411eca82643333c2552da7563 100644 (file)
@@ -14,3 +14,25 @@ module Net
     end
   end
 end
+
+# Monkey patch to allow sending of messages in specific locales
+module ActionMailer
+  class Base
+    adv_attr_accessor :locale
+  private
+    alias_method :old_render_message, :render_message
+
+    def render_message(method_name, body)
+      old_locale= I18n.locale
+
+      begin
+        I18n.locale = @locale
+        message = old_render_message(method_name, body)
+      ensure
+        I18n.locale = old_locale
+      end
+
+      message
+    end
+  end
+end