11import React , { useState } from 'react'
2+ import { uiLanguage } from '../lib/lang'
23import type { Translations } from '../strings/types'
34import InfoIcon from './icons/InfoIcon'
45import type { StatementData } from './types'
@@ -23,6 +24,59 @@ export function Statement({
2324 voteError
2425} : StatementProps ) {
2526 const [ showImportanceDesc , setShowImportanceDesc ] = useState < boolean > ( false )
27+ const [ translationsEnabled , setTranslationsEnabled ] = useState < boolean > ( false )
28+
29+ // Get current user language
30+ const currentLang = uiLanguage ( )
31+ const statementLang = statement . lang
32+ const langMismatch = statementLang && currentLang && statementLang !== currentLang
33+
34+ // Find matching translation if translations array exists
35+ const matchingTranslation = statement . translations ?. find ( ( t ) => t . lang === currentLang )
36+
37+ // Determine if we have an official translation (src > 0)
38+ const hasOfficialTranslation = langMismatch && matchingTranslation && matchingTranslation . src > 0
39+
40+ // Determine if we have a non-official translation (src <= 0)
41+ const hasNonOfficialTranslation =
42+ langMismatch && matchingTranslation && matchingTranslation . src <= 0
43+
44+ // Show translation button only if:
45+ // - Translations not enabled
46+ // - Language mismatch exists
47+ // - NOT official translation
48+ // - AND (no translation exists OR translation is non-official)
49+ const shouldShowTranslationButton =
50+ ! translationsEnabled &&
51+ langMismatch &&
52+ ! hasOfficialTranslation &&
53+ ( ! matchingTranslation || hasNonOfficialTranslation )
54+
55+ // Show hide button only if:
56+ // - Translations enabled
57+ // - AND (we have a non-official translation OR no translations array)
58+ const shouldShowHideButton =
59+ translationsEnabled && ( hasNonOfficialTranslation || ! statement . translations )
60+
61+ // Debug logging
62+ if (
63+ typeof window !== 'undefined' &&
64+ statement . translations &&
65+ statement . translations . length > 0
66+ ) {
67+ console . log ( '[Translation Debug]' , {
68+ currentLang,
69+ statementLang,
70+ langMismatch,
71+ matchingTranslation,
72+ hasOfficialTranslation,
73+ hasNonOfficialTranslation,
74+ shouldShowTranslationButton,
75+ shouldShowHideButton,
76+ translationsEnabled,
77+ translations : statement . translations
78+ } )
79+ }
2680
2781 const handleVoteClick = ( voteType : number ) => {
2882 if ( isVoting ) return
@@ -54,7 +108,72 @@ export function Statement({
54108 </ span >
55109 ) }
56110 </ div >
57- < p className = "statement-text" > { statement . txt } </ p >
111+
112+ { /* Show official translation (replaces original) or original text */ }
113+ { hasOfficialTranslation ? (
114+ < p className = "statement-text" > { matchingTranslation . txt } </ p >
115+ ) : (
116+ < p className = "statement-text" > { statement . txt } </ p >
117+ ) }
118+
119+ { /* Show translation buttons only if not using official translation */ }
120+ { ! hasOfficialTranslation && shouldShowTranslationButton && (
121+ < button
122+ className = "translation-button"
123+ onClick = { ( ) => {
124+ // No-op for now
125+ setTranslationsEnabled ( true )
126+ } }
127+ style = { {
128+ marginTop : '0.5rem' ,
129+ padding : '0.5rem 1rem' ,
130+ fontSize : '0.875rem' ,
131+ backgroundColor : 'transparent' ,
132+ border : '1px solid var(--color-border, #ccc)' ,
133+ borderRadius : '4px' ,
134+ cursor : 'pointer' ,
135+ color : 'var(--color-text, #333)'
136+ } }
137+ >
138+ { s . showTranslationButton }
139+ </ button >
140+ ) }
141+
142+ { ! hasOfficialTranslation && shouldShowHideButton && (
143+ < button
144+ className = "translation-button"
145+ onClick = { ( ) => {
146+ // No-op for now
147+ setTranslationsEnabled ( false )
148+ } }
149+ style = { {
150+ marginTop : '0.5rem' ,
151+ padding : '0.5rem 1rem' ,
152+ fontSize : '0.875rem' ,
153+ backgroundColor : 'transparent' ,
154+ border : '1px solid var(--color-border, #ccc)' ,
155+ borderRadius : '4px' ,
156+ cursor : 'pointer' ,
157+ color : 'var(--color-text, #333)'
158+ } }
159+ >
160+ { s . hideTranslationButton }
161+ </ button >
162+ ) }
163+
164+ { /* Show non-official translation below original text when enabled */ }
165+ { hasNonOfficialTranslation && translationsEnabled && (
166+ < p
167+ className = "statement-text"
168+ style = { {
169+ marginTop : '0.5rem' ,
170+ fontStyle : 'italic' ,
171+ color : 'var(--color-text-secondary, #666)'
172+ } }
173+ >
174+ { matchingTranslation . txt }
175+ </ p >
176+ ) }
58177
59178 < div className = "importance-container" >
60179 < label htmlFor = "important" > { s . importantCheckbox } </ label >
0 commit comments