Multilingual iOS App

How can I create a string for multiple languages?

I want this rule for more languages.

localNotification.alertBody = "You have received \(Int(msg)) new messages"

I want it:

U_Have = "You Received"; (In English)

U_Have = "U heeft"; (Dutch)

New_msg = "new messages"; (In English)

New_msg = "nieuwe berichten"; (Dutch)

I think it will look like this:

localNotification.alertBody = "U_Have \(Int(msg)) New_msg"

But how can I make this possible with Swift for iOS?

+4
source share
2 answers

What you are trying to do - translating words, not whole phrases - is bad in some languages.

. , , , .

+2

:

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)
}

, , , , :

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")

, , :

+1

Source: https://habr.com/ru/post/1625507/


All Articles