How to use genstrings across multiple directories?

XcodeLocalization

Xcode Problem Overview


I have an directory, which has a lot of subdirectories. those subdirs sometimes even have subdirs. there are source files inside.

How could I use genstrings to go across all these dirs and subdirs?

Let's say I cd to my root dir in Terminal, and then I would type this:

genstrings -o en.lproj *.m

How could I tell it now to look into all these directories? Or would I have to add a lot of relative paths comma separated? how?

Xcode Solutions


Solution 1 - Xcode

One method would be:

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

xargs is a nice chunk of shell-foo. It will take strings on standard in and convert them into arguments for the next function. This will populate your genstrings command with every .m file beneath the current directory.

This answer handels spaces in the used path so it is more robust. You should use it to avoid skipping files when processing your source files.

Edit: as said in the comments and in other answers, *.m should be quoted.

Solution 2 - Xcode

I don't know exactly why, but Brian's command didn't work for me. This did:

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

EDIT: Well, when I originally wrote this I was in a hurry and just needed something that worked. The issue that was occurring for me when using the accepted answer above was that "*.m" has to be quoted (and the curious can find an explanation as to why this is the case in the comments on Brian King's answer). I think that the best solution is to use that original answer with the appropriate bit quoted, which would then read:

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

I'm leaving my original reply intact above though, in case it still helps anybody for whatever reason.

Solution 3 - Xcode

This works for me:

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

Thanks to Brian and Uberhamster.

Solution 4 - Xcode

Not sure if anyone noticed it or the option came later on, but now there is an -a option is genstrings. None of the above options worked for me. Below is my solution.

find ./ -name "*.m" -exec echo {} \; -exec genstrings -a -o en.lproj {} \;

This will also print the name of the files read.

Though above command works fine, it was not exactly for me, because in my project folder there are many files which are lying in folder but not included in xcode project. So what I did was, created a list of files used in my project by parsing pbxproj file. Added the list in filelist.txt, and fired below command

while read f; do find ./ -name "$f" -exec echo {} \; -exec genstrings -a -o en.lproj {} \; ; done < filelist.txt

Solution 5 - Xcode

Since no one has posted a solution with both Objective-C and Swift, I though I'd share how I do it.

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

By using this your localizable strings file will be replaced with strings from both the swift and objective-c files.

Edit: You need to cd to the directory with all your subdirectories.

Solution 6 - Xcode

Swift

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

If you need the file in same folder

find ./ -name \*.swift -print0 | xargs -0 genstrings -o .

Solution 7 - Xcode

I just added another path to the genstrings command, like this:

genstrings -o en.lproj *.m Classes/*.m

..and it worked out fine!

Solution 8 - Xcode

I a have a lot of code in .mm files so i have to use:

find . -name \*.m -or -name \*.mm | xargs genstrings

or other variants, such as

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

Solution 9 - Xcode

The following improves on the earlier answers, this will find both .h and .m files:

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

Solution 10 - Xcode

I had problems generating the strings file for a folder structure where some of the folders had spaces in their names.

I found a nice solution on this site: <http://riveroften.com/generate-localizable-strings-file-with-genstrings/>

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

Solution 11 - Xcode

To scan all the .m files: in root folder run this

genstrings ./**/*.m

Solution 12 - Xcode

Try this

genstrings -o English.lproj ./Classes/*.m ./Classes/*.h ./Classes/subclass/*.m

If subfolders aren't too much, this will work perfectly.

Solution 13 - Xcode

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

This will populate your genstrings command with every .m file to the current directory. Than create the NSLocalizedString() "key/context" in the Localizable.string file, which can be used any where in the project.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondontWatchMyProfileView Question on Stackoverflow
Solution 1 - XcodeBrian KingView Answer on Stackoverflow
Solution 2 - XcodeJonathan ZhanView Answer on Stackoverflow
Solution 3 - XcodeSEGView Answer on Stackoverflow
Solution 4 - XcodeJOA80View Answer on Stackoverflow
Solution 5 - XcodeMattView Answer on Stackoverflow
Solution 6 - Xcodeanoop4realView Answer on Stackoverflow
Solution 7 - XcodeManiView Answer on Stackoverflow
Solution 8 - XcodeRok JarcView Answer on Stackoverflow
Solution 9 - XcodecbartelView Answer on Stackoverflow
Solution 10 - XcoderalizView Answer on Stackoverflow
Solution 11 - XcodeAlan NguyenView Answer on Stackoverflow
Solution 12 - XcodepuchikonView Answer on Stackoverflow
Solution 13 - Xcodesudhakkar kView Answer on Stackoverflow