Localizing in Xcode 4 with Localizable.String - xcode

I just updated to Xcode 4.
Normally when localizing apps we add "Localizable.String" file to the project, and then navigate to "Get Info" menu and click the "Make It Localized" button.
However, in Xcode 4, there is no "Get Info" menu.
When I try to add a language it only effects the "InfoPlist.String" file (which can only localize the app's name).
How may I add localization to my app in Xcode 4?

It's simple once you understand it.
If you want to accomplish this with Xcode 5.x and iOS 7 or Xcode 6.x and iOS 8, check out "How to localize my app with Xcode 5?" instead.
I liked SNR's link, but his answer was a bit short.
Also, I see that this question is a bit old, and my answer may be different from older versions of Xcode. (I used Xcode v. 4.3.3)
However, i have updated my answer to work with both Xcode 4.3.5 and below + 4.4 and above (and Xcode 5.x and 6.x here: How to localize my app with Xcode 5?).
To begin you should add a new "Strings File" in the iOS Resource category.
Next, create the file as "Localizable.strings".
When the file is created, select it and open File Inspector .
EDIT: Things have changed (a bit) with the new Xcode 4.4, 4.5 (and above) (for iOS 6 support).
If you'r Not using Xcode 4.4 or above, joust skip this step.
{ The Xcode 4.4, 4.5 (and above) way:
Click the "Make localized" button
Now head over to your Main Project page.
And click the "+" button under Localization, and select the languages you want to support.
(I'll select German / Deutsch)
Now a window will appear asking you what files you want to localize, make sure Only the "Localizable.strings" file is selected and click Finish.
}
{ The Xcode 4.3.5 and below way:
Click the "+" button under Localization, and select the languages you want to support.
(I'll select German / Deutsch)
}
.
.
.
.
You should now see that you have two files under the "Localizable.strings" file.
Next, add your localization strings inside both of the localization files.
Now here comes the coding part.
Here i'll simply declare a UILabel and set it's text to the Localizable file string.
Declare:
IBOutlet UILabel *testLabel;
And Inside ViewDidLoad i'll set the text using NSLocalizedString:
[testLabel setText:NSLocalizedString(#"TEST", nil)];
To finish up, just connect our testLabel in "Interface Builder".
Now you should be able to run the Project and see the localized string.
And if you change the language, you should see the localized string change as well.

Select the file you want to localize and klick on the file inspector in the utilities section.
There is a section Localization

You can view the file info (and add localizations) when you select the file and open the "File Inspector" via the main menu's "View" -> "Utilities" -> "File Inspector".

first you need to add new Localizable.string file and select it and go to View" -> "Utilities" -> "File Inspector click on + button to add language in localization section.
also check it XCode 4, Adding localization

the way of Localization in xcode4 has changed! right click the localizable.strings, and to add support for another language, simply click on the ‘+’ in that ‘Localization’ pane on the right.

You can take advanced of the User Defined Runtime Attributes:
http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html
First define a new category for UILabel:
#import "UILabel+Localized.h"
#implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
[self setText:NSLocalizedString(aText, nil)];
}
#end
Then in the interface builder, User Defined Runtime Attributes :
textLocalized String your string to localized
And you don't need to declare the IBOutlet,
and don't need code this in the ViewDidLoad [testLabel setText:NSLocalizedString(#"TEST", nil)];

If you want a dynamic way of switching languages inside your app AND using a Pods based solution check out:
https://github.com/nullic/DPLocalizationManager
Install with: pod 'DPLocalization', '~> 1.2'
It supports the same standard localization files but also uses dynamic linking using User Defined runtime attributes and also inside code such as viewDidLoad.
Some examples in the example project:
self.startup.text = DPLocalizedString(#"TITLE", nil);
self.label.autolocalizationKey = #"LABEL_TEXT";
[self.label updateAutolocalizationArguments:#[#"Hello", #1234567890, [NSDate date]]];
self.autolocalizationKey = #"TITLE";
Download the git project that includes example project showing several examples.

Or you could use TraductoPro to automate this process for you and save a lot oftime. It is a Mac app that integrates well with Xcode.

Related

Xcode 13 AppleScript framework can't connect UI to Applescript code

Trying to create an AppleScript executable for MacOS 11.5 using Xcode 13 with AppleScript framework. Right out of the box (no processing on my part), the delegate icon on the IB display does not point to the default AppleScript code. This is clear since the outlet list for the icon claims that the AppDelegate does not have an outlet named theWindow whereas the AppleScript code clearly does. I've been trying for days to do a simple Hello World sort of thing and have been completely unable to connect UI elements with the "delegate", especially action elements. Am I missing some configuration step or is this a Xcode 13 bug? In examples I've seen on YouTube for creating this for this kind of simple thing the normal Xcode storyboard techniques work as expected (e.g. ctrl drag) but none of them used Xcode 13. Any insight is appreciated.screenshot of IB delegate binding
For future reference, this is starting to pop up on various forums, and appears to be a bug.
The normal signatures for creating IB outlets and action handlers is not being recognized by the Interface Editor. Existing projects - including the base Xcode templates - will build normally, although the editor shows warnings that outlets/actions don’t exist.
There isn’t much of a workaround other than creating objects programmatically or going back to an earlier version of Xcode until a fix is issued.
Update:
As mentioned in other answers and comments, the IB outlet and action handler connection bug has been fixed in Xcode 14, but the AppleScript application and Automator action templates are no longer included.
Custom templates can be created (or copied from an earlier version of Xcode from its Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/macOS/Other folder) and placed in a custom templates folder in your user’s Library folder at ~/Library/Developer/Xcode/Templates/. You can name this template folder whatever you want, such as "My Templates", where it will be shown in the template chooser.
Each template contains a TemplateInfo.plist file with various settings for that template - a complete tutorial is beyond the scope of this topic, but the value for the key "Identifier" in the base dictionary can be used to give your template a custom identifier, such as "com.my.cocoaApplicationAppleScript".
I am using 13.4.1 and the bug is still there.
I am wondering its its worth trying version 14 beta to see if it has been resolved
Playing around with this a little, I did find a workaround, though it's not entirely satisfactory. It amounts to editing the xib xml directly. For example, say you have button in the GUI that you'd like to reference in your script. First, create a property in the AppleScript like so:
property myButton : missing value
Then navigate to the xib file in Xcode, right-click on the xib in the file navigator and choose Open as... → Source Code (the default is "interface Builder XIB Document", which you'll want to return to later). This will show you the xml that underlies the graphical representation. First, search through the text to find the button in question. It will look something like:
<window title="Window" ...>
...
<view key="contentView" id="...">
...
<subviews>
<button ... id="xok-ud-pwL">
...
</button>
</subviews>
</view>
</window>
You want to get the id from the button, which in this case is "xok-ud-pwL". Then go back up to the top of the xml and look for the AppDelegate entry, which will look like:
<customObject id="Voe-Tx-rLC" customClass="AppDelegate"/>
You'll want to edit this so that it looks like the following:
<customObject id="Voe-Tx-rLC" customClass="AppDelegate">
<connections>
<outlet property="myButton" destination="xok-ud-pwL" id="gn6-Ea-hra"/>
</connections>
</customObject>
property should be the name of the property in the script
destination should be the id value of the button you want to connect
id should be a random and unique alphanumeric in 3-2-3 format
This will create an outlet connection between the property and the GUI element. It will even appear in the pop-up menu for the appDelegate so that you can reconnect it graphically. However, if you delete the connection in IB, Xcode will delete the entry from the xml, so you'll have to start again from scratch.
Still buggy, but...
The Release Candidate version of Xcode 14 patch it !
"Fixed an issue with outlet and action connections to AppleScript-based AppDelegates. (83373726) (FB9643535)"
But, you can't create a new project.
If you could find this old Template directory from an Xcode version < 14, you could add again the template AppleScript App.
Quit Xcode
Copy source template from version < 14 (I used v12.4):
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/macOS/Other/AppleScrip App.xctemplate
Copy to the destination folder:
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/Other/
Restart Xcode
I think Apple wants to remove AppleScript in next version of MacOS
Tested on Xcode 14 (14A309) 11 Sept 2022!
Regards
Laurent

Adding localizable strings on xcode 10.1

I have a swift app where I now want to add translations for some of the strings used in the app using NSLocalizedString.
In order to do this, I added a file named Localizable.strings to the project and pressed the "localize" button in the file inspector in order to add multiple languages to it. However only english appeared in the localization list, and I seem to be unable to add more.
Having looked around at SO and just googling around, many of the articles on how to do this mention that I should add more languages to my project under build settings. But the only setting I can change under Localization is one named "Localized String Macro Names", and there is no option to add languages there. There is also no "Use Base Internationalization" marker to check.
I have also tried to click on my project, then Editor -> Add Localization, but there all the languages are greyed out and I am unable to chose any of them.
I therefore suppose that you are supposed to do this differently in the latest version of xcode (10.1 atm). Can anyone help me figure out how you are supposed to do it?
It's about the project settings. Take a look at the image:
Under the Localizations you add new languages. After you add the language here you select which files you want to localize.
// update
Click on the top right button shown in the screen below and select your project under the PROJECT tab.
You just selected the wrong settings. The picture that you attached is actually under TARGET settings -> Build Settings, rather than project settings.
See the picture below to select "project settings" under xcode 10:
Xcode 10, project settings

Xcode Base Internationalization with no Main.storyboard

I'm trying to follow the tips in Apple's I18N and L10N Guide. I have a pre-existing project from which I have (long ago) deleted the Base.lproj folder. Why? Because I have no Main.storyboard or LaunchScreen.xib. Both of those things are handled programmatically.
However I do have a large number of subsidiary storyboards, including a WatchKit Interface.storyboard. When I click the + in the Project (not target) Localizations section, Xcode presents a dialog that lists only the Interface.storyboard file. Not any of the many others.
How can I persuade Xcode to help me localize the other storyboards? Can I do this all manually? As usual, I am sure it is my mental model that needs refinement.
This is an example where configuration yields convention. That is, if you customize your folder hierarchy, Xcode can adapt and implement its naming conventions.
Select storyboard file in left pane (Project navigator)
Click Doc icon (File inspector) in right pane
Click Localize...
This will create a new Base.proj folder inside whatever folder holds the storyboard. If you are like me, you have done lots of folder-factoring. Xcode goes along with this.
Then you go back to the Project Localizations section and click + to add locales. Xcode creates extracts the strings from your storyboard and creates new folders for the corresponding .strings files.

How to add localization language to Xcode 4.4?

In reference to this guide:
How to localize my app with Xcode 4?
I noticed that under Localization, my Xcode 4.4 shows no [+] button:
How can I add languages to the Localization list?
Note: I'm not using IB. I purely need a localized strings file with translated strings for a game.
My workaround so far: I created the de.lproj folders manually, copied the strings file into de.lproj, added it to Xcode and Xcode did recognize the file as german. Xcode also added German to the Localization list. I'm wondering if there is an easier way to do this (from within Xcode) instead of copying & adding the files manually?
Select your project from the project navigator to open the project editor. Select your project from the left side of the project editor. Click the Info button at the top of the editor. You should see a list of localizations along with a button to add a localization. Click the Add button and choose a language. A sheet will open that lets you choose what files to localize.

Ignore "Attribute Unavailable"-Warning in Xcode4

I'm using a lot of "custom identifiers" for Toolbar Items, which is fine in Xcode4, but it gives me a bunch of warnings when I build the project:
Attribute Unavailable
Custom Identifiers in Interface Builder versions prior to 3.2
Is there a way to ignore these warnings in Xcode4? It's pretty messy when I search for "real" warnings and errors.
Click on your xib file. On the properties you will see the following section:
.
Where it says "Development" change that to Interface Builder 3.2 and you should no longer receive those warnings.
UPDATE for current iOS Xcode users:
In iOS 4.3 - IB is built into the IDE, and on the right after selecting the xib file there will be some icons, the first icon is called "File Inspector" (looks like a page with a fold in it) that is where the section resides (see below).
I am running Xcode 4.2 build 4C199.
Click on xib file,
on the right, click on Utilies View
Click File Inspector. As tony.stack mentioned look under "interface BuilderDocument"
set Deployment = "Project SDK version"
set Development = " xcode 4.2"
Even if you are running xcode 4.2, the Development is set to default 4.1, So you get that warning.
Have you tried looking at the "Document Info" window in Interface Builder? You can set supported versions there.

Resources