Skip to content

A lightweight Swift package designed to effortlessly localize your SwiftUI project.

Notifications You must be signed in to change notification settings

nazar-41/Localized-Swift

Repository files navigation

Localized-Swift

A lightweight Swift package designed to effortlessly localize your SwiftUI project.

Example.mp4

Installation (Swift Package Manager)

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)
]

Simple Integration

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()
            }
        }
    }
}

Localize Text

Method 1

To localize text, simply replace

Text("Hello World")

with

LText("Hello World")

Method 2

Use the .localized extension on a string:

Text("Hello World".localized)

✅ Both methods are equally effective. Use whichever you find easier

Language Switching

To change the language anywhere in your app, simply call:

LocalizationService.setLanguage(newLanguage)

Demo

Example Project

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!

About

A lightweight Swift package designed to effortlessly localize your SwiftUI project.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages