{"id":2142,"date":"2017-01-09T03:18:37","date_gmt":"2017-01-09T08:18:37","guid":{"rendered":"http:\/\/www.rexfeng.com\/blog\/?p=2142"},"modified":"2017-01-09T03:18:37","modified_gmt":"2017-01-09T08:18:37","slug":"ios-10-locales-and-currency-symbols","status":"publish","type":"post","link":"https:\/\/www.rexfeng.com\/blog\/2017\/01\/ios-10-locales-and-currency-symbols\/","title":{"rendered":"iOS 10 Locales and Currency Symbols &#8211; Sample App"},"content":{"rendered":"<p>While working on adding localization to my <a href=\"https:\/\/itunes.apple.com\/us\/app\/tip-solver-premier-gratuity\/id1130814051?mt=8\">tip calculator<\/a>, one thing that seems obvious in retrospect\u00a0is the difference between a device&#8217;s language &amp; region. iOS lets you set the language &amp; region\u00a0separately. For example,\u00a0you might want to read\u00a0text in English, but you could be in Asia. This is relevant to tipping since you could travel\u00a0to a country where tipping is expected, but\u00a0the country\u00a0your phone&#8217;s language is associated with doesn&#8217;t traditionally tip.<\/p>\n<p>While exploring locales and currency symbols, I whipped together a basic demo app that lets you scroll between all the known locales and their currency symbol in iOS 10. This is pretty useful since you can quickly see what the <a href=\"https:\/\/developer.apple.com\/reference\/foundation\/numberformatter\/1414668-currencysymbol\">currencySymbol<\/a> is for each known iOS locale.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-2145\" src=\"http:\/\/www.rexfeng.com\/blog\/wp-content\/uploads\/2017\/01\/Simulator-Screen-Shot-Jan-9-2017-12.08.57-AM-304x540.png\" alt=\"\" width=\"304\" height=\"540\" srcset=\"https:\/\/www.rexfeng.com\/blog\/wp-content\/uploads\/2017\/01\/Simulator-Screen-Shot-Jan-9-2017-12.08.57-AM-304x540.png 304w, https:\/\/www.rexfeng.com\/blog\/wp-content\/uploads\/2017\/01\/Simulator-Screen-Shot-Jan-9-2017-12.08.57-AM-84x150.png 84w, https:\/\/www.rexfeng.com\/blog\/wp-content\/uploads\/2017\/01\/Simulator-Screen-Shot-Jan-9-2017-12.08.57-AM-169x300.png 169w, https:\/\/www.rexfeng.com\/blog\/wp-content\/uploads\/2017\/01\/Simulator-Screen-Shot-Jan-9-2017-12.08.57-AM-624x1110.png 624w, https:\/\/www.rexfeng.com\/blog\/wp-content\/uploads\/2017\/01\/Simulator-Screen-Shot-Jan-9-2017-12.08.57-AM.png 750w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/p>\n<p>Below is the full implementation of very hacked together (quick and dirty) code. All you need to do:<\/p>\n<ul>\n<li>Create new\u00a0Single View Application project in Xcode<\/li>\n<li>Replace the ViewController.swift with below (written for Swift 3)<\/li>\n<li>Run the app in Xcode<\/li>\n<\/ul>\n<pre>import UIKit\r\n\r\nclass ViewController: UIViewController, UITableViewDelegate {\r\n    \r\n    let cellIdentifier = \"Cell\"\r\n    let currentLocaleHeight = CGFloat(80)\r\n    \r\n    let locales = Locale.availableIdentifiers.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        \r\n        let tableView: UITableView = UITableView()\r\n        tableView.frame = CGRect(x: 0, y: currentLocaleHeight, width: view.frame.width, height: view.frame.height)\r\n        tableView.dataSource = self\r\n        tableView.delegate = self\r\n        \r\n        self.view.addSubview(tableView)\r\n        \r\n        addCurrentLocaleLabel()\r\n    }\r\n    \r\n    func addCurrentLocaleLabel() {\r\n        let local = Locale.current.identifier\r\n        \r\n        let width = view.frame.width\r\n        let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: currentLocaleHeight))\r\n        label.text = \"Current locale: \" + local\r\n        label.textAlignment = .center\r\n        view.addSubview(label)\r\n    }\r\n    \r\n}\r\n\r\nextension ViewController: UITableViewDataSource {\r\n    \r\n    func numberOfSections(in tableView: UITableView) -&gt; Int {\r\n        return 1\r\n    }\r\n    \r\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {\r\n        return locales.count\r\n    }\r\n    \r\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {\r\n        let cell = UITableViewCell(style: .value1, reuseIdentifier: cellIdentifier)\r\n        \r\n        let localeString = locales[indexPath.row]\r\n        \r\n        let numberFormatter = NumberFormatter()\r\n        numberFormatter.locale = Locale(identifier: localeString)\r\n        \r\n        cell.textLabel?.text = localeString\r\n        cell.detailTextLabel?.text = numberFormatter.currencySymbol\r\n        \r\n        return cell\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\n<p>Note that the &#8220;\u00a4&#8221; symbol means the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Currency_sign_(typography)\">currency is\u00a0unspecified<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on adding localization to my tip calculator, one thing that seems obvious in retrospect\u00a0is the difference between a device&#8217;s language &amp; region. iOS lets you set the language &amp; region\u00a0separately. For example,\u00a0you might want to read\u00a0text in English, but you could be in Asia. This is relevant to tipping since you could travel\u00a0to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1029],"tags":[1311,966,1310,946,1086],"class_list":["post-2142","post","type-post","status-publish","format-standard","hentry","category-programming","tag-currency","tag-ios","tag-locale","tag-symbol","tag-xcode"],"_links":{"self":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/comments?post=2142"}],"version-history":[{"count":6,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2142\/revisions"}],"predecessor-version":[{"id":2149,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2142\/revisions\/2149"}],"wp:attachment":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/media?parent=2142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/categories?post=2142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/tags?post=2142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}