replace static method
final inp = '{IS_LOADING ? "Loading..." : "{COUNT > 1 ? "COUNT items" : "{COUNT == 1 ? "One item" : "No items"}"}"}';
TextReplacer.replace(inp, {'COUNT': 1, 'IS_LOADING': true}); // Loading...
TextReplacer.replace(inp, {'COUNT': 0, 'IS_LOADING': false}); // No items
TextReplacer.replace(inp, {'COUNT': 1, 'IS_LOADING': false}); // One item
TextReplacer.replace(inp, {'COUNT': 2, 'IS_LOADING': false}); // 2 items
final inp1 = "There {NUMBER > 1 ? \"are NUMBER items\" : \"is an item\"} in stock";
TextReplacer.replace(inp1, {'NUMBER': 1}); // There is an item in stock
TextReplacer.replace(inp1, {'NUMBER': 2}); // There are 2 items in stock
final inp2 = "Status: {STATUS == active ? \"activated!\" : \"canceled!\"}";
TextReplacer.replace(inp2, {'STATUS': "active"}); // Status: activated!
TextReplacer.replace(inp2, {'STATUS': "inactive"}); // Status: canceled!
final inp3 = "Status: {IS_ACTIVATED ? \"activated!\" : \"inactivated!\"}";
TextReplacer.replace(inp3, {'IS_ACTIVATED': true}); // Status: activated!
TextReplacer.replace(inp3, {'IS_ACTIVATED': false}); // Status: inactivated!
final inp4 = "Last seen: {TIME: {a:now, b:3 min ago}}";
TextReplacer.replace(inp4, {"TIME": "a"}); // Last seen: now
TextReplacer.replace(inp4, {"TIME": "b"}); // Last seen: 3 min ago
Implementation
static String replace(String input, Map<String, Object?> args) {
if (!_replaceable(input)) return _v(input, args, true);
if (RegExp(_kConditional).hasMatch(input)) {
input = _con(input, args);
}
if (RegExp(_kMapped).hasMatch(input)) {
input = _map(input, args);
}
return replace(input, args);
}