Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Swift – iOS – Read Language JSON File Into Controls

Posted on February 20, 2018 By David Kittell

If you have an app that doesn’t use the internet or can’t use the internet but still want basic multi-language capability the below will help.

First add some labels and buttons. In adding the buttons add it twice (once as IBOutlet and then IBAction)

Create a JSON file, in my example I named mine data.json

{
    "english":[
               {
               "Sunrise":"Sun Rise",
               "Sunset":"Sunset",
               "ShareBrief":"Share Brief",
               "ShareDetailed":"Share Detailed"
               }
               ],
    "spanish":[
               {
               "Sunrise":"Salida del Sol",
               "Sunset":"Puesta de Sol",
               "ShareBrief":"Compartir Breve",
               "ShareDetailed":"Compartir Detallado"
               }
               ],
    "french":[
              {
              "Sunrise":"Lever du Soleil",
              "Sunset":"Le Coucher du Soleil",
              "ShareBrief":"Partager Bref",
              "ShareDetailed":"Partager Détaillé"
              }
              ]
}
@IBAction func btnButtonTest(_ sender: Any) {
   GetLanguage(language: "english");
}
@IBOutlet weak var btnButtonTestLabel: UIButton!
    
@IBAction func btnButtonTest1(_ sender: Any) {
   GetLanguage(language: "spanish");
}
    
@IBOutlet weak var btnButtonTestLabel1: UIButton!

@IBOutlet weak var lblLabelTest: UILabel!
    
@IBOutlet weak var lblLabelTest1: UILabel!

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
     
        // Get device language
        let dLanguageLocale = NSLocale.preferredLanguages[0]
        print(dLanguageLocale);
        let dLanguage = dLanguageLocale.components(separatedBy: "-")
        
        switch (dLanguage[0])
        {
        case "en":
            GetLanguage(language: "english");
        case "es":
            GetLanguage(language: "spanish");
        case "fr":
            GetLanguage(language: "french");
        default:
            GetLanguage(language: "english");
        }        
    }

func GetLanguage(language: String)
    {
        if let path = Bundle.main.url(forResource: "data", withExtension: "json") {
            
            do {
                let jsonData = try Data(contentsOf: path, options: .mappedIfSafe)
                do {
                    if let jsonResult = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions(rawValue: 0)) as? NSDictionary {
                        if let personArray = jsonResult.value(forKey: language) as? NSArray {
                            for (_, element) in personArray.enumerated() {
                                if let element = element as? NSDictionary {
                                    let sSunrise = element.value(forKey: "Sunrise") as! String
                                    print(sSunrise)
                                    lblLabelTest4.text = sSunrise
                                    let sSunset = element.value(forKey: "Sunset") as! String
                                    print(sSunset)
                                    lblLabelTest5.text = sSunset
                                    let sBrief = element.value(forKey: "ShareBrief") as! String
                                    print(sBrief)
                                    btnButtonTestLabel.setTitle(sBrief, for: .normal)
                                    let sDetailed = element.value(forKey: "ShareDetailed") as! String
                                    print(sDetailed)
                                    btnButtonTestLabel1.setTitle(sDetailed, for: .normal)
                                }
                            }
                        }
                    }
                } catch let error as NSError {
                    print("Error: \(error)")
                }
            } catch let error as NSError {
                print("Error: \(error)")
            }
        }
    }
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code iOS Swift

Post navigation

Previous post
Next post

Related Posts

Last Read / Last Write

Posted on April 29, 2013October 26, 2015

Sometimes you need to know the last time a table or database was read from or written to, this code will help determine that for you. Originally Found At: http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/ USE <Database_name> SET ANSI_WARNINGS OFF; SET NOCOUNT ON; GO WITH agg AS ( SELECT last_user_seek, last_user_scan, last_user_lookup, last_user_update FROM sys.dm_db_index_usage_stats…

Read More

TaskKill App List

Posted on October 15, 2013October 26, 2015

taskkill /f /im pdfpro7hook.exe taskkill /f /im searchprotocolhost.exe taskkill /f /im sgnmaster.exe taskkill /f /im apmsgfwd.exe taskkill /f /im apntex.exe taskkill /f /im apoint.exe taskkill /f /im jusched.exe taskkill /f /im openvpn-gui.exe taskkill /f /im shstat.exe taskkill /f /im udaterui.exe taskkill /f /im hidfind.exe taskkill /f /im hkcmd.exe taskkill /f…

Read More

File Upload Web Service

Posted on March 28, 2013October 26, 2015

This is a File Upload Web Service that can be a wrapper for a process in between. Source code is an adaptation of this Microsoft article: HOW TO: Send and Receive Binary Documents by Using an ASP.NET Web Service and Visual C# .NET Upload Client using System; using System.Collections.Generic; using…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes
 

Loading Comments...
 

You must be logged in to post a comment.