NSLocalizedString with swift variable

后端 未结 6 1004
清歌不尽
清歌不尽 2020-11-29 02:50

I\'m trying to localize my app using NSLocalizedString. When i import the XLIFF file, most works like a charm but something do not and some string is not localized. I have n

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 03:05

    I created an extension to String since I had many strings to be localized.

    extension String {
        var localized: String {
            return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
        }
    }
    

    For example:

    let myValue = 10
    let anotherValue = "another value"
    
    let localizedStr = "This string is localized: \(myValue) \(anotherValue)".localized
    print(localizedStr)
    

提交回复
热议问题