'If the translation spreadsheet has been uploaded then
it needs to be processed.
The translation strings need to be extracted from
the spreadsheet and inserted into the language file.'

@staticmethod
def translate_onaccept(form):

    if ('file' in form.vars):
        try:
            import xlrd
        except ImportError:
            print >>sys.stderr, 'ERROR:    xlrd    &    xlwt    modules    are    needed    for    importing    spreadsheets'
            return None
        from gluon.languages import read_dict, write_dict
        T = current.T
        request = current.request
        response = current.response
        msg_none = T('No    translations    exist    in    spreadsheet')
        upload_file = request.post_vars.file
        upload_file.file.seek(0)
        open_file = upload_file.file.read()
        lang = form.record.language
        code = form.record.code
        try:
            workbook = xlrd.open_workbook(file_contents=open_file)
        except IOError:
            msg = T('Unable    to    open    spreadsheet')
            response.error = msg
            response.flash = None
            return
        try:
            language_sheet = workbook.sheet_by_name(lang)
        except IOError:
            msg = (T('Unable    to    find    sheet    %(sheet_name)s    in    uploaded    spreadsheet') % dict(sheet_name=lang))
            response.error = msg
            response.flash = None
            return
        if (language_sheet.ncols == 1):
            response.warning = msg_none
            response.flash = None
            return
        count = 0
        lang_filename = ('applications/%s/uploads/survey/translations/%s.py' % (request.application, code))
        try:
            strings = read_dict(lang_filename)
        except IOError:
            strings = {}
        for row in xrange(1, language_sheet.nrows):
            original = language_sheet.cell_value(row, 0)
            translation = language_sheet.cell_value(row, 1)
            if ((original not in strings) or (translation != '')):
                strings[original] = translation
                count += 1
        write_dict(lang_filename, strings)
        if (count == 0):
            response.warning = msg_none
            response.flash = None
        else:
            response.flash = (T('%(count_of)d    translations    have    been    imported    to    the    %(language)s    language    file') % dict(count_of=count, language=lang))
