Exemplo n.º 1
0
def pack(rows, char_tbl, h_fn):
    """Generate a C included file listing an array that packs multilanguage
    messages.

    The Output Format
    -----------------
    MLangHeader LangMsg^L
        L: the total number of languages
    MLangHeader: MsgCounterPerLang LangCount LangOffset^(L+1)
    LangMsg: MsgOffset^(M+1) Msg^M
        M: the total number of messages
    """
    def lang_offsets(langs, mlang_tbl):
        """Return language offsets.
        """
        msg_total = len(mlang_tbl['ID'])
        langs = [lang.upper() for lang in langs]
        lens = [msg_total+1+len(''.join(mlang_tbl[lang])) for lang in langs]
        return offsets_from_lens(lens)

    def msg_offsets(msgs):
        """Return message offsets.
        """
        return offsets_from_lens([len(x) for x in msgs])

    def char_idx_str_from_msg(msg, char_tbl):
        """Generate the string of indexes of chars in a message.
        """
        return array_str_from_ints([char_tbl[c] for c in msg])

    langs = get_lang_names(rows)
    mlang_tbl = gen_mlang_tbl(rows)

    lines = [''] * 2
    lines += ['%4d,   // the total messages of a language'
                % len(mlang_tbl['ID'])]
    lines += ['%4d,   // the total number of languages' % len(langs)]
    lines += ['', '// The offsets of languages']
    lines += [array_str_from_ints(lang_offsets(langs, mlang_tbl))]
    for lang in langs:
        msgs = mlang_tbl[lang.upper()]
        lines += ['', '// %s message offsets' % lang]
        lines += [array_str_from_ints(msg_offsets(msgs))]
        lines += ['', '// %s messages' % lang]
        if lang.upper() == 'ARABIC':
            msgs = [arabic.shape(m) for m in msgs]
        lines += [char_idx_str_from_msg(m, char_tbl) for m in msgs]

    lines = prefix_authorship(lines, comment_mark='//')
    save_utf8_file(h_fn, lines)
Exemplo n.º 2
0
 def get_mlang_records(rows):
     """Get records without ID column
     """
     heads, records = rows[0], rows[1:]
     heads = [h.upper() for h in heads]
     try:
         i = heads.index('ARABIC')
         records = [list(r) for r in records]
         for r in records:
             r[i] = arabic.shape(r[i])
     except ValueError:
         pass
     i = heads.index('ID')
     return [r[:i] + r[i+1:] for r in records]