Can Xcode find/replace in XIBs? - ios

If I use Xcode's "Find/Replace in workspace" it seems to skip any text contained in the UIViews in XIB files.
Anyway to do this in Xcode or do I need to use another tool?
Thanks!

XCode will successfully Refactor IBOutlet names even if they are connected up in the nib. So to answer you:
Before choosing Find/Replace on the text, first see if Xcode will Refactor it instead. It won't refactor certain things (such as enums and #defines). If it will Refactor your target text then choose that and it should be okay.

You can use find and sed from the command line
find . -name '*.xib' -type f -exec sed -i "" 's/OldText/NewText/g' {} \
For instance, I just had to find and replace all class prefixed from SC to MCSC and I used:
find . -name '*.xib' -type f -exec sed -i "" 's/[[:<:]]SC/MCSC/g' {} \
The [[:<:]] indicates a word boundary on OS X (see https://stackoverflow.com/a/5734237/456366).

Find and replace option of Xcode if for text/string replacement not for files.
If you want to do this you can have your shell script/ apple script.

If you really have a lot of this text you want to find/replace, you might find IBTool usefull.
It's usually used to export/import texts from/into .XIB for translation reasons, but it should fit your needs, too.
You can see example of IBTool usage on iPhone Applications Localization Guide. You'd be interested in points 2 and 5 of the guide.
you export the strings using IBTool
you do find/replace in any text editor
you reimport the string

Related

Finding a file in the User Library of OS X Yosemite

I wish to find a file in the lower levels of the OS X file system (Below where Spotlight searches) by content.
In other words: I know the sting abc12345 os in a text file someplace.
It could be in /Library or perhaps /Users/joe/Library or even /Users/joe/Library/Caches
How does one file the file?
I.m not sure how to use grep (or even if it the appropriate tool for the job)
Humor a newbee to the command line?
In Terminal, type
find $HOME -type f -exec grep "abc12345" {} \+
That says... starting in your HOME directory, find all things that are files (not directories) and look inside them for the string "abc12345" and print anything you find. The \+ at the end says to look in several files in one go, rather than one at a time.
If you want to read all about the find command, or any other command, type find in the Terminal, put your mouse over the word and right click, then left-click Open man Page.

Marking some XIB/Storyboard strings as not localizable

I am using Base Internationalization for XIB/Storyboard files and the "Export for Localization" method using XLIFF files for translators.
I have some labels, buttons, etc. that have text that should be translated, but I also have labels where we use some placeholder text (like a full-name) so you can see what the view would look like when populated with data, but those labels always have their text come from an outlet programmatically.
Is there some way to mark this label's .text property that is set in the XIB as non-localizable so that it doesn't end up in the XLIFF (or resulting .strings) files.
I know that I can remove the text -- I also thought about having a prefix (like #"!DNL!") to mean that the translator shouldn't localize, but I am hoping that there is just a standard way to do this.
I add a note "DNL" to the "Comment for Localizer" field in the identity tab. Then, I run this command to automatically remove all of those elements from the XLIFF:
xmlstarlet ed -d "//*[contains(text(), 'Note = \"DNL\"')]/.." en.xliff > out.xliff
Basically, it's using xmlstarlet (which can be downloaded via homebrew) to find all elements that contain the text Note = "DNL", and then deleting the parent of that element from the XLIFF.
Combined with using xcodebuild -exportLocalizations, you can make a pretty simple script for generating your XLIFFs:
xcodebuild -exportLocalizations -localizationPath build -project ProjectName.xcodeproj
xmlstarlet ed -d "//*[contains(text(), 'Note = \"DNL\"')]/.." build/en.xliff > build/out.xliff
It turns out the localization export from Xcode ignores attributed strings in the storyboard.
So just set the type of text for every label/button you want to exclude to Attributed in the Attributes Inspector.
This will give you an attributed string rather than a plain string, which as far as I know has no implications, apart from the (empty) list of attributes that has to be kept in memory now.
UPDATE:
Check out ReMafoX, it's a Mac app that perfectly solves your problem. It can be easily installed and integrated within your project, watch this video for a detailed walkthrough.
To ignore specific Strings, simply add one of the customizable ignore flags from the "Interface Builder" pane in the "Comment for Localizer" field in your Storyboard/XIB files and the build-script you configured following the above linked video will exclude it on next build of your project (or press of the "Update" button in the config).
OLD ANSWER:
This is now possible using the BartyCrouch command line utility which I recently wrote to solve this problem (see installation instructions in my answer on that thread).
BartyCrouch runs ibtool for you and does additional processing on top of its resulting .strings file. It will exclude views from translation if you include #bc-ignore! into your value or comment within your base internationalized Storyboard/XIB file.
Please check out out the related section within the README on GitHub for detailed information.

flag for no localization in iOS storyboard [duplicate]

I am using Base Internationalization for XIB/Storyboard files and the "Export for Localization" method using XLIFF files for translators.
I have some labels, buttons, etc. that have text that should be translated, but I also have labels where we use some placeholder text (like a full-name) so you can see what the view would look like when populated with data, but those labels always have their text come from an outlet programmatically.
Is there some way to mark this label's .text property that is set in the XIB as non-localizable so that it doesn't end up in the XLIFF (or resulting .strings) files.
I know that I can remove the text -- I also thought about having a prefix (like #"!DNL!") to mean that the translator shouldn't localize, but I am hoping that there is just a standard way to do this.
I add a note "DNL" to the "Comment for Localizer" field in the identity tab. Then, I run this command to automatically remove all of those elements from the XLIFF:
xmlstarlet ed -d "//*[contains(text(), 'Note = \"DNL\"')]/.." en.xliff > out.xliff
Basically, it's using xmlstarlet (which can be downloaded via homebrew) to find all elements that contain the text Note = "DNL", and then deleting the parent of that element from the XLIFF.
Combined with using xcodebuild -exportLocalizations, you can make a pretty simple script for generating your XLIFFs:
xcodebuild -exportLocalizations -localizationPath build -project ProjectName.xcodeproj
xmlstarlet ed -d "//*[contains(text(), 'Note = \"DNL\"')]/.." build/en.xliff > build/out.xliff
It turns out the localization export from Xcode ignores attributed strings in the storyboard.
So just set the type of text for every label/button you want to exclude to Attributed in the Attributes Inspector.
This will give you an attributed string rather than a plain string, which as far as I know has no implications, apart from the (empty) list of attributes that has to be kept in memory now.
UPDATE:
Check out ReMafoX, it's a Mac app that perfectly solves your problem. It can be easily installed and integrated within your project, watch this video for a detailed walkthrough.
To ignore specific Strings, simply add one of the customizable ignore flags from the "Interface Builder" pane in the "Comment for Localizer" field in your Storyboard/XIB files and the build-script you configured following the above linked video will exclude it on next build of your project (or press of the "Update" button in the config).
OLD ANSWER:
This is now possible using the BartyCrouch command line utility which I recently wrote to solve this problem (see installation instructions in my answer on that thread).
BartyCrouch runs ibtool for you and does additional processing on top of its resulting .strings file. It will exclude views from translation if you include #bc-ignore! into your value or comment within your base internationalized Storyboard/XIB file.
Please check out out the related section within the README on GitHub for detailed information.

Is there a way to show custom warnings when building a project in XCode?

Let's say I want to detect something in my code which I cannot check by tests.
e.g.:
- Go over my entire code base to detect there I have a space between brackets and curly brackets (using a regex).
- Go over my entire code base and detect style smells.
etc...
If one of these checks fails, I'd like to show a custom warning I define myself.
Is there anything like this in XCode?
Thanks.
I wrote a run script for Xcode that does exactly what you requested. It searches by regex for predefined "code smells" or "style smells" and warns you at compile time
Read the blog post, but the final script is here
#test if git exists.
command -v git > /dev/null 2>$1 || {exit 0}
#separate code smells with | i.e. (code_smell_1|code_smell_2)
KEYWORDS="(didDeselectRowAtIndexPath)"
git diff --name-only HEAD | grep "\.m" | xargs egrep -s --with-filename --line-number --only-matching "$KEYWORDS.*[^\/]*$" | perl -p -e "s/($KEYWORDS)/ warning: Are you sure you want to use \$1?\nTo remove this warning, append a comment at the end of this line \$1/"
This particular script searches for appearances of didDeselectRowAtIndexPath in the code, but you can change that to any string
You would need to create a project target that runs a script, which searches through your source files and does your checks (this is not trivial) and returns non-zero to indicate error.
Then make your app target dependent on this "check target" and the build will fail if the check fails.
If you want some indication of how you write this "check script" then I would be inclined to employ the clang static analyzer. However, unfortunately, scan-build is not installed as part of the Xcode Command Line tools, so you would need to use the LLVM-binaries in some way, or perhaps the macports version contains it.
For trivial checks, relating to spaces and other stylistic issues rather than real, live, code problems, then you will have to employ some other mechanism, and I have no idea how to start with that, sorry.

Force genstrings to build the Localizable.strings file in order of appearance rather than alphabetically

I'm new to internationalization and localization for iOS. I'm running genstrings:
find . -name \*.m | xargs genstrings -o en.lproj to generate my Localizable.strings files. It builds the file in alphabetical order (by key).
For ease of translation I'd prefer that the keys and values be ordered by their order of appearance in the .m files. Is this possible with genstrings? I couldn't find the relevant info on its man page.
You could do something like:
find . -name '*.m' -print | xargs -n1 genstrings -a
I'm sure there are more elegant ways. Perhaps just use ls *.m instead of the find. The strings are kept together by file with -a switch but they are still sorted within each file.
It is not possible to change the behavior of genstrings other than what it is allowed within the parameters specified in the manual:
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/genstrings.1.html
but if you want to ease the translation you can use Linguan
http://www.cocoanetics.com/apps/linguan/
If you want to stick to genstrings and are having some trouble with it you can try this page, it offers a good explanation:
http://spritebandits.wordpress.com/2012/01/25/ios-iphone-app-localization-genstrings-tips/
But yeah returning to the main question, it is not possible in my knowledge.

Resources