def table_to_str(tab):
    """
    Produce a formatted string for a translation table.

    Given a table stored as a dictionary, sort the contents and return the
    table as text in the translation table format for storage.

    """
    table_str = ''
    items = list(tab.items())
    items.sort(key=lambda x: (-len(x[0]), x[0]))
    for i in items:
        table_str += i[0] + '|' + '|'.join(i[1]) + '\n'
    return table_str
