BaSha
BaSha

Reputation: 2406

Issue with genstrings for Swift file

genstrings works well to extract localizable content from .m file as,

find . -name \*.m | xargs genstrings -o en.lproj

But, not working for .swift file as,

find . -name \*.swift | xargs genstrings -o en.lproj

Upvotes: 25

Views: 11815

Answers (7)

ahbou
ahbou

Reputation: 4918

Xcode now includes a powerful tool for extracting localizations.

Just select your project on the left then Editor menu >> Export localizations.

You'll get a folder with all the text in your files as well as the Localizable.strings and InfoPlist.strings

More details here: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html

Upvotes: 0

Christian Gossain
Christian Gossain

Reputation: 5972

The issue I was having with find/genstrings was twofold:

  1. When it reached folder names with spaces (generated by the output of find), it would exit with an error
  2. When it reached the file where I had my custom routine defined, it was giving me an error when trying to parse my actual function definition

To fix both those problems I'm using the following:

find Some/Path/ \( -name "*.swift" ! -name "MyExcludedFile.swift" \) | sed "s/^/'/;s/$/'/" | xargs genstrings -o . -s MyCustomLocalizedStringRoutine

To summarize, we use the find command to both find and exclude your Swift files, then pipe the results into the sed command which will wrap each file path in quotes, then finally pipe that result into the genstrings command

Upvotes: 0

Raiden Yamato
Raiden Yamato

Reputation: 9

There is a similar question here: How to use genstrings across multiple directories?

find ./ -name "*.m" -print0 | xargs -0 genstrings -o en.lproj

Upvotes: 0

m_katsifarakis
m_katsifarakis

Reputation: 1907

I believe genstrings works as intended, however Apple's xargs approach to generate strings from all your project's files is flawed and does not properly parse paths containing spaces.

That might be the reason why it's not working for you.

Try using the following:

find . -name \*.swift | tr '\n' '\0' | xargs -0 genstrings -o .

Upvotes: 5

Juraj Blahunka
Juraj Blahunka

Reputation: 18523

There's an alternative tool called SwiftGenStrings

Hello.swift

NSLocalizedString("hello", value: "world", comment: "Hi!")

SwiftGenStrings:

$ SwiftGenStrings Hello.swift 

/* Hi! */
"hello" = "world";

Apple genstrings:

$ genstrings Hello.swift

Bad entry in file Hello.swift (line = 1): Argument is not a literal string.

Disclaimer: I worked on SwiftGenStrings.

Upvotes: 2

Mundi
Mundi

Reputation: 80265

The genstrings tool works fine with swift as far as I am concerned. Here is my test:

// MyClass.swift
let message = NSLocalizedString("This is the test message.", comment: "Test")

then, in the folder with the class

# generate strings for all swift files (even in nested directories)
$ find . -name \*.swift | xargs genstrings -o .

# See results
$ cat Localizable.strings
/* Test */
"This is the test message." = "This is the test message.";
$

Upvotes: 36

philipp
philipp

Reputation: 4183

We wrote a command line tool that works for Swift files and merges the result of apples genstrings tool. It allows for key and value in NSLocalizedString

https://github.com/KeepSafe/genstrings_swift

Upvotes: 4

Related Questions