anotherim/src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java
Daniel Gultsch 9bf5fb98ac show language in message bubble if multiple language variants were received
XML and by inheritence XMPP has the feature of transmitting multiple language
variants for the same content. This can be really useful if, for example, you
are talking to an automated system. A chat bot could greet you in your own
language.

On the wire this will usually look like this:

```xml
<message to="you">
  <body>Good morning</body>
  <body xml:lang="de">Guten Morgen</body>
</message>
```

However receiving such a message in a group chat can be very confusing and
potentially dangerous if the sender puts conflicting information in there and
different people get shown different strings.

Disabeling support for localization entirely isn’t an ideal solution as on
principle it is still a good feature; and other clients might still show a
localization even if Conversations would always show the default language.

So instead Conversations now shows the displayed language in a corner of the
message bubble if more than one translation has been received.

If multiple languages are received Conversations will attempt to find one in
the language the operating system is set to. If no such translation can be
found it will attempt to display the English string.

If English can not be found either (for example a message that only has ru and
fr on a phone that is set to de) it will display what ever language came first.

Furthermore Conversations will discard (not show at all) messages with with
multiple bodies of the same language. (This is considered an invalid message)

The lanuage tag will not be shown if Conversations received a single body in
a language not understood by the user. (For example operating system set to
'de' and message received with one body in 'ru' will just display that body as
usual.)

As a guide line to the user: If you are reading a message where it is important
that this message is not interpreted differently by different people (like a
vote (+1 / -1) in a chat room) make sure it has *no* language tag.
2019-09-12 10:12:51 +02:00

101 lines
2.5 KiB
Java

package eu.siacs.conversations.xmpp.stanzas;
import android.util.Pair;
import eu.siacs.conversations.parser.AbstractParser;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.LocalizedContent;
public class MessagePacket extends AbstractAcknowledgeableStanza {
public static final int TYPE_CHAT = 0;
public static final int TYPE_NORMAL = 2;
public static final int TYPE_GROUPCHAT = 3;
public static final int TYPE_ERROR = 4;
public static final int TYPE_HEADLINE = 5;
public MessagePacket() {
super("message");
}
public LocalizedContent getBody() {
return findInternationalizedChildContentInDefaultNamespace("body");
}
public void setBody(String text) {
this.children.remove(findChild("body"));
Element body = new Element("body");
body.setContent(text);
this.children.add(0, body);
}
public void setAxolotlMessage(Element axolotlMessage) {
this.children.remove(findChild("body"));
this.children.add(0, axolotlMessage);
}
public void setType(int type) {
switch (type) {
case TYPE_CHAT:
this.setAttribute("type", "chat");
break;
case TYPE_GROUPCHAT:
this.setAttribute("type", "groupchat");
break;
case TYPE_NORMAL:
break;
case TYPE_ERROR:
this.setAttribute("type","error");
break;
default:
this.setAttribute("type", "chat");
break;
}
}
public int getType() {
String type = getAttribute("type");
if (type == null) {
return TYPE_NORMAL;
} else if (type.equals("normal")) {
return TYPE_NORMAL;
} else if (type.equals("chat")) {
return TYPE_CHAT;
} else if (type.equals("groupchat")) {
return TYPE_GROUPCHAT;
} else if (type.equals("error")) {
return TYPE_ERROR;
} else if (type.equals("headline")) {
return TYPE_HEADLINE;
} else {
return TYPE_NORMAL;
}
}
public Pair<MessagePacket,Long> getForwardedMessagePacket(String name, String namespace) {
Element wrapper = findChild(name, namespace);
if (wrapper == null) {
return null;
}
Element forwarded = wrapper.findChild("forwarded", "urn:xmpp:forward:0");
if (forwarded == null) {
return null;
}
MessagePacket packet = create(forwarded.findChild("message"));
if (packet == null) {
return null;
}
Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
return new Pair(packet,timestamp);
}
public static MessagePacket create(Element element) {
if (element == null) {
return null;
}
MessagePacket packet = new MessagePacket();
packet.setAttributes(element.getAttributes());
packet.setChildren(element.getChildren());
return packet;
}
}