Refactor localizations

This commit is contained in:
Christian Pauly 2020-05-06 12:21:54 +02:00
parent 3615b7c22b
commit 48e6d42873
13 changed files with 16 additions and 321 deletions

View File

@ -8,8 +8,6 @@ import 'package:pedantic/pedantic.dart';
import '../../i18n/i18n.dart'; import '../../i18n/i18n.dart';
import '../../utils/app_route.dart'; import '../../utils/app_route.dart';
import '../../utils/date_time_extension.dart'; import '../../utils/date_time_extension.dart';
import '../../utils/event_extension.dart';
import '../../utils/room_extension.dart';
import '../../views/chat.dart'; import '../../views/chat.dart';
import '../theme_switcher.dart'; import '../theme_switcher.dart';
import '../avatar.dart'; import '../avatar.dart';

View File

@ -2,7 +2,6 @@ import 'package:bubble/bubble.dart';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluffychat/utils/event_extension.dart';
class StateMessage extends StatelessWidget { class StateMessage extends StatelessWidget {
final Event event; final Event event;

View File

@ -2,7 +2,6 @@ import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/audio_player.dart'; import 'package:fluffychat/components/audio_player.dart';
import 'package:fluffychat/components/image_bubble.dart'; import 'package:fluffychat/components/image_bubble.dart';
import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/i18n/i18n.dart';
import 'package:fluffychat/utils/event_extension.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:link_text/link_text.dart'; import 'package:link_text/link_text.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';

View File

@ -1,6 +1,5 @@
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/i18n/i18n.dart';
import 'package:fluffychat/utils/event_extension.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class ReplyContent extends StatelessWidget { class ReplyContent extends StatelessWidget {

View File

@ -1,3 +1,4 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'messages_all.dart'; import 'messages_all.dart';
@ -21,7 +22,7 @@ class AppLocalizationsDelegate extends LocalizationsDelegate<I18n> {
} }
} }
class I18n { class I18n extends MatrixLocalizations {
I18n(this.localeName); I18n(this.localeName);
static Future<I18n> load(Locale locale) { static Future<I18n> load(Locale locale) {
@ -229,8 +230,11 @@ class I18n {
String get copy => Intl.message("Copy"); String get copy => Intl.message("Copy");
String get couldNotDecryptMessage => String couldNotDecryptMessage(String error) => Intl.message(
Intl.message("Could not decrypt message"); "Could not decrypt message: $error",
name: "couldNotDecryptMessage",
args: [error],
);
String get couldNotSetAvatar => Intl.message("Could not set avatar"); String get couldNotSetAvatar => Intl.message("Could not set avatar");

View File

@ -1,240 +1,7 @@
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'room_state_enums_extensions.dart';
extension LocalizedBody on Event { extension LocalizedBody on Event {
static Set<MessageTypes> textOnlyMessageTypes = {
MessageTypes.Text,
MessageTypes.Reply,
MessageTypes.Notice,
MessageTypes.Emote,
MessageTypes.None,
};
String getLocalizedBody(I18n i18n,
{bool withSenderNamePrefix = false, bool hideReply = false}) {
if (this.redacted) {
return i18n.removedBy(redactedBecause.sender.calcDisplayname());
}
String localizedBody = body;
final String senderName = this.sender.calcDisplayname();
switch (this.type) {
case EventTypes.Sticker:
localizedBody = i18n.sentASticker(senderName);
break;
case EventTypes.Redaction:
localizedBody = i18n.redactedAnEvent(senderName);
break;
case EventTypes.RoomAliases:
localizedBody = i18n.changedTheRoomAliases(senderName);
break;
case EventTypes.RoomCanonicalAlias:
localizedBody = i18n.changedTheRoomInvitationLink(senderName);
break;
case EventTypes.RoomCreate:
localizedBody = i18n.createdTheChat(senderName);
break;
case EventTypes.RoomJoinRules:
JoinRules joinRules = JoinRules.values.firstWhere(
(r) =>
r.toString().replaceAll("JoinRules.", "") ==
content["join_rule"],
orElse: () => null);
if (joinRules == null) {
localizedBody = i18n.changedTheJoinRules(senderName);
} else {
localizedBody = i18n.changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(i18n));
}
break;
case EventTypes.RoomMember:
String text = "Failed to parse member event";
final String targetName = this.stateKeyUser.calcDisplayname();
// Has the membership changed?
final String newMembership = this.content["membership"] ?? "";
final String oldMembership =
this.unsigned["prev_content"] is Map<String, dynamic>
? this.unsigned["prev_content"]["membership"] ?? ""
: "";
if (newMembership != oldMembership) {
if (oldMembership == "invite" && newMembership == "join") {
text = i18n.acceptedTheInvitation(targetName);
} else if (oldMembership == "invite" && newMembership == "leave") {
if (this.stateKey == this.senderId) {
text = i18n.rejectedTheInvitation(targetName);
} else {
text = i18n.hasWithdrawnTheInvitationFor(senderName, targetName);
}
} else if (oldMembership == "leave" && newMembership == "join") {
text = i18n.joinedTheChat(targetName);
} else if (oldMembership == "join" && newMembership == "ban") {
text = i18n.kickedAndBanned(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey != this.senderId) {
text = i18n.kicked(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey == this.senderId) {
text = i18n.userLeftTheChat(targetName);
} else if (oldMembership == "invite" && newMembership == "ban") {
text = i18n.bannedUser(senderName, targetName);
} else if (oldMembership == "leave" && newMembership == "ban") {
text = i18n.bannedUser(senderName, targetName);
} else if (oldMembership == "ban" && newMembership == "leave") {
text = i18n.unbannedUser(senderName, targetName);
} else if (newMembership == "invite") {
text = i18n.invitedUser(senderName, targetName);
} else if (newMembership == "join") {
text = i18n.joinedTheChat(targetName);
}
} else if (newMembership == "join") {
final String newAvatar = this.content["avatar_url"] ?? "";
final String oldAvatar =
this.unsigned["prev_content"] is Map<String, dynamic>
? this.unsigned["prev_content"]["avatar_url"] ?? ""
: "";
final String newDisplayname = this.content["displayname"] ?? "";
final String oldDisplayname =
this.unsigned["prev_content"] is Map<String, dynamic>
? this.unsigned["prev_content"]["displayname"] ?? ""
: "";
// Has the user avatar changed?
if (newAvatar != oldAvatar) {
text = i18n.changedTheProfileAvatar(targetName);
}
// Has the user avatar changed?
else if (newDisplayname != oldDisplayname) {
text = i18n.changedTheDisplaynameTo(targetName, newDisplayname);
}
}
localizedBody = text;
break;
case EventTypes.RoomPowerLevels:
localizedBody = i18n.changedTheChatPermissions(senderName);
break;
case EventTypes.RoomName:
localizedBody = i18n.changedTheChatNameTo(senderName, content["name"]);
break;
case EventTypes.RoomTopic:
localizedBody =
i18n.changedTheChatDescriptionTo(senderName, content["topic"]);
break;
case EventTypes.RoomAvatar:
localizedBody = i18n.changedTheChatAvatar(senderName);
break;
case EventTypes.GuestAccess:
GuestAccess guestAccess = GuestAccess.values.firstWhere(
(r) =>
r.toString().replaceAll("GuestAccess.", "") ==
content["guest_access"],
orElse: () => null);
if (guestAccess == null) {
localizedBody = i18n.changedTheGuestAccessRules(senderName);
} else {
localizedBody = i18n.changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(i18n));
}
break;
case EventTypes.HistoryVisibility:
HistoryVisibility historyVisibility = HistoryVisibility.values
.firstWhere(
(r) =>
r.toString().replaceAll("HistoryVisibility.", "") ==
content["history_visibility"],
orElse: () => null);
if (historyVisibility == null) {
localizedBody = i18n.changedTheHistoryVisibility(senderName);
} else {
localizedBody = i18n.changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(i18n));
}
break;
case EventTypes.Encryption:
localizedBody = i18n.activatedEndToEndEncryption(senderName);
if (!room.client.encryptionEnabled) {
localizedBody += ". " + i18n.needPantalaimonWarning;
}
break;
case EventTypes.Encrypted:
case EventTypes.Message:
switch (this.messageType) {
case MessageTypes.Image:
localizedBody = i18n.sentAPicture(senderName);
break;
case MessageTypes.File:
localizedBody = i18n.sentAFile(senderName);
break;
case MessageTypes.Audio:
localizedBody = i18n.sentAnAudio(senderName);
break;
case MessageTypes.Video:
localizedBody = i18n.sentAVideo(senderName);
break;
case MessageTypes.Location:
localizedBody = i18n.sharedTheLocation(senderName);
break;
case MessageTypes.Sticker:
localizedBody = i18n.sentASticker(senderName);
break;
case MessageTypes.Emote:
localizedBody = "* $body";
break;
case MessageTypes.BadEncrypted:
String errorText;
switch (body) {
case DecryptError.CHANNEL_CORRUPTED:
errorText = i18n.channelCorruptedDecryptError + ".";
break;
case DecryptError.NOT_ENABLED:
errorText = i18n.encryptionNotEnabled + ".";
break;
case DecryptError.UNKNOWN_ALGORITHM:
errorText = i18n.unknownEncryptionAlgorithm + ".";
break;
case DecryptError.UNKNOWN_SESSION:
errorText = i18n.noPermission + ".";
break;
default:
errorText = body;
break;
}
localizedBody =
"🔒 " + i18n.couldNotDecryptMessage + ": " + errorText;
break;
case MessageTypes.Text:
case MessageTypes.Notice:
case MessageTypes.None:
case MessageTypes.Reply:
localizedBody = body;
break;
}
break;
default:
localizedBody = i18n.unknownEvent(this.typeKey);
}
// Hide reply fallback
if (hideReply) {
localizedBody = localizedBody.replaceFirst(
RegExp(r'^>( \*)? <[^>]+>[^\n\r]+\r?\n(> [^\n]*\r?\n)*\r?\n'), "");
}
// Add the sender name prefix
if (withSenderNamePrefix &&
this.type == EventTypes.Message &&
textOnlyMessageTypes.contains(this.messageType)) {
final String senderNameOrYou =
this.senderId == room.client.userID ? i18n.you : senderName;
localizedBody = "$senderNameOrYou: $localizedBody";
}
return localizedBody;
}
IconData get statusIcon { IconData get statusIcon {
switch (this.status) { switch (this.status) {
case -1: case -1:

View File

@ -12,8 +12,6 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart'; import 'package:flutter_styled_toast/flutter_styled_toast.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import '../utils/event_extension.dart';
import '../utils/room_extension.dart';
import 'famedlysdk_store.dart'; import 'famedlysdk_store.dart';
abstract class FirebaseController { abstract class FirebaseController {

View File

@ -1,20 +0,0 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
extension LocalizedRoomDisplayname on Room {
String getLocalizedDisplayname(I18n i18n) {
if ((this.name?.isEmpty ?? true) &&
(this.canonicalAlias?.isEmpty ?? true) &&
!this.isDirectChat &&
(this.mHeroes != null && this.mHeroes.isNotEmpty)) {
return i18n.groupWith(this.displayname);
}
if ((this.name?.isEmpty ?? true) &&
(this.canonicalAlias?.isEmpty ?? true) &&
!this.isDirectChat &&
(this.mHeroes?.isEmpty ?? true)) {
return i18n.emptyChat;
}
return this.displayname;
}
}

View File

@ -1,45 +0,0 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
extension HistoryVisibilityDisplayString on HistoryVisibility {
String getLocalizedString(I18n i18n) {
switch (this) {
case HistoryVisibility.invited:
return i18n.fromTheInvitation;
case HistoryVisibility.joined:
return i18n.fromJoining;
case HistoryVisibility.shared:
return i18n.visibleForAllParticipants;
case HistoryVisibility.world_readable:
return i18n.visibleForEveryone;
default:
return this.toString().replaceAll("HistoryVisibility.", "");
}
}
}
extension GuestAccessDisplayString on GuestAccess {
String getLocalizedString(I18n i18n) {
switch (this) {
case GuestAccess.can_join:
return i18n.guestsCanJoin;
case GuestAccess.forbidden:
return i18n.guestsAreForbidden;
default:
return this.toString().replaceAll("GuestAccess.", "");
}
}
}
extension JoinRulesDisplayString on JoinRules {
String getLocalizedString(I18n i18n) {
switch (this) {
case JoinRules.public:
return i18n.anyoneCanJoin;
case JoinRules.invite:
return i18n.invitedUsersOnly;
default:
return this.toString().replaceAll("JoinRules.", "");
}
}
}

View File

@ -12,8 +12,6 @@ import 'package:fluffychat/components/list_items/message.dart';
import 'package:fluffychat/components/matrix.dart'; import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/components/reply_content.dart'; import 'package:fluffychat/components/reply_content.dart';
import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/i18n/i18n.dart';
import 'package:fluffychat/utils/event_extension.dart';
import 'package:fluffychat/utils/room_extension.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';

View File

@ -9,8 +9,6 @@ import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
import 'package:fluffychat/components/list_items/participant_list_item.dart'; import 'package:fluffychat/components/list_items/participant_list_item.dart';
import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/i18n/i18n.dart';
import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/utils/room_extension.dart';
import 'package:fluffychat/utils/room_state_enums_extensions.dart';
import 'package:fluffychat/views/chat_list.dart'; import 'package:fluffychat/views/chat_list.dart';
import 'package:fluffychat/views/invitation_selection.dart'; import 'package:fluffychat/views/invitation_selection.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';

View File

@ -117,8 +117,8 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
path: "." path: "."
ref: "1265ebf7da56440ead152bbf44502b941436dc1b" ref: "73ed5ed093a1275f93adc946a605d46530cbdd39"
resolved-ref: "1265ebf7da56440ead152bbf44502b941436dc1b" resolved-ref: "73ed5ed093a1275f93adc946a605d46530cbdd39"
url: "https://gitlab.com/famedly/famedlysdk.git" url: "https://gitlab.com/famedly/famedlysdk.git"
source: git source: git
version: "0.0.1" version: "0.0.1"
@ -253,7 +253,7 @@ packages:
name: http name: http
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.0+4" version: "0.12.1"
http_multi_server: http_multi_server:
dependency: transitive dependency: transitive
description: description:
@ -342,11 +342,11 @@ packages:
dependency: transitive dependency: transitive
description: description:
path: "." path: "."
ref: HEAD ref: "1.x.y"
resolved-ref: b043fcc29031979dc65e5b08e10ebb9b8d2fae30 resolved-ref: "32edeff765369a7a77a0822f4b19302ca24a017b"
url: "https://gitlab.com/famedly/libraries/matrix_file_e2ee.git" url: "https://gitlab.com/famedly/libraries/matrix_file_e2ee.git"
source: git source: git
version: "1.0.2" version: "1.0.3"
meta: meta:
dependency: transitive dependency: transitive
description: description:
@ -367,7 +367,7 @@ packages:
name: mime_type name: mime_type
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.7" version: "0.3.0"
multi_server_socket: multi_server_socket:
dependency: transitive dependency: transitive
description: description:

View File

@ -27,7 +27,7 @@ dependencies:
famedlysdk: famedlysdk:
git: git:
url: https://gitlab.com/famedly/famedlysdk.git url: https://gitlab.com/famedly/famedlysdk.git
ref: 1265ebf7da56440ead152bbf44502b941436dc1b ref: 73ed5ed093a1275f93adc946a605d46530cbdd39
localstorage: ^3.0.1+4 localstorage: ^3.0.1+4
bubble: ^1.1.9+1 bubble: ^1.1.9+1
@ -52,7 +52,7 @@ dependencies:
photo_view: ^0.9.2 photo_view: ^0.9.2
flutter_sound: ^2.1.1 flutter_sound: ^2.1.1
open_file: ^3.0.1 open_file: ^3.0.1
mime_type: ^0.2.7 mime_type: ^0.3.0
flutter_styled_toast: ^1.2.1 flutter_styled_toast: ^1.2.1
intl: ^0.16.0 intl: ^0.16.0