cs193p – Project #1 Assignment #1 Internationalization

See page for author [Public domain], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

When running on a real device, which region is set to something having not a “.” as comma symbol, the app crashes. Strangely it does not crash on the simulator.

One way to cope with this behavior is to force the number formatter to use the US locale all the time:

           if let displayText = display.text {
                let numberFormatter = NSNumberFormatter()
                numberFormatter.locale = NSLocale(localeIdentifier: "en_US")
                if let displayNumber = numberFormatter.numberFromString(displayText) {
                    return displayNumber.doubleValue
                }
            }


A little bit more “difficult” but more reasonable – because when we start handling internationalization we can do it right from the beginning – is to use a localized comma button.

Start by adding two new properties. An outlet to link to the button view and string variable for the current comma symbol:

    @IBOutlet weak var decimalButton: UIButton!
    
    var userIsInTheMiddleOfTypingANumber = false
    let decimalSeparator = NSNumberFormatter().decimalSeparator!

Use those two properties to change the title of the button:

    
    override func viewDidLoad() {
        super.viewDidLoad()
        decimalButton.setTitle(decimalSeparator, forState: UIControlState.Normal)
    }

Where ever we used before “.” change it to use the new separator variable:

    @IBAction func appendDigit(sender: UIButton) {
        ...
            if (digit == decimalSeparator) && (display.text!.rangeOfString(decimalSeparator) != nil) { return }
            ...
            if (digit != decimalSeparator) && ((display.text == "0") || (display.text == "-0")) {
                ...
        ...
            if digit == decimalSeparator {
                display.text = "0" + decimalSeparator
            } ...
    }

Finally use the number formatter not only to get the display value, but also to set it, and adjust the history label to use the new decimal separator:

    var displayValue: Double? {
        ...
        set {
            if (newValue != nil) {
                let numberFormatter = NSNumberFormatter()
                numberFormatter.numberStyle = .DecimalStyle
                numberFormatter.maximumFractionDigits = 10
                display.text = numberFormatter.stringFromNumber(newValue!)
            } ...
                history.text = join(decimalSeparator, stack!.componentsSeparatedByString(".")) + " ="
            ...
    }

The complete code is available on GitHub.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

5 thoughts on “cs193p – Project #1 Assignment #1 Internationalization”

  1. Thanks Martin for supporting us folks! Your work is a goldmine for me since I wouldn’t get things done without.

    Although I didn’t found an task concerning internationalization in the first two assignments this is still a valuable advise.

    All the best
    Thorsten

  2. Thanks Martin for your great work.

    i am wondering ..
    why it was mentioned in the project notes, that the solution could be implemented in a single line of code ..
    do you think there a better solution from the this solution ?

    Regards
    Salem

    1. Dear Salem,
      most of the time single-line codes are not a good solution. Good codes does what it is intended to do, meets the requirements, and you are able to read it later on 😉
      Have fun
      Martin

      1. Thanks a Lot.I was stuck and wanted to see code to figure how to do this as later assignments are linked to this.You helped a person in need just remember that.Thanks for your effort.

Leave a Reply

Your email address will not be published. Required fields are marked *