Add localizations

This commit is contained in:
Christian Pauly 2020-01-20 13:46:39 +01:00
commit a776ac1513
28 changed files with 3406 additions and 249 deletions

View file

@ -1,15 +1,8 @@
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
/// Provides extra functionality for formatting the time.
extension DateTimeExtension on DateTime {
@Deprecated('Use [millisecondsSinceEpoch] instead.')
num toTimeStamp() => this.millisecondsSinceEpoch;
@deprecated
String toTimeString() => localizedTimeOfDay(null);
@deprecated
String toEventTimeString() => localizedTime(null);
operator <(DateTime other) {
return this.millisecondsSinceEpoch < other.millisecondsSinceEpoch;
}
@ -41,7 +34,8 @@ extension DateTimeExtension on DateTime {
/// Returns a simple time String.
/// TODO: Add localization
String localizedTimeOfDay(BuildContext context) {
return "${_z(this.hour % 12)}:${_z(this.minute)} ${this.hour > 11 ? 'pm' : 'am'}";
return I18n.of(context).timeOfDay(_z(this.hour % 12), _z(this.hour),
_z(this.minute), this.hour > 11 ? 'pm' : 'am');
}
/// Returns [localizedTimeOfDay()] if the ChatTime is today, the name of the week
@ -63,24 +57,29 @@ extension DateTimeExtension on DateTime {
} else if (sameWeek) {
switch (this.weekday) {
case 1:
return "Monday";
return I18n.of(context).monday;
case 2:
return "Tuesday";
return I18n.of(context).tuesday;
case 3:
return "Wednesday";
return I18n.of(context).wednesday;
case 4:
return "Thursday";
return I18n.of(context).thursday;
case 5:
return "Friday";
return I18n.of(context).friday;
case 6:
return "Saturday";
return I18n.of(context).saturday;
case 7:
return "Sunday";
return I18n.of(context).sunday;
}
} else if (sameYear) {
return "${this.month.toString().padLeft(2, '0')}-${this.day.toString().padLeft(2, '0')}";
return I18n.of(context).dateWithoutYear(
this.month.toString().padLeft(2, '0'),
this.day.toString().padLeft(2, '0'));
}
return "${this.year.toString()}-${this.month.toString().padLeft(2, '0')}-${this.day.toString().padLeft(2, '0')}";
return I18n.of(context).dateWithYear(
this.year.toString(),
this.month.toString().padLeft(2, '0'),
this.day.toString().padLeft(2, '0'));
}
/// If the DateTime is today, this returns [localizedTimeOfDay()], if not it also
@ -94,7 +93,8 @@ extension DateTimeExtension on DateTime {
bool sameDay = sameYear && now.month == this.month && now.day == this.day;
if (sameDay) return localizedTimeOfDay(context);
return "${localizedTimeShort(context)}, ${localizedTimeOfDay(context)}";
return I18n.of(context).dateAndTimeOfDay(
localizedTimeShort(context), localizedTimeOfDay(context));
}
static String _z(int i) => i < 10 ? "0${i.toString()}" : i.toString();

View file

@ -1,4 +1,5 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
import 'room_state_enums_extensions.dart';
@ -14,25 +15,27 @@ extension LocalizedBody on Event {
getLocalizedBody(BuildContext context,
{bool withSenderNamePrefix = false, hideQuotes = false}) {
if (this.redacted) {
return "Removed by ${this.redactedBecause.sender.calcDisplayname()}";
return I18n.of(context)
.removedBy(redactedBecause.sender.calcDisplayname());
}
String localizedBody = body;
final String senderName = this.sender.calcDisplayname();
switch (this.type) {
case EventTypes.Sticker:
localizedBody = "$senderName sent a sticker";
localizedBody = I18n.of(context).sentASticker(senderName);
break;
case EventTypes.Redaction:
localizedBody = "$senderName redacted an event";
localizedBody = I18n.of(context).redactedAnEvent(senderName);
break;
case EventTypes.RoomAliases:
localizedBody = "$senderName changed the room aliases";
localizedBody = I18n.of(context).changedTheRoomAliases(senderName);
break;
case EventTypes.RoomCanonicalAlias:
localizedBody = "$senderName changed the room invite link";
localizedBody =
I18n.of(context).changedTheRoomInvitationLink(senderName);
break;
case EventTypes.RoomCreate:
localizedBody = "$senderName created the room";
localizedBody = I18n.of(context).createdTheChat(senderName);
break;
case EventTypes.RoomJoinRules:
JoinRules joinRules = JoinRules.values.firstWhere(
@ -41,10 +44,10 @@ extension LocalizedBody on Event {
content["join_rule"],
orElse: () => null);
if (joinRules == null) {
localizedBody = "$senderName changed the join rules";
localizedBody = I18n.of(context).changedTheJoinRules(senderName);
} else {
localizedBody =
"$senderName changed the join rules to: ${joinRules.getLocalizedString(context)}";
localizedBody = I18n.of(context).changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(context));
}
break;
case EventTypes.RoomMember:
@ -58,35 +61,36 @@ extension LocalizedBody on Event {
: "";
if (newMembership != oldMembership) {
if (oldMembership == "invite" && newMembership == "join") {
text = "$targetName has accepted the invitation";
text = I18n.of(context).acceptedTheInvitation(targetName);
} else if (oldMembership == "invite" && newMembership == "leave") {
if (this.stateKey == this.senderId) {
text = "$targetName has rejected the invitation";
text = I18n.of(context).rejectedTheInvitation(targetName);
} else {
text = "$senderName has withdrawn the invitation for $targetName";
text = I18n.of(context)
.hasWithdrawnTheInvitationFor(senderName, targetName);
}
} else if (oldMembership == "leave" && newMembership == "join") {
text = "$targetName has joined the chat";
text = I18n.of(context).joinedTheChat(targetName);
} else if (oldMembership == "join" && newMembership == "ban") {
text = "$senderName has kicked and banned $targetName";
text = I18n.of(context).kickedAndBanned(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey != this.senderId) {
text = "$senderName has kicked $targetName";
text = I18n.of(context).kicked(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey == this.senderId) {
text = "$senderName has left the room";
text = I18n.of(context).userLeftTheChat(targetName);
} else if (oldMembership == "invite" && newMembership == "ban") {
text = "$senderName has banned $targetName";
text = I18n.of(context).bannedUser(senderName, targetName);
} else if (oldMembership == "leave" && newMembership == "ban") {
text = "$senderName has banned $targetName";
text = I18n.of(context).bannedUser(senderName, targetName);
} else if (oldMembership == "ban" && newMembership == "leave") {
text = "$senderName has unbanned $targetName";
text = I18n.of(context).unbannedUser(senderName, targetName);
} else if (newMembership == "invite") {
text = "$senderName has invited $targetName";
text = I18n.of(context).invitedUser(senderName, targetName);
} else if (newMembership == "join") {
text = "$targetName has joined";
text = I18n.of(context).joinedTheChat(targetName);
}
} else if (newMembership == "join") {
final String newAvatar = this.content["avatar_url"] ?? "";
@ -103,29 +107,29 @@ extension LocalizedBody on Event {
// Has the user avatar changed?
if (newAvatar != oldAvatar) {
text = "$targetName has changed the profile avatar";
text = I18n.of(context).changedTheProfileAvatar(targetName);
}
// Has the user avatar changed?
else if (newDisplayname != oldDisplayname) {
text =
"${this.stateKeyUser.id} has changed the displayname to '$newDisplayname'";
text = I18n.of(context)
.changedTheDisplaynameTo(targetName, newDisplayname);
}
}
localizedBody = text;
break;
case EventTypes.RoomPowerLevels:
localizedBody = "$senderName changed the group permissions";
localizedBody = I18n.of(context).changedTheChatPermissions(senderName);
break;
case EventTypes.RoomName:
localizedBody =
"$senderName changed the group name to: '${content["name"]}'";
I18n.of(context).changedTheChatNameTo(senderName, content["name"]);
break;
case EventTypes.RoomTopic:
localizedBody =
"$senderName changed the group name to: '${content["topic"]}'";
localizedBody = I18n.of(context)
.changedTheChatDescriptionTo(senderName, content["topic"]);
break;
case EventTypes.RoomAvatar:
localizedBody = "$senderName changed the group avatar";
localizedBody = I18n.of(context).changedTheChatAvatar(senderName);
break;
case EventTypes.GuestAccess:
GuestAccess guestAccess = GuestAccess.values.firstWhere(
@ -134,10 +138,11 @@ extension LocalizedBody on Event {
content["guest_access"],
orElse: () => null);
if (guestAccess == null) {
localizedBody = "$senderName changed the guest access rules";
} else {
localizedBody =
"$senderName changed the guest access rules to: ${guestAccess.getLocalizedString(context)}";
I18n.of(context).changedTheGuestAccessRules(senderName);
} else {
localizedBody = I18n.of(context).changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(context));
}
break;
case EventTypes.HistoryVisibility:
@ -148,52 +153,39 @@ extension LocalizedBody on Event {
content["history_visibility"],
orElse: () => null);
if (historyVisibility == null) {
localizedBody = "$senderName changed the history visibility";
} else {
localizedBody =
"$senderName changed the history visibility to: ${historyVisibility.getLocalizedString(context)}";
I18n.of(context).changedTheHistoryVisibility(senderName);
} else {
localizedBody = I18n.of(context).changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(context));
}
break;
case EventTypes.Encryption:
localizedBody = "$senderName activated end to end encryption";
localizedBody =
I18n.of(context).activatedEndToEndEncryption(senderName);
break;
case EventTypes.Encrypted:
localizedBody = "Could not decrypt message";
break;
case EventTypes.CallInvite:
localizedBody = body;
break;
case EventTypes.CallAnswer:
localizedBody = body;
break;
case EventTypes.CallCandidates:
localizedBody = body;
break;
case EventTypes.CallHangup:
localizedBody = body;
break;
case EventTypes.Unknown:
localizedBody = body;
localizedBody = I18n.of(context).couldNotDecryptMessage;
break;
case EventTypes.Message:
switch (this.messageType) {
case MessageTypes.Image:
localizedBody = "$senderName sent a picture";
localizedBody = I18n.of(context).sentAPicture(senderName);
break;
case MessageTypes.File:
localizedBody = "$senderName sent a file";
localizedBody = I18n.of(context).sentAFile(senderName);
break;
case MessageTypes.Audio:
localizedBody = "$senderName sent an audio";
localizedBody = I18n.of(context).sentAnAudio(senderName);
break;
case MessageTypes.Video:
localizedBody = "$senderName sent a video";
localizedBody = I18n.of(context).sentAVideo(senderName);
break;
case MessageTypes.Location:
localizedBody = "$senderName shared the location";
localizedBody = I18n.of(context).sharedTheLocation(senderName);
break;
case MessageTypes.Sticker:
localizedBody = "$senderName sent a sticker";
localizedBody = I18n.of(context).sentASticker(senderName);
break;
case MessageTypes.Emote:
localizedBody = "* $body";
@ -207,8 +199,7 @@ extension LocalizedBody on Event {
}
break;
default:
localizedBody =
"Unknown event '${this.type.toString().replaceAll("EventTypes.", "")}'";
localizedBody = I18n.of(context).unknownEvent(this.typeKey);
}
// Add the sender name prefix

View file

@ -1,4 +1,5 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
extension LocalizedRoomDisplayname on Room {
@ -7,7 +8,7 @@ extension LocalizedRoomDisplayname on Room {
(this.canonicalAlias?.isEmpty ?? true) &&
!this.isDirectChat &&
this.mHeroes.isNotEmpty) {
return "Group with ${this.displayname}";
return I18n.of(context).groupWith(this.displayname);
}
return this.displayname;
}

View file

@ -1,17 +1,18 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
extension HistoryVisibilityDisplayString on HistoryVisibility {
String getLocalizedString(BuildContext context) {
switch (this) {
case HistoryVisibility.invited:
return "From the invitation";
return I18n.of(context).fromTheInvitation;
case HistoryVisibility.joined:
return "From joining";
return I18n.of(context).fromJoining;
case HistoryVisibility.shared:
return "Visible for all participants";
return I18n.of(context).visibleForAllParticipants;
case HistoryVisibility.world_readable:
return "Visible for everyone";
return I18n.of(context).visibleForEveryone;
default:
return this.toString().replaceAll("HistoryVisibility.", "");
}
@ -22,9 +23,9 @@ extension GuestAccessDisplayString on GuestAccess {
String getLocalizedString(BuildContext context) {
switch (this) {
case GuestAccess.can_join:
return "Guests can join";
return I18n.of(context).guestsCanJoin;
case GuestAccess.forbidden:
return "Guests are forbidden";
return I18n.of(context).guestsAreForbidden;
default:
return this.toString().replaceAll("GuestAccess.", "");
}
@ -35,9 +36,9 @@ extension JoinRulesDisplayString on JoinRules {
String getLocalizedString(BuildContext context) {
switch (this) {
case JoinRules.public:
return "Anyone can join";
return I18n.of(context).anyoneCanJoin;
case JoinRules.invite:
return "Invited users only";
return I18n.of(context).invitedUsersOnly;
default:
return this.toString().replaceAll("JoinRules.", "");
}