How to localize label strings in iOS for a beginner?

前端 未结 2 647
青春惊慌失措
青春惊慌失措 2020-12-10 07:58

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
相关标签:
2条回答
  • 2020-12-10 08:40

    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
    0 讨论(0)
  • 2020-12-10 08:45

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题