Giter Site home page Giter Site logo

gaetanomatonti / swift-macro-localized Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 9 KB

A Swift macro to use localized strings in Swift Packages.

License: MIT License

Swift 100.00%
localization localized macro string swift swift-package-manager swiftpackagemanager swiftpm localizedstringresource

swift-macro-localized's Introduction

Localized

A Swift macro to access localized strings in Swift Packages.

Why #Localized?

Swift Packages support resources, such as assets and localized strings. However, these are only accessible from a .module Bundle instance inside the package. This can prove to be tricky when using such resources in SwiftUI views.

Some SwiftUI views, like Text, provide initializers where it's possible to specify the Bundle that contains a specific resource. This parameter defaults to the main app Bundle, meaning you must specify it for every view in your package. It may sound easy, but it's not.

Many views are attached to others as modifiers โ€“ alerts are among those. You can specify a title in the modifier function, but you cannot specify a bundle. Even worse, the APIs do not allow you to directly use a LocalizedStringResource, which does allow you to specify a bundle. So the correct way to approach this problem would be to use the String(localized:) initializer โ€“ which also provides a bundle parameter. This means defining your strings as follows:

.alert(String(localized: "Are you sure you want to delete this item?", bundle: .module), isPresented: $isPresented) {
  ...
}

This can be simplified using the #Localized macro, which expands your LocalizedStringResource into a String(localized:) expression.

.alert(#Localized("Are you sure you want to delete this item?"), isPresented: $isPresented) {
  ...
}

While it's not great that SwiftUI cannot automatically infer the bundle where a view is located, this is the best solution so far to:

  • ensure developers access resources from the correct bundle
  • support localized String interpolation
  • extract used strings with the compiler

Requirements

Please, be mindful that this macro only works in Swift Packages that embed resources, as it uses the Bundle.module accessor synthesized by the package.

swift-macro-localized's People

Contributors

gaetanomatonti avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

kaioelfke

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.