1
votes

How can I set ios application supported languages? e.g I use NSDate to get current day. If the device language is other than my supported languages NSDateFormatter returns "day" in device's language but I want to get in English if I don't support that language.

I know there is a way to get day in specific language using NSLocal but I don't want to do that way because I need to convert other strings as well.

1

1 Answers

0
votes

The Apple documentation covers this pretty clearly. I know all you need is the word "day", but the following will help you include any word for any language if you do as follows:

1) You need to place all of the words (Strings) in your application into a single .swift file. Each word should be returned in a function that converts this string into the localized string per the device's NSLocal set in the device settings:

struct Localization {
    static let all: String = {
        return getLocalized("All")
    }()
    static let allMedia: String = {
        return getLocalized("All Media")
    }()
    static let back: String = {
        return getLocalized("Back")
    }()

    // ...and do this for each string

}

2) This file should also contain a static function that will convert the string:

static func getLocalized(_ string: String) -> String {
    return NSLocalizedString(string, comment: "")
}

Here, the NSLocalizedString( method will do all of the heavy lifting for you. If will look into the .XLIFF file (we will get to that) in your project and grab the correct string per the device NSLocale. This method also includes a "comment" to tell the language translator what to do with the "string" parameter you passed along with it.

3) Reviewing all of the strings that you placed in your .swift file, you need to include each of those into an .XLIFF file. This is the file that a language expert will need to go over and include the proper translated word per string in the .XLIFF. As I stated before, this is the file that once included inside your project, the NSLocalizedString( method will search this file and grab the correct translated string for you.

And that's it!