Reputation: 551
How can I make a string for multi languages?
I want this rule for more languages.
localNotification.alertBody = "You have received \(Int(msg)) new messages"
I want this:
U_Have = "You have receive"; (English)
U_Have = "U heeft"; (Dutch)
New_msg = "new messages"; (English)
New_msg = "nieuwe berichten"; (Dutch)
I think it will looks lik this:
localNotification.alertBody = "U_Have \(Int(msg)) New_msg"
But how can I make this possible with Swift for iOS?
Upvotes: 4
Views: 3549
Reputation: 1928
Try sth like this:
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
}
And then:
if(getSystemLanguage == "da"){
U_Have = "U heeft"; (Dutch)
} else {
U_Have = "You have receive"; (English)
}
I highly recommend you to put all strings in one file and do sth like this because if u don't you'll have much work if u add a new language:
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"
}
}
}
And then:
LocalizedString.getVariable("Test")
If you just want to localize strings you set in the storyboard it is much easier:
Upvotes: 1