Skip to content

Commit

Permalink
🌍 Localize queries
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-araman committed Dec 15, 2021
1 parent fb2218d commit 5070a06
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Sources/MasKit/Controllers/StoreSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ extension StoreSearch {
URLQueryItem(name: "entity", value: "macSoftware"),
URLQueryItem(name: "term", value: appName),
]

if let country = country {
components.queryItems!.append(country)
}

return components.url
}

Expand All @@ -44,6 +49,24 @@ extension StoreSearch {
}

components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")]

if let country = country {
components.queryItems!.append(country)
}

return components.url
}

private var country: URLQueryItem? {
// CommerceKit and StoreFoundation don't seem to expose the region of the Apple ID signed
// into the App Store. Instead, we'll make an educated guess that it matches the currently
// selected locale in macOS. This obviously isn't always going to match, but it's probably
// better than passing no "country" at all to the iTunes Search API.
// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/
guard let region = Locale.autoupdatingCurrent.regionCode else {
return nil
}

return URLQueryItem(name: "country", value: region)
}
}

0 comments on commit 5070a06

Please # to comment.