iOS client for Grain grain.social
ios photography atproto
7
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add translate button for foreign language gallery descriptions

Uses Apple's NaturalLanguage framework to detect non-native text and
shows a translate link that opens the system Translation sheet.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

+35 -9
+35 -9
Grain/Views/Components/ExpandableDescriptionView.swift
··· 1 + import NaturalLanguage 1 2 import SwiftUI 3 + import Translation 2 4 3 5 struct ExpandableDescriptionView: View { 4 6 let text: String ··· 8 10 9 11 @State private var isExpanded = false 10 12 @State private var isTruncated = false 13 + @State private var showTranslation = false 14 + @State private var isForeignLanguage = false 11 15 12 16 var body: some View { 13 17 VStack(alignment: .leading, spacing: 2) { ··· 44 48 }) 45 49 ) 46 50 47 - if isTruncated, !isExpanded { 48 - Button { 49 - withAnimation(.easeInOut(duration: 0.2)) { 50 - isExpanded = true 51 + HStack(spacing: 12) { 52 + if isTruncated, !isExpanded { 53 + Button { 54 + withAnimation(.easeInOut(duration: 0.2)) { 55 + isExpanded = true 56 + } 57 + } label: { 58 + Text("more") 59 + .font(.subheadline) 60 + .foregroundStyle(.tertiary) 51 61 } 52 - } label: { 53 - Text("more") 54 - .font(.subheadline) 55 - .foregroundStyle(.tertiary) 62 + .buttonStyle(.plain) 56 63 } 57 - .buttonStyle(.plain) 64 + 65 + if isForeignLanguage { 66 + Button { 67 + showTranslation = true 68 + } label: { 69 + Text("translate") 70 + .font(.subheadline) 71 + .foregroundStyle(.tertiary) 72 + } 73 + .buttonStyle(.plain) 74 + } 75 + } 76 + } 77 + .translationPresentation(isPresented: $showTranslation, text: text) 78 + .onAppear { 79 + let recognizer = NLLanguageRecognizer() 80 + recognizer.processString(text) 81 + if let detected = recognizer.dominantLanguage?.rawValue { 82 + let preferred = Locale.preferredLanguages.first ?? "en" 83 + isForeignLanguage = !preferred.hasPrefix(detected) 58 84 } 59 85 } 60 86 }