Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions lib/src/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Localization {
late Locale _locale;

final RegExp _replaceArgRegex = RegExp('{}');
final RegExp _linkKeyMatcher =
RegExp(r'(?:@(?:\.[a-z]+)?:(?:[\w\-_|.]+|\([\w\-_|.]+\)))');
final RegExp _linkKeyMatcher = RegExp(r'(?:@(?:\.[a-z]+)?:(?:[\w\-_|.]+|\([\w\-_|.]+\)))');
final RegExp _linkKeyPrefixMatcher = RegExp(r'^@(?:\.([a-z]+))?:');
final RegExp _bracketsMatcher = RegExp('[()]');
final _modifiers = <String, String Function(String?)>{
Expand All @@ -28,8 +27,7 @@ class Localization {

static Localization get instance => _instance ?? (_instance = Localization());

static Localization? of(BuildContext context) =>
Localizations.of<Localization>(context, Localization);
static Localization? of(BuildContext context) => Localizations.of<Localization>(context, Localization);

static bool load(
Locale locale, {
Expand All @@ -41,8 +39,7 @@ class Localization {
instance._locale = locale;
instance._translations = translations;
instance._fallbackTranslations = fallbackTranslations;
instance._useFallbackTranslationsForEmptyResources =
useFallbackTranslationsForEmptyResources;
instance._useFallbackTranslationsForEmptyResources = useFallbackTranslationsForEmptyResources;
instance._ignorePluralRules = ignorePluralRules;
return translations == null ? false : true;
}
Expand Down Expand Up @@ -80,8 +77,7 @@ class Localization {
final formatterName = linkPrefixMatches.first[1];

// Remove the leading @:, @.case: and the brackets
final linkPlaceholder =
link.replaceAll(linkPrefix, '').replaceAll(_bracketsMatcher, '');
final linkPlaceholder = link.replaceAll(linkPrefix, '').replaceAll(_bracketsMatcher, '');

var translated = _resolve(linkPlaceholder);

Expand All @@ -90,14 +86,17 @@ class Localization {
translated = _modifiers[formatterName]!(translated);
} else {
if (logging) {
EasyLocalization.logger.warning(
'Undefined modifier $formatterName, available modifiers: ${_modifiers.keys.toString()}');
EasyLocalization.logger
.warning('Undefined modifier $formatterName, available modifiers: ${_modifiers.keys.toString()}');
}
}
}

result =
translated.isEmpty ? result : result.replaceAll(link, translated);
result = translated.isEmpty ? result : result.replaceAll(link, translated);
}

if (_linkKeyMatcher.hasMatch(result)) {
result = _replaceLinks(result, logging: logging);
}

return result;
Expand All @@ -113,8 +112,7 @@ class Localization {

String _replaceNamedArgs(String res, Map<String, String>? args) {
if (args == null || args.isEmpty) return res;
args.forEach((String key, String value) =>
res = res.replaceAll(RegExp('{$key}'), value));
args.forEach((String key, String value) => res = res.replaceAll(RegExp('{$key}'), value));
return res;
}

Expand Down Expand Up @@ -202,20 +200,17 @@ class Localization {

String _resolve(String key, {bool logging = true, bool fallback = true}) {
var resource = _translations?.get(key);
if (resource == null ||
(_useFallbackTranslationsForEmptyResources && resource.isEmpty)) {
if (resource == null || (_useFallbackTranslationsForEmptyResources && resource.isEmpty)) {
if (logging) {
EasyLocalization.logger.warning('Localization key [$key] not found');
}
if (_fallbackTranslations == null || !fallback) {
return key;
} else {
resource = _fallbackTranslations?.get(key);
if (resource == null ||
(_useFallbackTranslationsForEmptyResources && resource.isEmpty)) {
if (resource == null || (_useFallbackTranslationsForEmptyResources && resource.isEmpty)) {
if (logging) {
EasyLocalization.logger
.warning('Fallback localization key [$key] not found');
EasyLocalization.logger.warning('Fallback localization key [$key] not found');
}
return key;
}
Expand Down