NSLocalizedString returns text that is defined nowhere - ios

OK, I am officially flabbergasted. I started an IOS project some time ago, and juggled around with localization a bit at the start, which became a bit of a mess, because I did not know how to do that properly yet.
Now I have decided to do the localization from scratch, and therefore threw away all the .strings files, and created a proper multi-language structure. I started with empty Localizable.strings files, and run the app to see whether I cleaned up everything properly. With empty Localizable.string files, I assumed my "NSLocalizedString" calls would simply return the key as text. They don't. They still return the old text that I had in the old .strings files.
Just to be sure, I put an NSLog statement under one of the NSLocalizedString calls, as such:
NSString *text = NSLocalizedString( key, nil );
NSLog(#"key=%# text=%#", key, text);
Then I search my whole harddisk for the text that was returned. No file on my harddisk contains the string that NSLocalizedString returns. And it is a completely different string than the key, so it cannot be constructed by NSLocalizedString either.
Anybody knows how this can happen? Is the old info cached somewhere by XCode? How can I persuade the tool to use the new Localizable.strings files? Obviously, I have already 'clean'ed and rebuilt the whole project.

Try to delete the app from the phone, then clean your project and install the app afresh.
If it still doesn't work, check you haven't somehow messed up with the Build Rules (Xcode 4 -> select your target -> tab "Build Rules") There, check if you don't have too many "CopyStringsFile" rules that aren't using "CopyStringsFile". If so you can remove them all but one.

If you are testing on the simulator, try resetting the simulator using the "Reset content and settings..." option in the "iOS Simulator" application menu.
When you redeploy a new build of the app, even after a clean build, it often doesn't clear out the cached content of the previously built version.

Did you also clean build folder? command + option + shift + k or go to product, press option, there will be a clean build folder, usually, resources file are cached, any resources file replacement (file with same file name but different object) might not be detected by XCode
Also, deleting app / resetting simulator does help. Deleted files sometime don't get removed when you redeploy.

Related

When I change branch some localized keys doesn't get translated unless I recompile the project

I've been dealing with localization on several projects but it is the first time this happens to me.
When I compile my project on a new branch, some keys are not translated although everything is right (key exists, have a value and localize function is properly called).
I first though it was about some local cache but it also happens on my colleagues Mac.
The problem disappears after recompiling the project.
When I print the used Bundle this is what I get :
NSBundle </Users/myname/Library/Developer/CoreSimulator/Devices/0BBE6113-F013-4E0C-A47A-C40C47A5AD58/data/Containers/Bundle/Application/9FE75EF1-EA8C-4DDA-839B-D012EDD3C3E7/My App.app> (loaded)
I'm kind of worried about this bug being shipped to the Store when I will archive my next version.

Xcode 9.2 crashes exporting or importing xliff

I can see that this is a recurring problem with all sorts of Xcode versions. I'm using the latest non-beta build (9.2 9C40b).
I have already localized in Spanish. Doing that involved successful exports and imports of xliff files, with same Xcode. So what changed?
I am now trying to localize to another language. This is a showstopper. Any hints? I have looked through all the posts about previous versions crashing, and have not found anything that works.
(And before you mention it, I am done and over with genstrings. If it exists anymore.)
I've discovered that large .xliff files (more than about 6,500 lines or 1,200 <trans-unit> elements) cause Xcode 10.1 to crash consistently near the end of an import operation.
The work-around is to manually split the .xliff file into two distinct .xliff files, each containing a subset of the <file> elements in the original file. You can then import the two resulting .xliff files into Xcode separately without crashing.
Do not translate "bundle name"
I wanted to translate to Greek Language and every time I was importing xliff files, Xcode was crashing.
I realized that the problem was, that I was also translating "bundle name".
If you did the same mistake, open xliff file, find:
<trans-unit id="CFBundleName">
<source>NameOfYourApp</source>
and delete the line
<target>...</target>
After that everything worked perfect for me!
Here's a workaround. Select your project icon in the Project Navigator. Select Info at top (as opposed to Build Settings). Under Localizations, click the + button and add your new language.
Xcode will create the new .strings files and fill them up with pre-existing translations. (If there are any.) Strangely, for me, some of the new .strings files were filled in with English, some with Spanish.
In any case, you have the new .strings files and can manually paste in the translations for the new language. If your app doesn't have too much user-facing text, this isn't onerous.
But really, with this problem going back to Xcode 4 (!) one would think it would be fixed by now.
it's a late answer I know, and I'm using Xcode Version 10.1 (10B61) already.
In my scenario, I added Chinese Hong Kong (zh-HK) language under Project > Info > Localization and exported successfully then sent to the agency for translations. After few days they sent to me Chinese translated text within. At that point, everything seems correct for me by eye. But I've tried 10 times to import file and it crashes 10 times without exception! Which makes me sick. I use to checked to see the difference between 2 document via Apple's File Merge App. I understand that agency geniuses added few missing <\target> tags. Hmmm, that means while exporting Xcode didn't add for some translations <\target> tag. Interesting...
I removed those unnecessarily translated tags and checked everything was equal in 2 documents except translations of the words to the Chinese language.
Tried to import again. Then BOOM! It works like a charm! I was surprised by this. It's so weird that Xcode never shows me a descriptive message about my mistake. Probably, It's just a parse error for Xcode.
Whatever, I hope this will work for someone else.
Best.

Cannot open Xcode project on a new Mac

I think I have made a bit of a mistake here.
I designed an app a few months ago and even got it in the App Store. Since then I have purchased a new mac. I copied my Xcode project file across to my new Mac.
I've just come back to it for the first time in months and nothing opens. I sometimes get a cannot be parsed warning. Even looking at the file sizes I think there are all just empty files. When I try and open them in TextEdit they are just empty.
Any ideas anyone, I have a particular .swift file that I really need to open.
Check if the files are empty on your old Mac. If they are empty on your old Mac then the data has been lost, there is nothing you can do.
If the files are correct on your old Mac then something went wrong when you transferred the data. Therefore just try copying them over again.
If the first situation that I stated is true then there is nothing you can do.
Can you not retrieve the original files from your other Mac?
copy and paste
Would imply that you can. Maybe you should attempt to copy the files again, alternatively, if you just need the one .swift file you could probably copy the text from it and put it into plaintext and then copy that back out into a new xcode project if xcode is having such trouble opening that particular file.
Are you sure that you're not missing out some details? Seems almost too easy to answer this one.

Xcode project not opening in Xcode 8 - cannot be parsed [duplicate]

I think I have made a bit of a mistake here.
I designed an app a few months ago and even got it in the App Store. Since then I have purchased a new mac. I copied my Xcode project file across to my new Mac.
I've just come back to it for the first time in months and nothing opens. I sometimes get a cannot be parsed warning. Even looking at the file sizes I think there are all just empty files. When I try and open them in TextEdit they are just empty.
Any ideas anyone, I have a particular .swift file that I really need to open.
Check if the files are empty on your old Mac. If they are empty on your old Mac then the data has been lost, there is nothing you can do.
If the files are correct on your old Mac then something went wrong when you transferred the data. Therefore just try copying them over again.
If the first situation that I stated is true then there is nothing you can do.
Can you not retrieve the original files from your other Mac?
copy and paste
Would imply that you can. Maybe you should attempt to copy the files again, alternatively, if you just need the one .swift file you could probably copy the text from it and put it into plaintext and then copy that back out into a new xcode project if xcode is having such trouble opening that particular file.
Are you sure that you're not missing out some details? Seems almost too easy to answer this one.

Xcode crashed while renaming project - whole project broken

I wanted to release an app I worked on and quickly change the project name. I usually duplicate projects before doing things like this but this time - because this procedure always worked on Xcode as I found it way more reliable than e.g. Eclipse - I didn't, which I immediately regretted.
Opened Xcode again and now I am seeing this
I guess all the linking is gone now which is why Xcode doesn't "find" the files anymore. Renaming the remaining project stem to the old name gives
couldn’t be moved to because an item with the
same name already exists
and also manual renaming of folders won't work as the .xcodeproj is gone.
The project is written in Objective-C, and all the classes are still persistent, but the effort of adding all the xib's, referencing the outlets, setting architectures, etc will become the nightmare of my life. I do have a half-way recent backup but everything I did to make the app store-ready today will be gone.
Any ideas on how to rescue my project?
EDIT: What Xcode is now showing on the welcome screen is a project called "project" (literally) and it has the usual compass icon but with a white instead of a blue background referring to the path <project folder>/<new project name>/ and below that "white" project there is a folder icon with the new name pointing to <project folder>.
I also made a snapshot before, of course it now says "Unable to read snapshots" in the "restore from snapshots" window.
Ok what I now did is renaming ALL references from the old project name to the new one. I even changed file names. I used Xcode's CMD+SHIFT+F and TextWrangler's "replace" function and thankfully I still had the .xcodeproj from yesterday's backup so from there I could copy it into the new project and rename stuff. There are still some things that don't work perfectly, for example auto layout doesn't "stretch" contents over the screen (it stays on iPhone 4s size, even on 6 plus simulator), I needed to set the scheme again and Launch Images / Icons got lost but I can look over that. The only thing I am afraid of now is that the final build might miss out a symbol or something like that so I eventually have a corrupt file in the App Store, but man it's a beta version, how much worse can it get anyway?
Thanks to everyone recommending me to even backup the broken project, I did this even before you said it but because I think it's a valuable advice I wanted to put it in my answer as well.
It's a shame that stuff like this can happen, I've been backing up my work on a daily basis and my project never got corrupted - until now.

Resources