#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


from rapidsms.contrib.handlers.handlers.keyword import KeywordHandler
from rapidsms.models import Contact
from rapidsms.conf import settings


class LanguageHandler(KeywordHandler):
    """
    Allow remote users to set their preferred language, by updating the
    ``language`` field of the Contact associated with their connection.
    """

    keyword = "language|lang"

    def help(self):
        self.respond("To set your language, send LANGUAGE <CODE>")

    def handle(self, text):
        if self.msg.connection.contact is None:
            return self.respond_error(
                "You must JOIN or REGISTER yourself before you can " +
                "set your language preference.")

        t = text.lower()
        for code, name in settings.LANGUAGES:
            if t != code.lower() and t != name.lower():
                continue

            self.msg.connection.contact.language = code
            self.msg.connection.contact.save()

            return self.respond(
                "I will speak to you in %(language)s.",
                language=name)

        return self.respond_error(
            'Sorry, I don\'t speak "%(language)s".',
            language=text)

@@ -46,16 +46,51 @@
  sw=4%0A%0A%0A
+from rapidsms.conf import settings%0A
 from rap
@@ -155,79 +155,8 @@
 ler%0A
-from rapidsms.models import Contact%0Afrom rapidsms.conf import settings%0A
 %0A%0Acl
@@ -1052,17 +1052,20 @@
 uage)s.%22
-,
+ %25 %7B
 %0A       
@@ -1073,24 +1073,25 @@
         
+'
 language
 =name)%0A%0A
@@ -1086,13 +1086,16 @@
 uage
-=
+': 
 name
+%7D
 )%0A%0A 
@@ -1179,17 +1179,20 @@
 age)s%22.'
-,
+ %25 %7B
 %0A       
@@ -1196,23 +1196,24 @@
         
+'
 language
 =text)%0A
@@ -1209,11 +1209,14 @@
 uage
-=
+': 
 text
+%7D
 )%0A

Fix registration test