Commit 16915948 authored by Wilko Manger's avatar Wilko Manger

Fix generating TextSpans for localized strings

parent 002c36e9
......@@ -17,6 +17,7 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:characters/characters.dart';
import '../../models/chat_member.dart';
......@@ -96,35 +97,51 @@ List<TextSpan> _toTextSpans({
if (args.length >= 2) placeholders[1],
]);
final placeholderPositions = <int, String>{};
final placeHolderIndexes = List.generate(
placeholders.length,
(index) => index,
);
for (final placeholder in placeholders) {
final split = message.split(placeholder);
final characters = message.characters.toList();
var i = 0;
for (final string in split) {
if (string != split.last) {
placeholderPositions[i] = placeholder;
}
final pieces = <TextSpan>[];
var currentPieceIndex = 0;
i++;
}
}
final placeholderPieceIndexes = <int, int>{};
final split = message.split(RegExp(r'\$\d'));
final spans =
split.where((s) => s.isNotEmpty).map((s) => TextSpan(text: s)).toList();
for (var i = 0; i < characters.length; i++) {
final currentChar = characters[i];
final nextChar =
i + 1 < characters.length ? characters.toList()[i + 1] : null;
for (final entries in placeholderPositions.entries) {
final position = entries.key;
final placeholder = entries.value;
final placeholderIndex = nextChar != null ? int.tryParse(nextChar) : null;
if (placeholderIndex != null &&
currentChar == r'$' &&
placeHolderIndexes.contains(placeholderIndex)) {
placeholderPieceIndexes[currentPieceIndex] = placeholderIndex;
final arg = args[placeholders.indexOf(placeholder)];
pieces.add(
TextSpan(
text: args[placeholderIndex],
style: style,
),
);
spans.insert(position, TextSpan(text: arg, style: style));
// Skip the next char
i++;
currentPieceIndex++;
} else {
if (pieces.length == currentPieceIndex) {
pieces.add(TextSpan(text: currentChar));
} else {
pieces[currentPieceIndex] = TextSpan(
text: pieces[currentPieceIndex].text + currentChar,
);
}
}
}
return spans;
return pieces;
}
extension LocalizedTextSpansString on String Function(String) {
......
......@@ -71,6 +71,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0-rc.1"
characters:
dependency: "direct main"
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.0"
charcode:
dependency: transitive
description:
......
......@@ -52,6 +52,7 @@ dependencies:
shimmer: ^1.1.0
flutter_svg: ^0.17.3+1
animations: ^1.0.0+5
characters: ^0.5.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment