Skip to content

Commit

Permalink
add nsms.lang app which takes care of activating and setting profile …
Browse files Browse the repository at this point in the history
…languages
  • Loading branch information
nicpottier committed Apr 26, 2012
1 parent 6e16beb commit 1da8185
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
Empty file added nsms/lang/__init__.py
Empty file.
90 changes: 90 additions & 0 deletions nsms/lang/app.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,90 @@
from rapidsms.apps.base import AppBase
from nsms.parser import Parser
from nsms.text.models import gettext as _
from nsms.utils import get_sms_profile
from django.conf import settings
from django.utils import translation
import re

# hackery to make sure our translation database contains all our the strings used in this app
_('lang-current-lang', "Your language is set to {{ language }}.")
_('lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.")
_('lang-set-success', "Success, your language is now set to {{ language }}.")

class App(AppBase):
"""
This app is responsible for setting and changing a user's preferred language.
In order for this app to do it's magic, you need to define what your SMS profile in
your settings_common.py:
SMS_PROFILE = 'chws.chw'
The language app will respond to messages that begin with the keyword 'lang'. Without
an argument it will respond with the currently selected language, with an argument it
will try to find a configured language with that name and set it, eg:
lang en[_us]
lang rw
"""
def handle (self, message):
profile = get_sms_profile(message.connection)

# handle activating the appropriate SMS language

# if this connection has a profile, use that language
if profile:
translation.activate(profile.language)

# otherwise use the default SMS language
else:
translation.activate(settings.DEFAULT_SMS_LANGUAGE)

parser = Parser(message.text)

# check whether this is a lang message
keyword = parser.next_keyword(['lang'])
if keyword:
if not profile:
message.respond(_('lang-unknown-user', "Sorry, your phone number is not registered."))
return True

# just querying the language
language = parser.next_word()
if not language:
message.respond(_('lang-current-lang', "Your language is set to {{ language }}.", dict(language=profile.get_language_display())))
return True

else:
# lowercase the language they passed us
language = language.lower()

lang_mapping = dict()
for lang in settings.LANGUAGES:
code = lang[0].lower()
lang_mapping[code] = code

parts = code.split('_')
if not parts[0] in lang_mapping:
lang_mapping[parts[0]] = code

# this language doesn't exit
if not language.lower() in lang_mapping:
message.respond(_('lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.", dict(code=language.lower())))
return True

profile.language = lang_mapping[language.lower()]
profile.save()

# activate the new language
translation.activate(profile.language)
message.respond(_('lang-set-success', "Success, your language is now set to {{ language }}.", dict(language=profile.get_language_display())))

return True

return False





33 changes: 33 additions & 0 deletions nsms/utils/__init__.py
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,37 @@
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.conf import settings
import sys

def import_from_string(kls):
"""
Used to load a class object dynamically by name
"""
parts = kls.split('.')
module = ".".join(parts[:-1])
m = __import__(module)
for comp in parts[1:]:
m = getattr(m, comp)
return m

def get_sms_profile(connection):
"""
Given a connection, looks up the SMS profile using the SMS_PROFILE model registered in settings.py
"""
profile_class_name = getattr(settings, 'SMS_PROFILE', None)

# no SMS_PROFILE set, then return nothing
if not profile_class_name:
return None

profile_class = import_from_string(profile_class_name)

# see if we can find a match for that connection
matches = profile_class.objects.filter(connection=connection)
if matches:
return matches[0]

else:
return None


def get_connection_user(connection): def get_connection_user(connection):
""" """
Expand Down

0 comments on commit 1da8185

Please sign in to comment.