Hi I\'m very much stuck on the process of localization on iOS.
Here\'s what I do understand:
For localization, you need to do:
NSLocalizedString(yourKey, nil);
or
[[NSBundle mainBundle] localizedStringForKey:(yourKey) value:@"" table:nil]
For getting the localized string of passed key.
Tutorials:
Making it pretty
For Swift, there is better solution which makes your localizations little better - not that messy. You can use following extension:
extension String {
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
}
func localizedWithComment(comment:String) -> String {
return NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: comment)
}
}
Now, whenever you want something localized, you can just request it as string property:
let text = "my-localization-key".localized
And it will get localization string based on your current system language / locale.
Few tips
If you have bigger project, it is worth looking into managers, that handle the localization export / adding new languages for you. I for example use Crowdin - but you might find some services more appealing. Great thing is that you don't have to do everything by hand, you just insert keys / translations and export is done for you, for all the languages that you have.
Also, follow good naming of your keys. Based on what I've seen people do / my experience, I think it is wise to name keys by logical component / module, where they belong, for example:
"Profile.NavigationBar.Title" = "Profile";
"Profile.NavigationBar.Button.Edit" = "Edit;
This way, you can immediately see what and where you would find it.
Lastly, the localizations in general are huge pain because of number of mistakes that you can do. There is generator that can create actual code out of your localizations, so compiler will check it for you. You can get it on GitHub.
Good luck!
Update for Swift 4+, which has changed bundle-access syntax:
extension String
{
var localized: String
{
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
func localizedWithComment(comment:String) -> String
{
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: comment)
}
}