Im doing my first localized project and I've been fighting with it for several hours with no luck.
I have to create an app that, based on the user selection, shows texts and images in different languages.
I've read most of Apple's documents on the matter but I cant make a simple example work.
This are my steps so far:
1) Create a new project.
2) Manually create a "en.lproj" directory in the projects folder.
3) Using TexEdit create file called "Localizable.strings" and store it in Unicode UTF-16. The file looks like this:
/*
Localizable.strings
Multilanguage02
Created by Gonzalo Floria on 5/6/10.
Copyright 2010 __MyCompanyName__. All rights reserved.
*/
"Hello" = "Hi";
"Goodbye" = "Bye";
4) I drag this file to the Resources Folder on XCode and it appear with the "subdir" "en" underneath it (with the dropdown triangle to the left). If I try to see it on XCode it looks all wrong, whit lots of ? symbols, but Im guessing thats because its a UTF-16 file. Right?
5) Now on my view did load I can access this strings like this:
NSString *translated;
translated = NSLocalizedString(#"Hello", #"User greetings");
NSLog(#"Translated text is %#",translated);
My problem is allowing the user to switch language. I have create an es.lproj with the Localizable.strings file (in Spanish), but I CANT access it.
I've tried this line:
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"es", nil] forKey:#"AppleLanguages"];
But that only works the NEXT time you load the application. Is there no way to allow the user to switch languages while running the application??
Do I have to implement my own Dictionary files and forget all about NSLocalizableString family?
Thanks for ANY advice or pointers.
Gonso
There is already a discussion about that here.
Their suggestion is to create a sub-bundle and then use a method called NSLocalizedStringFromTableInBundle(...) (as described in the reference manual) to
get a localized string from a specific table in a bundle.
I am just giving you an hunch, I haven't tried but, I guess, this could be a good way to face your problem.
Related
I use Done button in my custom view and I'd like to make its title localizable. I know that system Done button from UINavigationBar is already localized and it would be perfect to get it's localized strings somehow. Is there a way to do this?
Using the whole UINavigationBar only because of localized Done button seems to be inappropriate.
Clarification: the point is to use the same localized strings, that system uses.
There isn't an official way to get standard strings from the OS, but it's highly likely that all the strings will be in localized strings files, and most of those are present on the simulator (it won't have strings from apps that aren't present on the simulator, but should have almost all the strings from frameworks).
The simulator's framework directory is relative to your Xcode install directory and something like this (typing it from memory, change the version as appropriate):
Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/
To start with, I'd look in UIKit.framework/en.lproj/*.strings (or English.lproj or en_US.lproj). Strings files are plists and will generally be binary plists on device (such files start with "bplist00"). There are various ways to open these: TextWrangler will automatically display plists as XML, and the command-line tool plutil will convert to other formats. I use the bash alias
alias plist-dump='plutil -convert xml1 -o /dev/stdout --'
When you've found the string, you have a couple of options:
Load it directly from the framework at runtime with something like
[[NSBundle bundleWithIdentifier:...] localizedStringForKey:#"DONE" value:nil table:...]
This is not recommended. Yhere's no guarantee that it will work on a different OS version. (In practice, a string like "Done" probably won't move, but the alternative is easy enough that it's not worth the fuss.)
Copy the strings into your project. I don't know of a tool to automatically do this and merge with your existing Localizable.strings, but it probably exists somewhere and would not be too difficult to write. Slightly legally dubious (but not much more so than copying strings by eye for the languages you care about); I would definitely avoid doing it on a prerelease SDK to avoid concerns about Apple's NDA.
Copy the whole strings file into e.g. UIKit.strings and use something like NSLocalizedStringFromTable(#"DONE",#"UIKit",nil). Likely to be a copyright infringement!
In practice, I think Apple is unlikely to care about copying a handful of strings/images from iOS into an iOS app. Copying them into an Android app is another matter entirely...
Just add needed localization in project settings, all system stuff like done button will be localized by device locale if you added it, else it willbe on default language which you also should set up.
For custom button:
[self.myButton setTitle:NSLocalizedString(#"my localizable title", #"") forState:UIControlStateNormal];
It also valid for any NSString in your project.
NSString *someText = NSLocalizedString(#"my localizable title", #"");
self.myLabel.text = someText;
You should make your app localizable anyway by adding needed localizations.
After that just follow this instructions:
http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
But I really recommend to you to do this in the end of project.
I've been trying to add a Portuguese translation to my app. At the moment it's all in English. (well, it started that way).
I'm using a Storyboard for the main part of the UI. There are also a couple of additional xib files for reusable UI in table etc...
Anyway, I set out on the path of trying to internationalise the app and managed to convert the Storyboard to Portuguese.
I then tried to set up some strings (just a couple to begin with) to make translations of those also.
I've now got an storyboard that's only in Portuguese (I lost the English version) and none of the strings are being translated properly anyway.
I've set all the string back to just use #"blah" now (I'd put NSLocalizedString in a couple of places).
So I should be back to square one (once I fix the storyboard).
Anyway, is there a list of steps somewhere of how to go about making an app localised?
Any help appreciated.
There are couple of tutorials available - this is one of the good ones: http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
This will definitely help you to get started real quick!
However, if you are on iOS6 xCode 4.5 you should also check the new features regarding localization. Apple now has also gon the route to have just one XIB (not like before multiple XIBs, one per language - which was impossible to maintain)
Probably the most important point is to always use NSLocalizedString, so there is no need for multiple XIBs. And then it's real easy to just add a new strings file for each language.
All you have to do then is in the Project Editor select your project -> select the Info tab on the right -> scroll all the way down -> there you find the localizations, press the little + on the buttom to add a new language. That's it.
I have my app with english version. I want it to run with french text also.
What steps I need to perform? or what API or extra code I will need?
Any examples or tutorial will help me more.
You need to add localizable strings for all the languages. And also you need to add the .lproj along with the localized file for each language you want to provide the support for.
Hope this helps you.
EDIT:
I have some of these links useful for you.
http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
http://adeem.me/blog/2009/05/09/tutorial-iphone-localization-in-xib-nib-files/
How simplify iPhone localization?
http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
I feel the last one which is from http://www.raywenderlich.com is the best one I would recommend.
Hope this helps you.
Thanks
The tutorials are good for starters but you should consider following points before you start:
"normal" localization where you localize XIB files is not really recommendable when the XIB might change, since you have to maintain multiple XIB files (for each language there will be a standalone XIB) Honestly this becomes a pain after a while
Therefore I suggest (even though it is more work at the beginning) to set the labels and button titles programmatically:
mylabel.text = NSLocalizedString(#"text:", #"text:");
[mybtn setTitle: NSLocalizedString(#"textbtn", #"textbtn") forState:UIControlStateNormal];
Then use: genstrings -o en.lproj *.m to create the strings file which will look for all those NSLocalizedString and create a file Localizable.strings in the folder en.lproj
If you follow this advice it will make live easier for you in the future - though it's not really 100% comfortable yet. If you need to add a new NSLocalizedString (eg because you have a new label) you need to create a completely new file Localizable.strings. Make sure you have a backup copy of this file where you have the translation, otherwise it gets overwritten and lost. I haven't yet come accross a good solution how to build up the strings to be translated...
ps there is no need to add localized XIB files anymore.. otherwise you end up having multiple XIB files which one wants to avoid in the first place...
When an iPhone program has something like a paragraph or a set of instructions that need to be displayed on screen--basically anything that is longer than two sentences, where does it go?
Should it be stored as an NSLocalizedString (is this stored in the info.plist? I tried searching documentation--I'm a beginner and don't quite understand where that goes)?
Should it simply be hard-coded into the UITextView or UILabel that contains it?
Should it be defined as a constant then referenced in the UITextView/UILabel?
Should it be placed in a .txt file then referenced? (http://stackoverflow.com/questions/2506594/where-to-put-text-files-for-iphone-uitextview)
If you are thinking about localizing the text at all, I'd put it into a strings file:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
You could also put it into a plist and read the plist file at runtime from the bundle. Generally try to keep the info.plist clean.
I would put such items in a plist file ad read it in.
i got button....
displays on button
Search...
when i selected localization.....
i need to display
Zoeken
for search button its displays Zoeken...
#All thanks in advance.
You should look at NSLocalizedStringWithDefaultValue:
NSString *buttonTitle = NSLocalizedStringWithDefaultValue(#"KEY", nil, [NSBundle mainBundle], #"VISIBLE_DESCRIPTION", #"DEVELOPER_DESCRIPTION");
[aButton setTitle:buttonTitle forState:UIControlState...];
You'll then need to provide the relevant localization files in your project for the languages you intended on targeting.
As a top level skim, you can create per-locale NIB and string resource files using the built-in internationalisation capabilites.
However, this is quite a broad topic (there's an entire section of the Apple developer site dedicated entirely to internationalisation, complete with sample code, etc.), so what you need to do it read the documents there, look at the sample code and then ask a more targeted question if you get stuck/have a specific issue.