'Function to get database strings from csv files
which are to be considered for translation.'

@staticmethod
def get_database_strings(all_template_flag):

    from s3import import S3BulkImporter
    database_strings = []
    dappend = database_strings.append
    template_list = []
    base_dir = current.request.folder
    path = os.path
    if all_template_flag:
        template_dir = path.join(base_dir, 'modules', 'templates')
        files = os.listdir(template_dir)
        tappend = template_list.append
        for f in files:
            curFile = path.join(template_dir, f)
            baseFile = path.basename(curFile)
            if path.isdir(curFile):
                tappend(baseFile)
    else:
        template_list.append(current.deployment_settings.base.template)
    always_translate = ('project_beneficiary_type_id', 'stats_demographic_id')
    bi = S3BulkImporter()
    S = Strings()
    read_csv = S.read_csv
    for template in template_list:
        pth = path.join(base_dir, 'modules', 'templates', template)
        if (path.exists(path.join(pth, 'tasks.cfg')) == False):
            continue
        bi.load_descriptor(pth)
        s3db = current.s3db
        for csv in bi.tasks:
            if (csv[0] != 1):
                continue
            translate = False
            fieldname = ('%s_%s_id' % (csv[1], csv[2]))
            if (fieldname in always_translate):
                translate = True
                represent = Storage(fields=['name'])
            elif (hasattr(s3db, fieldname) is False):
                continue
            else:
                reusable_field = s3db.get(fieldname)
                if (reusable_field and isinstance(reusable_field, S3ReusableField)):
                    represent = reusable_field.attr.represent
                    if hasattr(represent, 'translate'):
                        translate = represent.translate
            if translate:
                if (hasattr(represent, 'fields') is False):
                    fields = ['name']
                else:
                    fields = represent.fields
                csv_path = csv[3]
                try:
                    data = read_csv(csv_path)
                except IOError:
                    continue
                title_row = data[0]
                idx = 0
                idxlist = []
                idxappend = idxlist.append
                for e in title_row:
                    if (e.lower() in fields):
                        idxappend(idx)
                    idx += 1
                if idxlist:
                    line_number = 1
                    for row in data[1:]:
                        line_number += 1
                        for idx in idxlist:
                            try:
                                s = row[idx]
                            except:
                                current.log.error('CSV    row    incomplete', csv_path)
                            if (s != ''):
                                loc = ('%s:%s' % (csv_path, line_number))
                                dappend((loc, s))
    return database_strings
