Task: Add i18n translator and factory for translatable for text formatter and default factory

This commit is contained in:
2021-06-17 15:25:40 +07:00
parent 84c0e44b87
commit 5915aafb95
12 changed files with 154 additions and 16 deletions

4
lib/i18n_translator.dart Normal file
View File

@@ -0,0 +1,4 @@
library i18n_translator;
export 'src/i18n/translator_provider.dart';
export 'src/i18n/translator_factory.dart';

View File

@@ -0,0 +1,34 @@
import 'translator_provider.dart';
/// Translate Factory
///
/// @author sombochea
/// @since 1.0.0
class TranslatorFactory {
static TranslatorProvider? _provider;
static void setProvider(TranslatorProvider provider) {
_provider = provider;
}
static bool hasProvider() => _provider != null;
static bool hasKey(String key) {
if (hasProvider()) {
return _provider!.hasKey(key);
}
return false;
}
static String translate(
String key, {
Map<String, dynamic>? params,
String? fallback,
}) {
if (hasProvider()) {
return _provider!.translate(key, params: params, fallback: fallback);
}
return fallback ?? key;
}
}

View File

@@ -0,0 +1,9 @@
/// Translate Provider
///
/// @author sombochea
/// @since 1.0.0
abstract class TranslatorProvider {
bool hasKey(String key);
String translate(String key,
{Map<String, dynamic>? params, String? fallback});
}

View File

@@ -7,10 +7,11 @@ extension StringExtensionOnNonull on String {
}
extension StringExtensionOnNullable on String? {
String? textFormat(List<dynamic> args) => StringUtils.textFormat(this, args);
String? textFormat({List<dynamic>? args, bool translate = false}) =>
StringUtils.textFormat(this, args: args, translate: translate);
String? decorator(Map<String, dynamic> params) =>
StringUtils.decorator(this, params);
String? decorator({Map<String, dynamic>? params, bool translate = false}) =>
StringUtils.decorator(this, params: params, translate: translate);
bool get isBlank {
if (this == null) return true;

View File

@@ -13,13 +13,19 @@ class StringUtils {
return n.toStringAsFixed(n.truncateToDouble() == n ? precision : precision);
}
/// Text formatter with custom args
static TextFormatter textFormatter(String? text, {bool translate = false}) =>
TextFormatter(text).translate(translate: translate);
/// Text format with custom args
static String? textFormat(String? text, List<dynamic> args) =>
TextFormatter(text).format(args);
static String? textFormat(String? text,
{List<dynamic>? args, bool translate = false}) =>
textFormatter(text, translate: translate).format(args: args);
/// Text decorator with custom key/value params
static String? decorator(String? text, Map<String, dynamic> params) =>
TextFormatter(text).decorate(params);
static String? decorator(String? text,
{Map<String, dynamic>? params, bool translate = false}) =>
textFormatter(text, translate: translate).decorate(params: params);
static String? asLowerCaseThenTrim(String? text) =>
text?.toLowerCase().trim();

View File

@@ -1,3 +1,5 @@
import 'package:cubetiq/i18n_translator.dart';
/// Text Formatter
///
/// @author sombochea
@@ -9,12 +11,20 @@ class TextFormatter {
this.text = text;
}
String? format(List<dynamic> args) {
TextFormatter translate({bool translate = true}) {
if (translate && text != null && text?.isNotEmpty == true) {
text = TranslatorFactory.translate(text!);
}
return this;
}
String? format({List<dynamic>? args}) {
if (text == null) {
return null;
}
if (args.isEmpty) {
if (args == null || args.isEmpty == true) {
return text;
}
@@ -31,12 +41,12 @@ class TextFormatter {
return msg;
}
String? decorate(Map<String, dynamic> params) {
String? decorate({Map<String, dynamic>? params}) {
if (text == null) {
return null;
}
if (params.isEmpty) {
if (params == null || params.isEmpty == true) {
return text;
}

View File

@@ -25,7 +25,7 @@ abstract class XLogProvider {
if (args == null || args.isEmpty) {
content = data;
} else {
content = StringUtils.textFormat(data, args) ?? 'null';
content = StringUtils.textFormat(data, args: args) ?? 'null';
}
var text = '[$type] ${nowToString()}: $prefix => $content'.trim();