2021-06-17 15:25:40 +07:00
|
|
|
import 'package:cubetiq/i18n_translator.dart';
|
|
|
|
|
2021-06-01 23:03:54 +07:00
|
|
|
/// Text Formatter
|
|
|
|
///
|
|
|
|
/// @author sombochea
|
|
|
|
/// @since 1.0.0
|
|
|
|
class TextFormatter {
|
|
|
|
String? text;
|
|
|
|
|
|
|
|
TextFormatter(String? text) {
|
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:25:40 +07:00
|
|
|
TextFormatter translate({bool translate = true}) {
|
|
|
|
if (translate && text != null && text?.isNotEmpty == true) {
|
|
|
|
text = TranslatorFactory.translate(text!);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String? format({List<dynamic>? args}) {
|
2021-06-01 23:03:54 +07:00
|
|
|
if (text == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:25:40 +07:00
|
|
|
if (args == null || args.isEmpty == true) {
|
2021-06-01 23:03:54 +07:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = text;
|
|
|
|
args.asMap().forEach((index, element) {
|
|
|
|
var _replaced = '';
|
|
|
|
if (element != null) {
|
|
|
|
_replaced = element.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = msg?.replaceAll('{$index}', _replaced);
|
|
|
|
});
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:25:40 +07:00
|
|
|
String? decorate({Map<String, dynamic>? params}) {
|
2021-06-01 23:03:54 +07:00
|
|
|
if (text == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:25:40 +07:00
|
|
|
if (params == null || params.isEmpty == true) {
|
2021-06-01 23:03:54 +07:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = text;
|
|
|
|
params.forEach((index, element) {
|
|
|
|
var _replaced = '';
|
|
|
|
if (element != null) {
|
|
|
|
_replaced = element.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = msg?.replaceAll('{$index}', _replaced);
|
|
|
|
});
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|