Localize-Swift Documentation

repository·master·Indexed 25 days ago

https://github.com/marmelroy/localize-swift

A Swift framework for iOS applications that improves i18n and localization. It provides a .localized() method for string translation, enables in-app language switching via the Localize class without changing device settings, and includes a genstrings.swift script for string collection.

Tokens
773
Snippets
7
Records
8
Agent score
35%

What's inside Localize-Swift

  1. Generate strings using genstrings.swift

    master

    Localize-Swift includes a custom genstrings.swift script that recognizes the .localized() syntax.

    1. Copy genstrings.swift into your project's root folder.
    2. Run the script from the terminal:
      ./genstrings.swift
    3. The script will print collected strings to the terminal. Copy these into your Localizable.strings file.

    Note: You can exclude specific directories or files by editing the script directly.

    ./genstrings.swift
  2. Install Localize-Swift via Swift Package Manager

    master

    Swift Package Manager is the preferred method for installing Localize-Swift.

    Using Xcode UI:

    1. Go to File > Swift Packages > Add Package Dependency.
    2. Enter https://github.com/marmelroy/Localize-Swift.git.
    3. Set the version rule to Up to Next Major starting from 3.2.0.
    4. Add the Localize-Swift library to your app target.

    Using Package.swift: Add the dependency directly to your Package.swift file.

    dependencies: [
        .package(url: "https://github.com/marmelroy/Localize-Swift.git", .upToNextMajor(from: "3.2.0"))
    ]
  3. Use .localized() for string translation

    master

    After importing the library, you can use the .localized() method on any String object to retrieve its translated value from your Localizable.strings file.

    import Localize_Swift
    
    textLabel.text = "Hello World".localized()
  4. Manage app language with Localize

    master

    Use the Localize class to query available languages, change the current language, or reset to the system default.

    • Localize.availableLanguages(): Returns an array of available localizations.
    • Localize.setCurrentLanguage("code"): Changes the app's language to the specified code (e.g., "fr").
    • Localize.resetCurrentLanguageToDefault(): Resets the language to the device's default language.
  5. Observe language changes with LCLLanguageChangeNotification

    master

    To update your UI when the user changes the app language, observe the LCLLanguageChangeNotification via NotificationCenter.

    NotificationCenter.default.addObserver(
        self, 
        selector: #selector(setText), 
        name: NSNotification.Name(LCLLanguageChangeNotification), 
        object: nil
    )