Hi I'm very much stuck on the process of localization on iOS.
Here's what I do understand:
However the app, which has a simple label, with the writing "hello world" when compiled doesn't change to Chinese in a Chinese simulator.
Here's where I think I'm going wrong:
I think I need to connect the strings on the screen to the strings contained within my localized strings file.
And do I make a separate string file for each language?
I'm looking to make a very very simple stub of an app containing nothing but a few labels which are translated into different languages, so I can learn how to make it connect and work so I can use it else where.
There is a storyboard for Chinese, do I have to do any work to that or is it left blank?
After hours of reading documents I seem to be going around in circles, I'm not impressed with Apples documentation on this issue think its missing something.
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With