2

I create a rating function which include textview to show the rating status (poor, good, very good, amazing), and with localization (Localizable.strings) English as default and Chinese.

When user submit the rating to server, I want to pass the rating status (get from TextView) in English to server, but when user's change their phone device language to Chinese, the rating status that pass to server will be in Chinese.

How do I keep the current localizable string show in TextView and pass the default English string to server?

YinKiet
  • 479
  • 6
  • 14
  • you can use two variables, one for display the rating to end user and another for storing values which store the value to submit. – Shiv Jaiswal Nov 24 '17 at 06:31
  • now I am doing as the way you that suggested, I'm seeking for solution is there another way to implement so my coding will be clean always – YinKiet Nov 24 '17 at 06:34
  • What @ShivJ won't make your code "unclean", as long as you do it correctly. You need to separate the model from the view. – Sweeper Nov 24 '17 at 06:41
  • Use struct for this. This will keep ur code modularised and clean. – Apogee Nov 24 '17 at 06:42
  • @YinKiet I don't have other solution so I suggested you this one. Yes, it makes code some messy but I don't think that it's a big issue. – Shiv Jaiswal Nov 24 '17 at 06:44

3 Answers3

2
struct Rating {
    static let poor = "Poor"
    static let good = "Good"
    static let veryGood = "Very good"
    static let amazing = "Amazing"

    static func poorRating(forServer: Bool) -> String {
        return forServer == true ? poor : NSLocalizedString(poor, comment: "")
    }

    static func goodRating(forServer: Bool) -> String {
        return forServer == true ? good : NSLocalizedString(good, comment: "")
    }

    static func veryGoodRating(forServer: Bool) -> String {
        return forServer == true ? veryGood : NSLocalizedString(veryGood, comment: "")
    }

    static func amazingRating(forServer: Bool) -> String {
        return forServer == true ? amazing : NSLocalizedString(amazing, comment: "")
    }
}

To access

print(Rating.amazingRating(forServer: true))

Output

Amazing
Apogee
  • 689
  • 8
  • 19
  • @YinKiet Plz accept the answer if it helped you, as it will help other users having similar issue as yours. – Apogee Nov 24 '17 at 12:50
2
enum Rating : String{
    case bad = "bad"
    case good = "good"
    case excelent = "excelent"

    func localize() -> String{
        return NSLocalizedString(self.rawValue, comment: "")
    }

    func getRating(forServer isServer: Bool) -> String{
        if isServer{
            return localize()
        }else{
            return self.rawValue
        }
    }
}

And just use it:

let rate: Rating = .bad
let stringToPost = rate.getRating(forServer: true) //English version for server
let stringToShow = rate.getRating(forServer: false)//Localized version for UI
0
func getEnglishString(forKey key: String) -> String {
    if let currentLanguage = 
(NSUserDefaults.standardUserDefaults().arrayForKey(AppleLanguages)?.first as? 
String) {
    if currentLanguage == "en" {
        return NSLocalizedString(key, comment: "")
    }
    else {
        //the application is not currently on `en`
        //change application to `en`
        NSBundle.setLanguage("en")

        //get the localized string on `en`
        let enString = NSLocalizedString(key, comment: "")

        //return the application to the old language
        NSBundle.setLanguage(currentLanguage)

        return enString
    }
}

return ""
}

Maybe this will help. For further information you can follow this: https://stackoverflow.com/a/36863894/5167909

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50