UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Loading Different JSON based on Locale

Forums > Swift

Good afternoon everyone,

I am starting to localize one of my apps now. That app will load different JSON data based on the locale. What is the best way to do that. I know I can use the following just not sure which one is the best to use:

  1. Locale.autoupdatingCurrent and Locale.current return 'en_US'
  2. Locale.current.languageCode returns 'en'

Im not sure which one of the above would be best. Any help would be appreciated.

Thanks Mark

3      

Hi Mark,

Is the JSON in the app bundle? if so, when you select the orginal JSON then Inspector panel (but on right) tick the boxes Localization and this will create the addtional JSON for each langauge, then it change this JSON for correct lanaguage. The system will select the locale depands on the user device.

PS This is assuming that you have orginal set up Localization for the rest of you app. If not you should do that first. How to localize your iOS app

3      

@NigelGee

Yes, JSON is in the app bundle. The problem, is that I have different JSON based on the language. In other words, the Spanish set of hymns is different than the English. Their are a different number of hymns and even different names for hymns so I can't use the English hymns JSON file as the base language.

Thats the reason I want to load the hymns looking at the locale. Is this possible? Which Locale would you use?

Thanks, Mark

3      

Hi Mark,

Are you saying that the struct are different for each language? If the key names do not change then it does not matter what in the value or how many items are in the json.

struct Hymns: Identifiable, Decodable {
    let id: String // unique id for for hymn number
    let title: String
    let verses: [String]
}

english json

[
    {
        "id": "1",
        "title": "Amazing Grace",
        "verses": [
            "verse one",
            "verse two",
            "verse three"
        ]
    }
]

spanish json

[
    {
        "id": "1",
        "title": "Ausente en un pesebre",
        "verses": [
            "verso uno",
            "verso dos",
            "verse three"
        ]
    },
    {
        "id": "2",
        "title": "Una vez en Royal David's City",
        "verses": [
            "verso uno",
            "verso dos",
            "verso tres"
        ]
    },
]

as you can see the keys eg id, title and verses are the same however the values are different. So when the device language (option>product>run) is set to Spanish is that json is called and when the language is set to default/english then the english version is run.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.