Generate Swift and SwiftUI symbols for localized strings files.

Overview

localized-strings-symbols

An SPM and Xcode build plugin for creating Swift symbols for localized string keys.

What It Does

This is a very simple build plugin that creates Swift structures representing the keys in your app's .strings files. This allows the compiler to check you're referencing valid strings, and provides some nice features for format tokens.

The plugin creates two "routes" for each key in your .strings file:

  • As an extension on LocalizedStringKey for use with SwiftUI

  • As a struct named after the table name (i.e., Localizable) for use with everything else.

If you follow the SwiftUI convention of including format specifiers in your keys, the generated structures will automatically include strongly-typed functions for the format values. In SwiftUI contexts(i.e., extensions on LocalizedStringKey), functions are also provided for inlining SF Symbols via Image parameters.

For example, given the following strings file, named Localizable.strings:

"NameAlertTitle %@" = "Hello %@!";
"OKButtonTitle" = "OK";

The output will provide symbols that can be used as such:

// In a SwiftUI context
VStack {
    Text(.nameAlertTitle(formatValue: "Daniel"))
    Text(.nameAlertTitle(imageValue: Image(systemName: "exclamationmark.triangle")))
}

// In a non-SwiftUI context
let alertTitle: String = Localizable.nameAlertTitle(formatValue: "Daniel")

Limitations

Right now this is a very basic plugin, and as such there are a number of limitations and requirements. If you'd like to help make it better, pull requests are welcome!

  • Strings file keys are expected to be in CamelCase, with format specifiers space-separated at the end of the key matching in number to the ones in the value (for example: "NameAlertTitle %@" = "Hello %@!";).

  • Only the %@ format specifier is supported.

  • Multiple format specifiers aren't very intelligent in the LocalizedStringKey output. For example, if you have two specifiers and want one format value to be a String and the other an Image, that isn't currently provided for.

  • In the LocalizedStringKey output, only functions with String and Image parameters are generated.

  • It doesn't appear that build plug-ins can find out what a project's development language is, so instead the plug-in chooses the .strings file with the most keys in it as the source.

How To Use

Xcode Projects

Important: Build plugins like this one require Xcode 14 beta 3 or higher.

First, add the package to your project by clicking the + button at the bottom of the package list in your project's Package Dependencies tab, then pasting the package's URL into the search field: https://github.com/iKenndac/localized-strings-symbols.git.

Then, in the following dialog, make sure the package is added as a dependency to the relevant target(s).

Finally, go to your target(s) Build Phases tab, and in the Run Build Tool Plug-ins phase, add the plug-in.

SPM Packages

First, add the package to your dependencies list:


dependencies: [
    .package(url: "https://github.com/iKenndac/localized-strings-symbols.git", branch: "main")
],

Then, add the dependency and the plugin to your target(s):


    .target(
        name: "MyCoolPackage",
        dependencies: [.product(name: "Generate Strings File Symbols", package: "localized-strings-symbols")],
        plugins: ["Generate Strings File Symbols"]
    ),
You might also like...
Simple and light weight facebook login library for UIKit & SwiftUI
Simple and light weight facebook login library for UIKit & SwiftUI

MjFbLogin Simple and light weight facebook login library which provides support for UIKit & SwiftUI Example To run the example project, clone the repo

Pure SwiftUI state-driven library to present view sequences and hierarchies.
Pure SwiftUI state-driven library to present view sequences and hierarchies.

PathPresenter swiftUIOnboarding.mp4 Pure SwiftUI routing with transitions, animations, and .sheet() support. In SwiftUI, View is a function of the sta

A Drag-and-Drop library in pure SwiftUI.
A Drag-and-Drop library in pure SwiftUI.

SwiftUI Drag-and-Drop Drag-and-drop is an intuitive gesture and can improve the UX of an app. Chess Emoji Art Card Game To Do List Documentation Docum

iOS Programming with Swift - SwiftUI Edition

swiftuilectures SwiftUI Lectures Репозитори, в което се съхряняват лекциите за курса по iOS със SwiftUI. Анкета Моля, попълнете следната анкета. Онлай

SwiftUI project demonstrating Custom coded confetti animation for checkout page
SwiftUI project demonstrating Custom coded confetti animation for checkout page

Confetti-Checkout SwiftUI project demonstrating Custom coded confetti animation for checkout page NOTE: CAEmitterLayer is not used but all the confett

Fortune spinning wheel library built using SwiftUI, supports dynamic content.
Fortune spinning wheel library built using SwiftUI, supports dynamic content.

Fortune Wheel Fortune spinning wheel 🎡 library built using SwiftUI, supports dynamic content. Preview - Spin Wheel ⚙️ CocoaPods Installation FortuneW

Incognito animation made with SwiftUI
Incognito animation made with SwiftUI

IncognitoSwiftUI Incognito animation made with SwiftUI Incognito Mode / Private Browsing Private browsing is a privacy feature in some web browsers. W

SwiftUI Animation
SwiftUI Animation

Hey there Hi, I'm Arvind Patel, a iOS Developer 🚀 from India. Beside's programming, I enjoy eating food and Watching Movies. I'm a creative problem s

SwiftUI Package for Configurable Confetti Animation 🎉
SwiftUI Package for Configurable Confetti Animation 🎉

🎊 ConfettiSwiftUI 🎊 Swift package for displaying configurable confetti animation. Find the demo project here. Installation: It requires iOS 14 and X

Comments
  • Using plugin externally from another package doesn't work

    Using plugin externally from another package doesn't work

    Followed instructions in README.

    schwa@ungoliant Test on  main [✘!?] via 🐦 v5.7.1 >
    swift build
    Fetching https://github.com/iKenndac/localized-strings-symbols.git from cache
    Fetched https://github.com/iKenndac/localized-strings-symbols.git (0.72s)
    Creating working copy for https://github.com/iKenndac/localized-strings-symbols.git
    Working copy of https://github.com/iKenndac/localized-strings-symbols.git resolved at main
    error: 'test': no plugin named 'Generate Strings File Symbols' found
    

    Attached test project.

    opened by schwa 3
Releases(0.0.3)
Owner
Daniel Kennett
Cocoa developer, mountain biker, model railway owner. I spend my days working on @Cascable.
Daniel Kennett
SwiftUI animated image view that works on iOS and layout just as SwiftUI.Image

SwiftUI.AnimatedImage SwiftUI animated image view that works on iOS and layout just as SwiftUI.Image Screen.Recording.2021-07-31.at.02.18.33.mov Insta

Marcin Krzyzanowski 50 Oct 14, 2022
Swiftui-animation-observer - Track SwiftUI animation progress and completion via callbacks

SwiftUI Animation Observer Track SwiftUI animation progress and completion via c

Gordan Glavaš 9 Nov 5, 2022
SwiftUI-Text-Animation-Library - Text animation library for SwiftUI

⚠️ This repository is under construction. SwiftUI Text Animation Library Make yo

null 28 Jan 8, 2023
Emil 3 Jan 31, 2022
SwiftUI iOS application allowing users to create profiles and meet and chat with people

FindR SwiftUI iOS application allowing users to create profiles and meet and cha

Devang Papinwar 3 Dec 7, 2022
Match animations in SwiftUI and UIKit/AppKit

MatchedAnimation Match animations in SwiftUI and UIKit/AppKit. /// Draw a box in

Ryan Carver 6 Dec 21, 2022
Tools for SwiftUI that helps perform Path and Shape animations, such us morphing circle or shape transformations

SwiftUI+PathAnimations ?? Introduction This packages contains SimilarShape and InterpolatedShape, both can be used to achieve shapes animations with S

Alfredo Delli Bovi 180 Dec 29, 2022
A framework and generator for displaying SwiftUI component libraries

Exhibition Exhibition is a framework and generator for displaying a SwiftUI component library. Inspired by Storybook and Showkase Installation Swift P

Malcolm Jarvis 23 Dec 30, 2022
WordleDemo - A SwiftUI implementation of Wordle's core logic and UI

WordleDemo Just a SwiftUI implementation of Wordle's core logic and UI Contains

null 0 Feb 13, 2022
Easily pause and resume SwiftUI animations

swiftui-pausable-animation Easily pause and resume SwiftUI animations! Installation This component is distributed as a Swift package. Just add this re

Gordan Glavaš 6 Nov 24, 2022