feat: Implement official emoji translations for emoji verification

This commit is contained in:
Sorunome 2021-08-14 16:36:51 +02:00
parent 7b31c00d53
commit 95bcb467c7
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
3 changed files with 2217 additions and 3 deletions

View File

@ -177,3 +177,5 @@ location /_matrix/push/v1/notify {
* Also thanks to all translators and testers! With your help, fluffychat is now available in more than 12 languages.
* <a href="https://github.com/googlefonts/noto-emoji/">Noto Emoji Font</a> for the awesome emojis.
* The Matrix Foundation for making and maintaining the [emoji translations](https://github.com/matrix-org/matrix-doc/blob/master/data-definitions/sas-emoji.json) used for emoji verification, licensed Apache 2.0

2178
assets/sas-emoji.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,13 @@
import 'dart:ui';
import 'dart:convert';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../widgets/adaptive_flat_button.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
@ -37,6 +41,7 @@ class KeyVerificationDialog extends StatefulWidget {
class _KeyVerificationPageState extends State<KeyVerificationDialog> {
void Function() originalOnUpdate;
List<dynamic> sasEmoji;
@override
void initState() {
@ -51,6 +56,10 @@ class _KeyVerificationPageState extends State<KeyVerificationDialog> {
profile = p;
setState(() => null);
});
rootBundle.loadString('assets/sas-emoji.json').then((e) {
sasEmoji = json.decode(e);
setState(() => null);
});
super.initState();
}
@ -208,7 +217,7 @@ class _KeyVerificationPageState extends State<KeyVerificationDialog> {
compareText = L10n.of(context).compareEmojiMatch;
compareWidget = TextSpan(
children: widget.request.sasEmojis
.map((e) => WidgetSpan(child: _Emoji(e)))
.map((e) => WidgetSpan(child: _Emoji(e, sasEmoji)))
.toList(),
);
} else {
@ -371,8 +380,33 @@ class _KeyVerificationPageState extends State<KeyVerificationDialog> {
class _Emoji extends StatelessWidget {
final KeyVerificationEmoji emoji;
final List<dynamic> sasEmoji;
_Emoji(this.emoji);
_Emoji(this.emoji, this.sasEmoji);
String getLocalizedName() {
if (sasEmoji == null) {
// asset is still being loaded
return emoji.name;
}
final translations = Map<String, String>.from(
sasEmoji[emoji.number]['translated_descriptions']);
translations['en'] = emoji.name;
for (final locale in window.locales) {
final wantLocaleParts = locale.toString().split('_');
final wantLanguage = wantLocaleParts.removeAt(0);
for (final haveLocale in translations.keys) {
final haveLocaleParts = haveLocale.split('_');
final haveLanguage = haveLocaleParts.removeAt(0);
if (haveLanguage == wantLanguage &&
(Set.from(haveLocaleParts)..removeAll(wantLocaleParts)).isEmpty &&
(translations[haveLocale]?.isNotEmpty ?? false)) {
return translations[haveLocale];
}
}
}
return emoji.name;
}
@override
Widget build(BuildContext context) {
@ -380,7 +414,7 @@ class _Emoji extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(emoji.emoji, style: TextStyle(fontSize: 50)),
Text(emoji.name),
Text(getLocalizedName()),
Container(height: 10, width: 5),
],
);