언어 설정 화면 추가 및 언어 헤더 적용
설정에서 시스템/한국어/영어/일본어 선택을 지원한다. 선택 시 Accept-Language 헤더와 UI locale을 즉시 반영한다. 언어 변경 후 스플래시를 거쳐 메인으로 소프트 재시작한다.
This commit is contained in:
@@ -17,7 +17,7 @@ struct ContentSettingsView: View {
|
||||
|
||||
VStack(spacing: 0) {
|
||||
DetailNavigationBar(title: "콘텐츠 보기 설정") {
|
||||
if AppState.shared.isChangeAdultContentVisible {
|
||||
if AppState.shared.isRestartApp {
|
||||
AppState.shared.setAppStep(step: .splash)
|
||||
} else {
|
||||
AppState.shared.back()
|
||||
|
||||
@@ -12,7 +12,7 @@ final class ContentSettingsViewModel: ObservableObject {
|
||||
didSet {
|
||||
if oldValue != isAdultContentVisible {
|
||||
UserDefaults.set(isAdultContentVisible, forKey: .isAdultContentVisible)
|
||||
AppState.shared.isChangeAdultContentVisible = true
|
||||
AppState.shared.isRestartApp = true
|
||||
|
||||
if !isAdultContentVisible {
|
||||
adultContentPreference = .ALL
|
||||
@@ -26,7 +26,7 @@ final class ContentSettingsViewModel: ObservableObject {
|
||||
didSet {
|
||||
if oldValue != adultContentPreference {
|
||||
UserDefaults.set(adultContentPreference.rawValue, forKey: .contentPreference)
|
||||
AppState.shared.isChangeAdultContentVisible = true
|
||||
AppState.shared.isRestartApp = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// LanguageOption.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by Junie (AI) on 2025/12/16.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum LanguageOption: String, CaseIterable, Equatable {
|
||||
case system
|
||||
case ko
|
||||
case en
|
||||
case ja
|
||||
|
||||
var displayName: String {
|
||||
switch self {
|
||||
case .system: return String(localized: "시스템 기본")
|
||||
case .ko: return "한국어"
|
||||
case .en: return "English"
|
||||
case .ja: return "日本語"
|
||||
}
|
||||
}
|
||||
|
||||
var headerCode: String? { self == .system ? nil : self.rawValue }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// LanguageRepository.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by Junie (AI) on 2025/12/16.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol LanguageRepository {
|
||||
func load() async throws -> LanguageOption?
|
||||
func save(_ option: LanguageOption) async throws
|
||||
}
|
||||
|
||||
final class UserDefaultsLanguageRepository: LanguageRepository {
|
||||
private let key = "app.language"
|
||||
|
||||
func load() async throws -> LanguageOption? {
|
||||
try Task.checkCancellation()
|
||||
return await withCheckedContinuation { cont in
|
||||
let value = UserDefaults.standard.string(forKey: key)
|
||||
cont.resume(returning: value.flatMap { LanguageOption(rawValue: $0) })
|
||||
}
|
||||
}
|
||||
|
||||
func save(_ option: LanguageOption) async throws {
|
||||
try Task.checkCancellation()
|
||||
return await withCheckedContinuation { cont in
|
||||
UserDefaults.standard.set(option.rawValue, forKey: key)
|
||||
cont.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// AcceptLanguagePlugin.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by Junie (AI) on 2025/12/16.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Moya
|
||||
|
||||
struct AcceptLanguagePlugin: PluginType {
|
||||
func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
|
||||
var req = request
|
||||
let code = LanguageHeaderProvider.current
|
||||
req.setValue(code, forHTTPHeaderField: "Accept-Language")
|
||||
return req
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// LanguageEnvironment.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by Junie (AI) on 2025/12/16.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final class LanguageEnvironment: ObservableObject {
|
||||
@Published var locale: Locale = .current
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
//
|
||||
// LanguageService.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by Junie (AI) on 2025/12/16.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum LanguageHeaderProvider {
|
||||
private static let defaultsKey = "app.language"
|
||||
|
||||
// 현재 헤더 코드(ko|en|ja). 앱 시작 시 initialize()로 동기 설정.
|
||||
static var current: String = {
|
||||
// 초기값은 OS 선호 기반으로 계산
|
||||
return mapPreferredToSupported(Locale.preferredLanguages.first)
|
||||
}()
|
||||
|
||||
/// 앱 시작 직후 동기 초기화: 저장된 설정이 있으면 그 값을, 없으면 OS 선호를 사용.
|
||||
static func initialize() {
|
||||
if let raw = UserDefaults.standard.string(forKey: defaultsKey),
|
||||
let option = LanguageOption(rawValue: raw),
|
||||
option != .system {
|
||||
// 명시 선택 값
|
||||
current = option.rawValue
|
||||
return
|
||||
}
|
||||
// 시스템 기본 또는 저장값 없음 → OS 선호 매핑
|
||||
current = mapPreferredToSupported(Locale.preferredLanguages.first)
|
||||
}
|
||||
|
||||
private static func mapPreferredToSupported(_ tag: String?) -> String {
|
||||
guard let tag = tag else { return "en" }
|
||||
if tag.hasPrefix("ko") { return "ko" }
|
||||
if tag.hasPrefix("ja") { return "ja" }
|
||||
return "en"
|
||||
}
|
||||
}
|
||||
|
||||
actor LanguageService {
|
||||
private let repository: LanguageRepository
|
||||
private let environment: LanguageEnvironment
|
||||
|
||||
init(repository: LanguageRepository, environment: LanguageEnvironment) {
|
||||
self.repository = repository
|
||||
self.environment = environment
|
||||
}
|
||||
|
||||
func bootstrap() async {
|
||||
let saved = (try? await repository.load()) ?? .system
|
||||
await applyLanguage(saved)
|
||||
}
|
||||
|
||||
func applyLanguage(_ option: LanguageOption) async {
|
||||
do { try await repository.save(option) } catch { DEBUG_LOG("[Language] save failed: \(error)") }
|
||||
let locale = resolveLocale(option)
|
||||
let header = resolveHeader(option, locale: locale)
|
||||
LanguageHeaderProvider.current = header
|
||||
await MainActor.run { [weak environment] in
|
||||
environment?.locale = locale
|
||||
}
|
||||
DEBUG_LOG("[Language] changed to option=\(option.rawValue) locale=\(locale.identifier) header=\(header)")
|
||||
}
|
||||
|
||||
func currentLocale() async -> Locale {
|
||||
await MainActor.run { environment.locale }
|
||||
}
|
||||
|
||||
func currentLanguageCodeForHeader() async -> String { LanguageHeaderProvider.current }
|
||||
|
||||
// MARK: - Private
|
||||
private func resolveLocale(_ option: LanguageOption) -> Locale {
|
||||
switch option {
|
||||
case .system:
|
||||
guard let first = Locale.preferredLanguages.first else { return Locale(identifier: "en_US") }
|
||||
return mapToSupported(first)
|
||||
case .ko: return Locale(identifier: "ko_KR")
|
||||
case .en: return Locale(identifier: "en_US")
|
||||
case .ja: return Locale(identifier: "ja_JP")
|
||||
}
|
||||
}
|
||||
|
||||
private func resolveHeader(_ option: LanguageOption, locale: Locale) -> String {
|
||||
switch option {
|
||||
case .system:
|
||||
let id = locale.identifier
|
||||
if id.hasPrefix("ko") { return "ko" }
|
||||
if id.hasPrefix("ja") { return "ja" }
|
||||
return "en"
|
||||
case .ko: return "ko"
|
||||
case .en: return "en"
|
||||
case .ja: return "ja"
|
||||
}
|
||||
}
|
||||
|
||||
private func mapToSupported(_ tag: String) -> Locale {
|
||||
if tag.hasPrefix("ko") { return .init(identifier: "ko_KR") }
|
||||
if tag.hasPrefix("ja") { return .init(identifier: "ja_JP") }
|
||||
return .init(identifier: "en_US")
|
||||
}
|
||||
}
|
||||
|
||||
// Composition root for language feature
|
||||
enum LanguageContainer {
|
||||
static let environment = LanguageEnvironment()
|
||||
static let repository: LanguageRepository = UserDefaultsLanguageRepository()
|
||||
static let service = LanguageService(repository: repository, environment: environment)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// LanguageSettingsViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by Junie (AI) on 2025/12/16.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final class LanguageSettingsViewModel: ObservableObject {
|
||||
// 저장된(현재 적용된) 언어
|
||||
@Published var selected: LanguageOption = .system
|
||||
// 사용자가 화면에서 선택 중인 언어(적용 버튼 누르기 전 임시 상태)
|
||||
@Published var pending: LanguageOption = .system
|
||||
|
||||
private let service: LanguageService
|
||||
|
||||
init(service: LanguageService = LanguageContainer.service) {
|
||||
self.service = service
|
||||
}
|
||||
|
||||
func onAppear() {
|
||||
Task { await loadCurrent() }
|
||||
}
|
||||
|
||||
private func loadCurrent() async {
|
||||
let locale = await service.currentLocale()
|
||||
let option = map(locale: locale)
|
||||
await MainActor.run {
|
||||
self.selected = option
|
||||
self.pending = option
|
||||
}
|
||||
}
|
||||
|
||||
func select(_ option: LanguageOption) {
|
||||
// 즉시 저장/적용하지 않고, 사용자가 '적용' 버튼을 누를 때까지 대기
|
||||
self.pending = option
|
||||
}
|
||||
|
||||
/// 선택한 언어를 저장/적용하고, 앱을 소프트 재시작하여 즉시 반영
|
||||
func applyAndRestart() async {
|
||||
let toApply = pending
|
||||
await service.applyLanguage(toApply)
|
||||
await MainActor.run {
|
||||
self.selected = toApply
|
||||
// 앱 소프트 재시작(스플래시 -> 메인)
|
||||
AppState.shared.softRestart()
|
||||
}
|
||||
}
|
||||
|
||||
private func map(locale: Locale) -> LanguageOption {
|
||||
let id = locale.identifier
|
||||
if id.hasPrefix("ko") { return .ko }
|
||||
if id.hasPrefix("ja") { return .ja }
|
||||
if id.hasPrefix("en") { return .en }
|
||||
return .system
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// LanguageSettingsView.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by Junie (AI) on 2025/12/16.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct LanguageSettingsView: View {
|
||||
@StateObject private var viewModel = LanguageSettingsViewModel()
|
||||
|
||||
var body: some View {
|
||||
let cardWidth = screenSize().width - 26.7
|
||||
|
||||
ZStack {
|
||||
Color.black.ignoresSafeArea()
|
||||
|
||||
VStack(spacing: 0) {
|
||||
DetailNavigationBar(title: String(localized: "언어 설정"))
|
||||
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
VStack(spacing: 0) {
|
||||
// List를 ScrollView 안에 두면 렌더링 이슈가 발생할 수 있어
|
||||
// 기본 체크마크(SF Symbols)로 커스텀 라디오 UI를 구성합니다.
|
||||
VStack(spacing: 0) {
|
||||
ForEach(Array(LanguageOption.allCases.enumerated()), id: \.offset) { idx, option in
|
||||
HStack(spacing: 0) {
|
||||
Text(option.displayName)
|
||||
.font(.custom(Font.preRegular.rawValue, size: 16))
|
||||
.foregroundColor(Color.grayee)
|
||||
Spacer()
|
||||
if viewModel.pending == option {
|
||||
Image(systemName: "checkmark")
|
||||
.font(.system(size: 16, weight: .semibold))
|
||||
.foregroundColor(Color.white)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 16.7)
|
||||
.frame(height: 50)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture { viewModel.select(option) }
|
||||
|
||||
if idx < LanguageOption.allCases.count - 1 {
|
||||
Rectangle()
|
||||
.frame(height: 0.3)
|
||||
.foregroundColor(Color.gray90)
|
||||
.padding(.leading, 16.7)
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(width: cardWidth)
|
||||
.background(Color.gray22)
|
||||
.cornerRadius(6.7)
|
||||
.padding(.top, 26.7)
|
||||
|
||||
// 적용 버튼
|
||||
Button(action: {
|
||||
Task { await viewModel.applyAndRestart() }
|
||||
}) {
|
||||
Text(String(localized: "적용"))
|
||||
.font(.custom(Font.preBold.rawValue, size: 16))
|
||||
.frame(width: cardWidth, height: 50)
|
||||
.background(Color.button)
|
||||
.cornerRadius(6.7)
|
||||
.foregroundColor(.white)
|
||||
}
|
||||
.padding(.top, 20)
|
||||
.padding(.bottom, 26.7)
|
||||
}
|
||||
}
|
||||
}
|
||||
.task { viewModel.onAppear() }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,28 @@ struct SettingsView: View {
|
||||
AppState.shared.setAppStep(step: .notificationSettings)
|
||||
}
|
||||
|
||||
Rectangle()
|
||||
.frame(width: cardWidth - 26.7, height: 0.3)
|
||||
.foregroundColor(Color.gray90)
|
||||
|
||||
HStack(spacing: 0) {
|
||||
Text("언어 설정")
|
||||
.font(.custom(Font.bold.rawValue, size: 14.7))
|
||||
.foregroundColor(Color.grayee)
|
||||
|
||||
Spacer()
|
||||
|
||||
Image("ic_forward")
|
||||
.resizable()
|
||||
.frame(width: 20, height: 20)
|
||||
}
|
||||
.padding(.horizontal, 3.3)
|
||||
.frame(width: cardWidth - 26.7, height: 50)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
AppState.shared.setAppStep(step: .languageSettings)
|
||||
}
|
||||
|
||||
if UserDefaults.bool(forKey: .auth) {
|
||||
Rectangle()
|
||||
.frame(width: cardWidth - 26.7, height: 0.3)
|
||||
@@ -205,7 +227,7 @@ struct SettingsView: View {
|
||||
UserDefaults.reset()
|
||||
recentContentViewModel.truncate()
|
||||
|
||||
AppState.shared.isChangeAdultContentVisible = true
|
||||
AppState.shared.isRestartApp = true
|
||||
AppState.shared.setAppStep(step: .splash)
|
||||
}
|
||||
},
|
||||
@@ -227,7 +249,7 @@ struct SettingsView: View {
|
||||
viewModel.logoutAllDevice {
|
||||
self.isShowLogoutAllDeviceDialog = false
|
||||
UserDefaults.reset()
|
||||
AppState.shared.isChangeAdultContentVisible = true
|
||||
AppState.shared.isRestartApp = true
|
||||
AppState.shared.setAppStep(step: .splash)
|
||||
}
|
||||
},
|
||||
|
||||
@@ -56,7 +56,7 @@ final class SignOutViewModel: ObservableObject {
|
||||
|
||||
if decoded.success {
|
||||
UserDefaults.reset()
|
||||
AppState.shared.isChangeAdultContentVisible = true
|
||||
AppState.shared.isRestartApp = true
|
||||
AppState.shared.setAppStep(step: .splash)
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
|
||||
Reference in New Issue
Block a user