示例#1
0
def test_msg_with_basic_replace():
    msg = ICUMessageFormat.parse('''I {GREETING} this finds you well.''')

    i = ICUNodeVisitor({'GREETING': 'hope'})
    assert i.visit(msg) == 'I hope this finds you well.'

    i = ICUNodeVisitor({'GREETING': 'wish'})
    assert i.visit(msg) == 'I wish this finds you well.'

    i = ICUNodeVisitor({'GREETING': ''})
    assert i.visit(msg) == 'I  this finds you well.'
示例#2
0
def format(msg, values, lang):
    '''
    :param msg: String, The message to parse
    :param values: Dict, The values to use in forming the final message
    :param lang: String, The language code for the language to use to decide on
                 pluralisation rules
    '''

    ast = ICUMessageFormat.parse(msg)

    return format_tree(ast, values, lang)
示例#3
0
def test_msg_with_unicode_chars():
    msg = ICUMessageFormat.parse(
        '''{SYMBOL, select, snowman {☃} sun {☉} other {☹}}''')

    i = ICUNodeVisitor({'SYMBOL': 'snowman'})
    assert i.visit(msg) == '☃'

    i = ICUNodeVisitor({'SYMBOL': 'sun'})
    assert i.visit(msg) == '☉'

    i = ICUNodeVisitor({})
    assert i.visit(msg) == '☹'
示例#4
0
def test_process_select_statement():
    msg = ICUMessageFormat.parse(
        '''{WHO, select, male {He} female {She} other {They}}.''')

    i = ICUNodeVisitor({'WHO': 'male'})
    assert i.visit(msg) == 'He.'

    i = ICUNodeVisitor({'WHO': 'female'})
    assert i.visit(msg) == 'She.'

    i = ICUNodeVisitor({'WHO:': 'other'})
    assert i.visit(msg) == 'They.'
示例#5
0
def test_msg_with_basic_replace():
    msg = ICUMessageFormat.parse(
        '''I {GREETING} this finds you well.''')

    i = ICUNodeVisitor({'GREETING': 'hope'})
    assert i.visit(msg) == 'I hope this finds you well.'

    i = ICUNodeVisitor({'GREETING': 'wish'})
    assert i.visit(msg) == 'I wish this finds you well.'

    i = ICUNodeVisitor({'GREETING': ''})
    assert i.visit(msg) == 'I  this finds you well.'
示例#6
0
def test_plural_statement_with_offset():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, offset:3 one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.''')

    i = ICUNodeVisitor({'NUM_TICKETS': 2})
    assert i.visit(msg) == '0 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': 4})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': 45})
    assert i.visit(msg) == '42 ticketerinos.'
示例#7
0
def test_process_select_statement():
    msg = ICUMessageFormat.parse(
        '''{WHO, select, male {He} female {She} other {They}}.''')

    i = ICUNodeVisitor({'WHO': 'male'})
    assert i.visit(msg) == 'He.'

    i = ICUNodeVisitor({'WHO': 'female'})
    assert i.visit(msg) == 'She.'

    i = ICUNodeVisitor({'WHO:': 'other'})
    assert i.visit(msg) == 'They.'
示例#8
0
def test_plural_dict_uses_string():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.''')

    i = ICUNodeVisitor({'NUM_TICKETS': '1'})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': '2'})
    assert i.visit(msg) == '2 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': '42'})
    assert i.visit(msg) == '42 ticketerinos.'
示例#9
0
def test_msg_with_unicode_chars():
    msg = ICUMessageFormat.parse(
        '''{SYMBOL, select, snowman {☃} sun {☉} other {☹}}''')

    i = ICUNodeVisitor({'SYMBOL': 'snowman'})
    assert i.visit(msg) == '☃'

    i = ICUNodeVisitor({'SYMBOL': 'sun'})
    assert i.visit(msg) == '☉'

    i = ICUNodeVisitor({})
    assert i.visit(msg) == '☹'
示例#10
0
def test_process_plural_statement():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.''')

    i = ICUNodeVisitor({'NUM_TICKETS': 1})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': 2})
    assert i.visit(msg) == '2 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': 42})
    assert i.visit(msg) == '42 ticketerinos.'
示例#11
0
def test_plural_dict_uses_string():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.'''
    )

    i = ICUNodeVisitor({'NUM_TICKETS': '1'})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': '2'})
    assert i.visit(msg) == '2 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': '42'})
    assert i.visit(msg) == '42 ticketerinos.'
示例#12
0
def test_plural_statement_with_offset():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, offset:3 one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.'''
    )

    i = ICUNodeVisitor({'NUM_TICKETS': 2})
    assert i.visit(msg) == '0 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': 4})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': 45})
    assert i.visit(msg) == '42 ticketerinos.'
示例#13
0
def test_process_plural_statement():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.'''
    )

    i = ICUNodeVisitor({'NUM_TICKETS': 1})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': 2})
    assert i.visit(msg) == '2 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': 42})
    assert i.visit(msg) == '42 ticketerinos.'
示例#14
0
def test_format_tree():
    ast = ICUMessageFormat.parse(
        '{WHO, select, male {He} female {She} other {They}}.')

    select = format_tree(ast, {'WHO': 'male'}, 'en')
    assert select == 'He.'

    select = format_tree(ast, {'WHO': 'female'}, 'en')
    assert select == 'She.'

    select = format_tree(ast, {'WHO': 'other'}, 'en')
    assert select == 'They.'

    select = format_tree(ast, {}, 'en')
    assert select == 'They.'

    ast = ICUMessageFormat.parse(
        '{NUM_TICKETS, plural, offset:1 one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.'
    )

    plural = format_tree(ast, {'NUM_TICKETS': 1}, 'en')
    assert plural == '0 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': '1'}, 'en')
    assert plural == '0 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': '2'}, 'en')
    assert plural == '1 ticket.'

    plural = format_tree(ast, {'NUM_TICKETS': 2.0}, 'en')
    assert plural == '1.0 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': 5}, 'en')
    assert plural == '4 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': 43}, 'en')
    assert plural == '42 ticketerinos.'
示例#15
0
def test_plural_dict_with_decimals():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, one {1 ticket} other {# tickets} =42.5 {42 ticketerinos}}.''')

    i = ICUNodeVisitor({'NUM_TICKETS': '1'})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': '1.0'})
    assert i.visit(msg) == '1.0 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': '42'})
    assert i.visit(msg) == '42 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': '42.5'})
    assert i.visit(msg) == '42 ticketerinos.'
示例#16
0
def test_format_tree():
    ast = ICUMessageFormat.parse(
        '{WHO, select, male {He} female {She} other {They}}.')

    select = format_tree(ast, {'WHO': 'male'}, 'en')
    assert select == 'He.'

    select = format_tree(ast, {'WHO': 'female'}, 'en')
    assert select == 'She.'

    select = format_tree(ast, {'WHO': 'other'}, 'en')
    assert select == 'They.'

    select = format_tree(ast, {}, 'en')
    assert select == 'They.'

    ast = ICUMessageFormat.parse(
        '{NUM_TICKETS, plural, offset:1 one {1 ticket} other {# tickets} =42 {42 ticketerinos}}.')

    plural = format_tree(ast, {'NUM_TICKETS': 1}, 'en')
    assert plural == '0 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': '1'}, 'en')
    assert plural == '0 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': '2'}, 'en')
    assert plural == '1 ticket.'

    plural = format_tree(ast, {'NUM_TICKETS': 2.0}, 'en')
    assert plural == '1.0 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': 5}, 'en')
    assert plural == '4 tickets.'

    plural = format_tree(ast, {'NUM_TICKETS': 43}, 'en')
    assert plural == '42 ticketerinos.'
示例#17
0
def get_icu_keys(msg):
    """ Get the names of the parameters that the 'msg' ICU template needs in
        order to compile.

        :param str msg: The ICU template to be parsed
        :return: A "slightly bigger superset" of the parameter names
        :rtype: set
    """

    v = IcuKeysVisitor()
    try:
        ast = ICUMessageFormat.parse(msg)
        v.visit(ast)
    except Exception:
        return set()
    return v.keys
示例#18
0
def test_plural_dict_with_decimals():
    msg = ICUMessageFormat.parse(
        '''{NUM_TICKETS, plural, one {1 ticket} other {# tickets} =42.5 {42 ticketerinos}}.'''
    )

    i = ICUNodeVisitor({'NUM_TICKETS': '1'})
    assert i.visit(msg) == '1 ticket.'

    i = ICUNodeVisitor({'NUM_TICKETS': '1.0'})
    assert i.visit(msg) == '1.0 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': '42'})
    assert i.visit(msg) == '42 tickets.'

    i = ICUNodeVisitor({'NUM_TICKETS': '42.5'})
    assert i.visit(msg) == '42 ticketerinos.'
示例#19
0
def test_empty_msg():
    msg = ICUMessageFormat.parse('{number, plural, =1 {} other {#}}')
    i = ICUNodeVisitor({'number': 1})

    assert i.visit(msg) == ''
示例#20
0
def test_use_other_statement_if_no_select_arg():
    msg = ICUMessageFormat.parse(
        '''{WHO, select, male {He} female {She} other {They}}.''')

    i = ICUNodeVisitor({})
    assert i.visit(msg) == 'They.'
示例#21
0
def test_use_other_statement_if_no_select_arg():
    msg = ICUMessageFormat.parse(
        '''{WHO, select, male {He} female {She} other {They}}.''')

    i = ICUNodeVisitor({})
    assert i.visit(msg) == 'They.'