NSLocalizedString with swift variable

后端 未结 6 986
清歌不尽
清歌不尽 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:15

    Here is an extension I use in String, it adds a localizeWithFormat function with variable arguments,

    extension String:{
    
         func localizeWithFormat(arguments: CVarArg...) -> String{
            return String(format: self.localized, arguments: arguments)        
         }
    
         var localized: String{
             return Bundle.main.localizedString(forKey: self, value: nil, table: "StandardLocalizations")
         }
    }
    

    Usage:

    let siriCalendarText = "AnyCalendar"
    let localizedText = "LTo use Siri with my app, please set %@ as the default list on your device reminders settings".localizeWithFormat(arguments: siriCalendarTitle)
    

    Just be careful not to use the same function and property names that String has. I normally use a 3 letter prefix for all my framework functions.

提交回复
热议问题