-
Notifications
You must be signed in to change notification settings - Fork 100
Show translated text for participant messages #776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b0dbffe
b25cc61
f882035
926cfaf
af2af77
89868fa
5b5722e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// Copyright © 2025 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import StreamChat | ||
import StreamChatSwiftUI | ||
|
||
final class AppConfiguration { | ||
static let `default` = AppConfiguration() | ||
|
||
/// The translation language to set on connect. | ||
var translationLanguage: TranslationLanguage? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Copyright © 2025 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChat | ||
import SwiftUI | ||
|
||
struct AppConfigurationTranslationView: View { | ||
@Environment(\.dismiss) var dismiss | ||
|
||
var selection: Binding<TranslationLanguage?> = Binding { | ||
AppConfiguration.default.translationLanguage | ||
} set: { newValue in | ||
AppConfiguration.default.translationLanguage = newValue | ||
} | ||
|
||
var body: some View { | ||
List { | ||
ForEach(TranslationLanguage.all, id: \.languageCode) { language in | ||
Button(action: { | ||
selection.wrappedValue = language | ||
dismiss() | ||
}) { | ||
HStack { | ||
Text(language.languageCode) | ||
Spacer() | ||
if selection.wrappedValue == language { | ||
Image(systemName: "checkmark") | ||
} | ||
} | ||
} | ||
.foregroundStyle(.primary) | ||
} | ||
.navigationTitle("Translation Language") | ||
} | ||
} | ||
} | ||
|
||
extension TranslationLanguage { | ||
static let all = allCases.sorted(by: { $0.languageCode < $1.languageCode }) | ||
} | ||
|
||
#Preview { | ||
NavigationView { | ||
AppConfigurationTranslationView() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Copyright © 2025 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import SwiftUI | ||
|
||
struct AppConfigurationView: View { | ||
var body: some View { | ||
NavigationView { | ||
List { | ||
Section("Connect User Configuration") { | ||
NavigationLink("Translation") { | ||
AppConfigurationTranslationView() | ||
} | ||
} | ||
} | ||
.navigationBarTitleDisplayMode(.inline) | ||
.navigationTitle("App Configuration") | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
AppConfigurationView() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable { | |
onLongPress: handleLongPress(messageDisplayInfo:), | ||
isLast: !showsLastInGroupInfo && message == messages.last | ||
) | ||
.environment(\.channelTranslationLanguage, channel.membership?.language) | ||
.onAppear { | ||
if index == nil { | ||
index = messageListDateUtils.index(for: message, in: messages) | ||
|
@@ -597,3 +598,18 @@ private class MessageRenderingUtil { | |
return skipRendering | ||
} | ||
} | ||
|
||
private struct ChannelTranslationLanguageKey: EnvironmentKey { | ||
static let defaultValue: TranslationLanguage? = nil | ||
} | ||
|
||
extension EnvironmentValues { | ||
var channelTranslationLanguage: TranslationLanguage? { | ||
get { | ||
self[ChannelTranslationLanguageKey.self] | ||
} | ||
set { | ||
self[ChannelTranslationLanguageKey.self] = newValue | ||
} | ||
} | ||
} | ||
Comment on lines
+602
to
+615
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Channel data is not available in the message's text view. Interfaces are public so no easy way to forward this information with function arguments. Next best way is to use environment key for this. Note: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. V5 is a good option to improve this. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,12 @@ public extension ChatMessage { | |
|
||
return isDeleted ? L10n.Message.deletedMessagePlaceholder : adjustedText | ||
} | ||
|
||
func textContent(for translationLanguage: TranslationLanguage?) -> String? { | ||
guard let translationLanguage else { return nil } | ||
guard !isSentByCurrentUser, !isDeleted else { return nil } | ||
return translatedText(for: translationLanguage) | ||
} | ||
Comment on lines
+50
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convenience since similar logic is used in multiple places |
||
|
||
/// A boolean value that checks if the message is visible for current user only. | ||
var isOnlyVisibleForCurrentUser: Bool { | ||
|
@@ -95,3 +101,9 @@ public extension ChatMessage { | |
return isSentByCurrentUser | ||
} | ||
} | ||
|
||
extension TranslationLanguage { | ||
var localizedName: String? { | ||
Locale.current.localizedString(forLanguageCode: languageCode) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Customers might have their own message composer view implementation. Just to make sure, if we access it
@Environment(\.channelTranslationLanguage) var translationLanguage
, but this line is not called, it won't crash, because it's optional, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, then it uses the the default value for the key which is nil