2021-05-14 16:57:36 +07:00
|
|
|
class TextFormatter {
|
|
|
|
String? text;
|
|
|
|
|
|
|
|
TextFormatter(String? text) {
|
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
String? format(List<dynamic> args) {
|
|
|
|
if (text == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:24:05 +07:00
|
|
|
if (args.isEmpty) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2021-05-14 16:57:36 +07:00
|
|
|
var msg = text;
|
|
|
|
args.asMap().forEach((index, element) {
|
2021-05-28 15:24:05 +07:00
|
|
|
var _replaced = '';
|
|
|
|
if (element != null) {
|
|
|
|
_replaced = element.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = msg?.replaceAll('{$index}', _replaced);
|
2021-05-14 16:57:36 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:24:05 +07:00
|
|
|
String? decorate(Map<String, dynamic> params) {
|
2021-05-14 16:57:36 +07:00
|
|
|
if (text == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:24:05 +07:00
|
|
|
if (params.isEmpty) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2021-05-14 16:57:36 +07:00
|
|
|
var msg = text;
|
2021-05-28 15:24:05 +07:00
|
|
|
params.forEach((index, element) {
|
|
|
|
var _replaced = '';
|
|
|
|
if (element != null) {
|
|
|
|
_replaced = element.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = msg?.replaceAll('{$index}', _replaced);
|
2021-05-14 16:57:36 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|