嘗試某事像這樣:
func getSystemLanguage() -> String{
var Language: String = "en"
if let preferredLanguage = NSLocale.preferredLanguages()[0] as String? {
Language = preferredLanguage
if(Language.rangeOfString("en") == nil && Language.rangeOfString("da") == nil){
Language = "en";
}
if Language.rangeOfString("en") != nil {
Language = "en";
}
if(Language.rangeOfString("da") != nil){
Language = "da"
}
}
return Language
}
然後:
if(getSystemLanguage == "da"){
U_Have = "U heeft"; (Dutch)
} else {
U_Have = "You have receive"; (English)
}
我強烈建議你把所有的字符串在一個文件中,並做某事喜歡這樣,因爲如果你不你」就會有很多工作如果U添加一個新的語言:
class LocalizedStrings {
class func getVariable(Variable: String) -> String{
var Key: String = "Key"
switch (Variable) {
case "Test":
if(Logic.getSystemLanguage() == "da"){
Key = "Dutch"
return Key
} else {
Key = "English"
return Key
}
default:
return "NotFound"
}
}
}
然後:
LocalizedString.getVariable("Test")
如果你只是想本地化你的故事板設置字符串就容易多了:
通過使用本地化,而不是通過代碼,你看着的iOS本地化使用Xcode的應用程序?如果推送消息來自服務器,您可能必須在那裏進行本地化。 – luk2302