Swift 中使用函数代替宏进行本地化字符串获取
在 Swift 中,你可以使用以下代码将宏转换为函数:
func LString(_ key: String) -> String {
let language = UserDefaults.standard.string(forKey: 'AppLanguage') ?? 'en' // 替换为你存储语言设置的键名
guard let path = Bundle.main.path(forResource: language, ofType: 'lproj') else {
return ''
}
guard let bundle = Bundle(path: path) else {
return ''
}
return bundle.localizedString(forKey: key, value: '', table: nil)
}
然后你可以在代码中使用LString函数来获取本地化字符串,例如:
let localizedString = LString('Hello')
原文地址: https://www.cveoy.top/t/topic/b4N7 著作权归作者所有。请勿转载和采集!