GithubHelp home page GithubHelp logo

kayak / swiftgenstrings Goto Github PK

View Code? Open in Web Editor NEW
38.0 11.0 6.0 60 KB

genstrings replacement for Swift that actually works

License: Apache License 2.0

Swift 99.42% Makefile 0.58%
genstrings apple swift localization nslocalizedstring ios watchos tvos macos xcode

swiftgenstrings's Introduction

SwiftGenStrings

SwiftGenStrings is a command line application that can be used as a drop-in replacement for the standard genstrings command for Swift sources. The latter only supports the short form of the NSLocalizedString function but breaks as soon as you use any parameters other than key and comment as in

NSLocalizedString("DATE_RANGE", value: "%@ – %@", comment: "A range of dates")

The upstream issue is tracked here.

Usage

USAGE: SwiftGenStrings <files> ... [-s <s>] [-o <o>] [--exclude-comments]

ARGUMENTS:
  <files>                 List of files, that are used as source of
                          Localizable.strings generation.

OPTIONS:
  -s <s>                  (Optional) Substitute for NSLocalizedString, useful when different macro is used.
  -o <o>                  (Optional) Specifies what directory Localizable.strings table is created in. Not specifying output directory will print script output
                          content to standard output (console).
  -e, --exclude-comments  (Optional) Formatted output does not include comments
  --version               Show the version.
  -h, --help              Show help information.

To gather strings in current directory, run:

$ find . -name "*.swift" | xargs SwiftGenStrings

If you have any 3rd party code like CocoaPods or Carthage in your project directory, you might want to exclude it from localization. To do that, run the following:

$ find . \( -name "*.swift" ! -path "./Carthage/*" ! -path "./Pods/*" \) | xargs SwiftGenStrings

Installation

The quickest and easiest way to install SwiftGenStrings is via Mint

$ mint install kayak/SwiftGenStrings

Prebuilt Binaries

We tag releases and upload prebuilt binaries to GitHub. Checkout the releases tab or go straight to the latest release.

From Git

The project provides a Makefile. To export a binary run:

$ make release

The exported binary can be found under Products/SwiftGenStrings. Alternatively you can use make install to install the compiled library directly into /usr/local/bin/SwiftGenStrings

Testing

Since SwiftGenStrings is a SPM package, running tests is easy:

$ swift test

Requirements

  • Xcode 12
  • Swift 5.3

Limitations

  • SwiftGenStrings currently doesn't support multiple tables, only the default one - Localizable.strings.
  • It is not possible to use NSLocalizedString in string interpolation e.g.: let hello = "--- \(NSLocalizedString("Hello world!", comment: ""))" will not pickup the localized string.

swiftgenstrings's People

Contributors

henrik-dmg avatar johennes avatar juraj-blahunka avatar sirnacnud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftgenstrings's Issues

Fatal error

find . -name "*.swift" | xargs SwiftGenStrings

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=260 "The file “Supporting” couldn’t be opened because there is no such file." UserInfo={NSFilePath=./Pods/Log/Source/Supporting, NSUnderlyingError=0x7fd7c5007b80 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-902.0.48/src/swift/stdlib/public/core/ErrorType.swift, line 184

Support String extension for localization

Hi,

I defined this extension for localizing strings, How can I get strings file?

extension String {
    func localized(_ args: CVarArg..., comment: String = "") -> String {
        if args.isEmpty {
            return NSLocalizedString(self, comment: comment)
        } else {
            let format = NSLocalizedString(self, comment: comment)
            return String(format: format, arguments: args)
        }
    }
}

"Hello world!".localized()
let name = "Amir"
"Hello %@".localized(name, comment: "Say hello")

Add ability to parse LocalizedStringKey

The tool should be able to parse LocalizedStringKey, which is used for SwiftUI localisation.
We could either do this with a flag for example (whether you want to parse it as well or not)

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.