--- s080703/samizdat/lib/samizdat/controllers/message_controller.rb	2008-05-10 18:35:32.000000000 +0200
+++ /usr/lib/ruby/1.8/samizdat/controllers/message_controller.rb	2008-08-18 20:01:05.000000000 +0200
@@ -9,4 +9,5 @@
 # vim: et sw=2 sts=2 ts=8 tw=0
 
 require 'samizdat/helpers/message_helper'
+require 'samizdat/helpers/guess_lang'
 
@@ -476,4 +477,5 @@
       @message.content.title,
       message(@message, :full) <<
         cut_warning.to_s <<
+        lang_warning(body).to_s <<
         '<p>' << _("Press 'Back' button to change the message.") << '</p>' <<
@@ -510,4 +512,22 @@
       cache.flush
     end
   end
+
+  def lang_warning(body)
+    if config['site']['language_guess'].to_s == 'warn'
+      old_lang = @request.language	     
+      apparent_lang = body.to_s.guess_lang
+      @request.language = @message.lang
+      message_lang = _('English') # what the selected language calls itself
+      @request.language = apparent_lang    
+
+      if ( (body.kind_of? String) and @message.lang.guessable_lang(body.length) and
+           ( apparent_lang != @message.lang) )
+        warning = '<p><strong>'+sprintf(_('Warning: you said that your text is in %s, but it seems to be in another language.'), message_lang) + '</strong></p>'
+      end	   
+      @request.language = old_lang
+    end
+    warning.to_s # '' if nil
+  end
+
 end
--- /dev/null	2008-08-15 00:06:02.612589253 +0200
+++ /usr/lib/ruby/1.8/samizdat/helpers/guess_lang.rb	2008-08-18 22:00:44.000000000 +0200
@@ -0,0 +1,65 @@
+# This is an *extremely* basic language detection algorithm. Three 
+# successfully found tokens are considered to signify a probable
+# language detection. Even fewer numbers of tokens are used as a
+# best guess for short strings.
+# 
+# TODO: For a much more sophisticated algorithm with an extremely high
+#       accuracy, try porting libtextcat to ruby.
+# WARNING: This is coded in UTF-8, not ascii.
+# WARNING: Some scripts (e.g. latin) use spaces between words: do not
+#          remove these unless you're sure you know what you're doing.
+# WARNING: If you break the functionality here, the worst that should
+#          happen is either too many or too few incorrect guesses.
+
+
+class String
+  def guess_lang
+     length = self.length
+     count = {}
+
+     # ja 
+     count['ja']=0
+     self.strip[0,1999].scan(%r{です|ます|あ|い|う|え|お|か|き|く|け|こ|さ|し|す|せ|そ|た|ち|つ|て|と|な|に|ぬ|ね|の|は|ひ|ふ|へ|ほ|ま|み|む|め|も|や|を|ア|イ|ウ|エ|オ|カ|キ|ク|ケ|コ|サ|シ|ス|セ|ソ|タ|チ|ツ|テ|ト|ナ|ニ|ヌ|ネ|ノ|ハ|ヒ|フ|ヘ|ホ|マ|ミ|ム|メ|モ|ヤ|ヲ}){ |s| count['ja'] +=1 }
+     count['ja']= count['ja'] * 2000/(length) if (0 < length and length < 2000)
+
+     # en
+     count['en']=0
+     self.strip[0,999].scan(%r{ a | the | of | in | this | that | which | and }){ |s| count['en'] +=1 }
+     count['en']= count['en'] * 1000/(length) if (0 < length and length < 1000)
+     # es
+     count['es']=0
+     self.strip[0,999].scan(%r{ y | por | el | los | del | es | de la | la | una | un }){ |s| count['es'] +=1 }
+     count['es']= count['es'] * 1000/(length) if (0 < length and length < 1000)
+     # fr
+     count['fr']=0
+     self.strip[0,999].scan(%r{é|ê| à |ç|è| une | le | du | de la | dont | la | un |[^ ]on[st] }){ |s| count['fr'] +=1 }
+     count['fr']= count['fr'] * 1000/(length) if (0 < length and length < 1000)
+     # pl
+     count['pl']=0
+     self.strip[0,999].scan(%r{ą|ć|ę|ł|ń|ś|ż|ź|Ą|Ć|Ę|Ł|Ń|Ś|Ż|Ź| w | z | ze | od | czy | są | się |cz}){ |s| count['pl'] +=1 }
+     count['pl']= count['pl'] * 1000/(length) if (0 < length and length < 1000)
+
+     count = count.to_a.sort{|a,b| b[1] <=> a[1]}
+
+     lang = 'und' # undetermined: http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes
+     if count.length > 0 
+        lang = count[0][0] if count[0][1] > 3
+     end
+     lang
+  end
+
+  # Is a given language guessable given a string of a given length?
+  # Some languages are easier to recognise than others, so the minimum
+  # recommended length varies here.
+  def guessable_lang(length = 100)
+     if length >= 200 and ['es'].include? self 
+        true
+     elsif length >= 100 and ['en','fr','pl'].include? self 
+        true
+     elsif length >= 50 and ['ja'].include? self 
+        true 
+     else 
+        false
+     end
+  end
+end
