A lightweight Swift package designed to effortlessly localize your SwiftUI project.
Example.mp4
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
dependencies: [
.Package(url: "https://github.com/nazar-41/Localized-Swift", branch: .main)
]
First thing is to import the framework and wrap your main view with LocalizedView
import Localized_Swift
import SwiftUI
@main
struct ExampleProjectApp: App {
var body: some Scene {
WindowGroup{
LocalizedView { // wrap main view
ContentView()
}
}
}
}
To localize text, simply replace
Text("Hello World")
with
LText("Hello World")
Use the .localized extension on a string:
Text("Hello World".localized)
To change the language anywhere in your app, simply call:
LocalizationService.setLanguage(newLanguage)
struct ContentView: View {
@State private var selectedLanguage: String = "en"
var languages = ["en", "ru", "fr"]
var body: some View {
VStack {
List{
Picker("Select Your Language", selection: $selectedLanguage) {
ForEach(languages, id: \.self) {
Text($0)
}
}
.pickerStyle(.segmented)
Section(header: Text("**LText**")) {
LText("welcome_message")
LText("login_button")
LText("error_message")
}
.textCase(nil)
Section(header: Text("**.localized**")){
Text("welcome_message".localized)
Text("login_button".localized)
Text("error_message".localized)
}
.textCase(nil)
}
}
.onChange(of: selectedLanguage) { newLanguage in
LocalizationService.setLanguage(newLanguage)
}
}
}
Feel free to explore more about Localized-Swift in the example repository.
If you have any questions, feedback, or issues, please raise an issue. Contributions are welcome!