-
-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for rich text #724
Open
robbysoerya
wants to merge
2
commits into
aissat:develop
Choose a base branch
from
robbysoerya:feature/add_support_for_rich_text
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ class Localization { | |
late Locale _locale; | ||
|
||
final RegExp _replaceArgRegex = RegExp('{}'); | ||
final RegExp _namedArgMatcher = RegExp('({([a-zA-Z0-9_]+)})'); | ||
final RegExp _linkKeyMatcher = | ||
RegExp(r'(?:@(?:\.[a-z]+)?:(?:[\w\-_|.]+|\([\w\-_|.]+\)))'); | ||
final RegExp _linkKeyPrefixMatcher = RegExp(r'^@(?:\.([a-z]+))?:'); | ||
|
@@ -68,6 +69,22 @@ class Localization { | |
return _replaceArgs(res, args); | ||
} | ||
|
||
TextSpan trSpan( | ||
String key, { | ||
Map<String, TextSpan>? namedArgs, | ||
}) { | ||
late String res; | ||
late TextSpan span; | ||
|
||
res = _resolve(key); | ||
|
||
res = _replaceLinks(res); | ||
|
||
span = _replaceSpanNamedArgs(res, namedArgs); | ||
|
||
return span; | ||
} | ||
|
||
String _replaceLinks(String res, {bool logging = true}) { | ||
// TODO: add recursion detection and a resolve stack. | ||
final matches = _linkKeyMatcher.allMatches(res); | ||
|
@@ -103,6 +120,24 @@ class Localization { | |
return result; | ||
} | ||
|
||
List<String> _splitTextWithNamedArg(String text) { | ||
final matches = _namedArgMatcher.allMatches(text); | ||
var lastIndex = 0; | ||
final result = <String>[]; | ||
|
||
for (final match in matches) { | ||
result | ||
..add(text.substring(lastIndex, match.start)) | ||
..add(match.group(0) ?? ''); | ||
lastIndex = match.end; | ||
} | ||
if (lastIndex < text.length) { | ||
result.add(text.substring(lastIndex)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
String _replaceArgs(String res, List<String>? args) { | ||
if (args == null || args.isEmpty) return res; | ||
for (var str in args) { | ||
|
@@ -118,6 +153,15 @@ class Localization { | |
return res; | ||
} | ||
|
||
TextSpan _replaceSpanNamedArgs(String res, Map<String, TextSpan>? args) { | ||
if (args == null || args.isEmpty) return TextSpan(text: res); | ||
final spans = _splitTextWithNamedArg(res).map((part) { | ||
final key = part.replaceAll(RegExp(r'^\{|\}$'), ''); | ||
return args[key] ?? TextSpan(text: part); | ||
}).toList(); | ||
return TextSpan(children: spans); | ||
} | ||
Comment on lines
+156
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add validation and optimize performance. The method could benefit from validation and performance improvements:
Consider these improvements: TextSpan _replaceSpanNamedArgs(String res, Map<String, TextSpan>? args) {
if (args == null || args.isEmpty) return TextSpan(text: res);
+
+ // Quick check if text contains any placeholders
+ if (!res.contains('{')) return TextSpan(text: res);
+
+ // Extract all required argument names
+ final required = _namedArgMatcher
+ .allMatches(res)
+ .map((m) => m.group(2))
+ .where((name) => name != null)
+ .toSet();
+
+ // Validate all required arguments are provided
+ final missing = required.where((name) => !args.containsKey(name));
+ if (missing.isNotEmpty) {
+ throw ArgumentError('Missing named arguments: ${missing.join(', ')}');
+ }
+
final spans = _splitTextWithNamedArg(res).map((part) {
final key = part.replaceAll(RegExp(r'^\{|\}$'), '');
return args[key] ?? TextSpan(text: part);
}).toList();
return TextSpan(children: spans);
}
|
||
|
||
static PluralRule? _pluralRule(String? locale, num howMany) { | ||
if (instance._ignorePluralRules) { | ||
return () => _pluralCaseFallback(howMany); | ||
|
@@ -150,7 +194,8 @@ class Localization { | |
late String res; | ||
|
||
final pluralRule = _pluralRule(_locale.languageCode, value); | ||
final pluralCase = pluralRule != null ? pluralRule() : _pluralCaseFallback(value); | ||
final pluralCase = | ||
pluralRule != null ? pluralRule() : _pluralCaseFallback(value); | ||
|
||
switch (pluralCase) { | ||
case PluralCase.ZERO: | ||
|
@@ -193,7 +238,8 @@ class Localization { | |
if (subKey == 'other') return _resolve('$key.other'); | ||
|
||
final tag = '$key.$subKey'; | ||
var resource = _resolve(tag, logging: false, fallback: _fallbackTranslations != null); | ||
var resource = | ||
_resolve(tag, logging: false, fallback: _fallbackTranslations != null); | ||
if (resource == tag) { | ||
resource = _resolve('$key.other'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add documentation and consider feature parity with
tr
method.The new
trSpan
method lacks documentation and doesn't support all features available in thetr
method. Consider:tr
methodAdd documentation and consider implementing feature parity: