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 (String
s) 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!