diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-09-12 19:44:01 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-09-12 19:44:01 +0200 |
commit | 85b8ec7702f5423c82d601c9c265415de344e26e (patch) | |
tree | 2b62a61dc8fb9540781b44daf781e5fad8a61d40 /src/main/java/de/pixart/messenger/xml/LocalizedContent.java | |
parent | 0abffde5ef442e8bff1ce7cbfe84bf7e750b3136 (diff) |
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.
Disabling 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 we now show the displayed language in a corner of the
message bubble if more than one translation has been received.
If multiple languages are received we 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 we will discard (not show at all) messages with with
multiple bodies of the same language. (This is considered an invalid message)
The language tag will not be shown if we receive 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.
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/xml/LocalizedContent.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/xml/LocalizedContent.java b/src/main/java/de/pixart/messenger/xml/LocalizedContent.java new file mode 100644 index 000000000..be7c2e77e --- /dev/null +++ b/src/main/java/de/pixart/messenger/xml/LocalizedContent.java @@ -0,0 +1,57 @@ +package de.pixart.messenger.xml; + +import com.google.common.collect.Iterables; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class LocalizedContent { + + public static final String STREAM_LANGUAGE = "en"; + + public final String content; + public final String language; + public final int count; + + private LocalizedContent(String content, String language, int count) { + this.content = content; + this.language = language; + this.count = count; + } + + public static LocalizedContent get(final Element element, String name) { + final HashMap<String, String> contents = new HashMap<>(); + for (Element child : element.children) { + if (name.equals(child.getName())) { + final String namespace = child.getNamespace(); + final String lang = child.getAttribute("xml:lang"); + final String content = child.getContent(); + if (content != null && (namespace == null || "jabber:client".equals(namespace))) { + if (contents.put(lang, content) != null) { + //anything that has multiple contents for the same language is invalid + return null; + } + } + } + } + if (contents.size() == 0) { + return null; + } + final String userLanguage = Locale.getDefault().getLanguage(); + final String localized = contents.get(userLanguage); + if (localized != null) { + return new LocalizedContent(localized, userLanguage, contents.size()); + } + final String defaultLanguageContent = contents.get(null); + if (defaultLanguageContent != null) { + return new LocalizedContent(defaultLanguageContent, STREAM_LANGUAGE, contents.size()); + } + final String streamLanguageContent = contents.get(STREAM_LANGUAGE); + if (streamLanguageContent != null) { + return new LocalizedContent(streamLanguageContent, STREAM_LANGUAGE, contents.size()); + } + final Map.Entry<String, String> first = Iterables.get(contents.entrySet(), 0); + return new LocalizedContent(first.getValue(), first.getKey(), contents.size()); + } +}
\ No newline at end of file |