// strings (English)
"text_title" = "Title";
// strings (Korean)
"text_title" = "타이틀";
// strings (Japanese)
"text_title" = "タートル";
// CustomUIView.swift
import UIKit
class CustomUIView : UIView
{
private let label: UILabel =
{
let lbl = UILabel()
lbl.text = NSLocalizedString("text_title", tableName: "strings", comment: "this is title")
lbl.textAlignment = .center
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl
}()
override init(frame: CGRect)
{
super.init(frame: frame)
setupView()
}
required init?(coder: NSCoder)
{
super.init(coder: coder)
setupView()
}
private func setupView()
{
addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: centerXAnchor),
label.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
}
// ContentView.swift
import SwiftUI
struct ContentView : UIViewRepresentable
{
func makeUIView(context: Context) -> UIView
{
return CustomUIView()
}
func updateUIView(_ uiView: UIView, context: Context)
{
}
}
struct ContentView_Previews: PreviewProvider
{
static var previews: some View
{
Group
{
ContentView()
.environment(\.locale, Locale(identifier: "en_US"))
.previewDisplayName("English")
.onAppear
{
UserDefaults.standard.set(["en"], forKey: "AppleLanguages")
}
ContentView()
.environment(\.locale, Locale(identifier: "ko_KR"))
.previewDisplayName("Korean")
.onAppear
{
UserDefaults.standard.set(["ko"], forKey: "AppleLanguages")
}
ContentView()
.environment(\.locale, Locale(identifier: "ja_JP"))
.previewDisplayName("Japanese")
.onAppear
{
UserDefaults.standard.set(["ja"], forKey: "AppleLanguages")
}
}
}
}
※ 프리뷰가 XCode 버그로 바로 적용되지 않으니 위 두 버튼을 토글해주면 적용된다.
'Mobile App Development > IOS' 카테고리의 다른 글
[ IOS ] XCode 단축키 정리 (0) | 2024.05.17 |
---|---|
[ IOS ] IOS 탈옥 - Palera1n ( 2 / 2 ) (1) | 2024.05.16 |
[ IOS ] IOS 탈옥 ( 1 / 2 ) (0) | 2024.05.14 |
[ IOS ] 프로젝트 코코아팟 라이브러리(SnapKit) 적용 (0) | 2024.05.13 |
[ IOS ] UIViewController 프리뷰 확인 (0) | 2024.05.13 |