Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to localize label strings in iOS for a beginner?

Hi I'm very much stuck on the process of localization on iOS.

Here's what I do understand:

  • How to go to the project explorer and set base localizations for different countries
  • How to make a string file and set it to be localized to one of the particular countries chosen within base localization, such as simplified Chinese
  • key value pairs and how to correspond the word "HELLO WORLD" = " some other language"; with in the localized string file

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.

like image 316
Imran Raja Avatar asked May 07 '13 11:05

Imran Raja


2 Answers

For localization, you need to do:

  1. Add different .strings file for each language
  2. Add the Key value for each language file
  3. Use:

NSLocalizedString(yourKey, nil);

or

[[NSBundle mainBundle] localizedStringForKey:(yourKey) value:@"" table:nil]

For getting the localized string of passed key.

Tutorials:

  1. Localization in iOS apps made simple
  2. iPhone Apps Localization Guide
  3. Preparing your iOS app for localization in Xcode
like image 137
Midhun MP Avatar answered Nov 08 '22 09:11

Midhun MP


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)
    }
}
like image 42
Jiri Trecak Avatar answered Nov 08 '22 08:11

Jiri Trecak