|
| 1 | +// |
| 2 | +// MCatalog+Language.swift |
| 3 | +// MusadoraKit |
| 4 | +// |
| 5 | +// Created by Claude on 05/08/26. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// `LanguageTagResponse` is a struct that decodes the response from the best supported language endpoint. |
| 11 | +/// |
| 12 | +/// This structure corresponds to the response you would receive when asking the Apple Music API |
| 13 | +/// for the best supported language tag of a storefront. |
| 14 | +/// |
| 15 | +/// - Note: Apple's documentation refers to this object as `LangageTagResponse` (sic); |
| 16 | +/// MusadoraKit spells it correctly. |
| 17 | +struct LanguageTagResponse: Decodable { |
| 18 | + /// The results container returned by the endpoint. |
| 19 | + let results: Results |
| 20 | + |
| 21 | + /// The container holding the resolved language tag. |
| 22 | + struct Results: Decodable { |
| 23 | + /// The best language tag supported by the storefront, as an RFC 4646 language tag. |
| 24 | + let tag: String |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +public extension MCatalog { |
| 29 | + /// Fetches the best supported language tag for a storefront of Apple Music. |
| 30 | + /// |
| 31 | + /// Pass the language tags your app accepts, in order of preference, and the Apple Music API |
| 32 | + /// resolves them against the storefront's supported language tags server-side. The returned |
| 33 | + /// tag is the best match, ready to use as the localization (`l`) parameter in subsequent |
| 34 | + /// catalog requests. |
| 35 | + /// |
| 36 | + /// Example usage: |
| 37 | + /// |
| 38 | + /// ```swift |
| 39 | + /// do { |
| 40 | + /// let languageTag = try await MCatalog.bestLanguageTag(for: "in", accepting: ["hi", "en-GB"]) |
| 41 | + /// print(languageTag) // "hi" |
| 42 | + /// } catch { |
| 43 | + /// print("Failed to fetch the best language tag: \(error)") |
| 44 | + /// } |
| 45 | + /// ``` |
| 46 | + /// |
| 47 | + /// In the above example, "in" is the identifier for the India storefront, and the accepted |
| 48 | + /// languages prefer Hindi over British English. |
| 49 | + /// |
| 50 | + /// - Parameters: |
| 51 | + /// - storefront: The identifier for the storefront you want to resolve the language for. This is usually a country code. |
| 52 | + /// - languages: The language tags your app accepts, in order of preference. |
| 53 | + /// - localization: An optional localization to use for the request. Defaults to `nil`. |
| 54 | + /// - Returns: The best language tag supported by the storefront, as an RFC 4646 language tag. |
| 55 | + /// - Throws: `MusadoraKitError.languageTagsMissing` if `languages` is empty, or an error if there was a problem with the network request or decoding the response. |
| 56 | + static func bestLanguageTag(for storefront: String, accepting languages: [String], localization: String? = nil) async throws -> String { |
| 57 | + let url = try bestLanguageTagURL(storefront: storefront, languages: languages, localization: localization) |
| 58 | + |
| 59 | + let request = MusicDataRequest(urlRequest: .init(url: url)) |
| 60 | + let response = try await request.response() |
| 61 | + let languageTag = try JSONDecoder().decode(LanguageTagResponse.self, from: response.data) |
| 62 | + |
| 63 | + return languageTag.results.tag |
| 64 | + } |
| 65 | + |
| 66 | + /// Fetches the best supported language tag for a storefront of Apple Music, |
| 67 | + /// based on the user's preferred languages. |
| 68 | + /// |
| 69 | + /// This convenience method uses `Locale.preferredLanguages` — the user's preferred |
| 70 | + /// languages, in order — as the accepted language tags. |
| 71 | + /// |
| 72 | + /// Example usage: |
| 73 | + /// |
| 74 | + /// ```swift |
| 75 | + /// do { |
| 76 | + /// let languageTag = try await MCatalog.bestLanguageTag(for: "in") |
| 77 | + /// print(languageTag) |
| 78 | + /// } catch { |
| 79 | + /// print("Failed to fetch the best language tag: \(error)") |
| 80 | + /// } |
| 81 | + /// ``` |
| 82 | + /// |
| 83 | + /// - Parameter storefront: The identifier for the storefront you want to resolve the language for. This is usually a country code. |
| 84 | + /// - Returns: The best language tag supported by the storefront, as an RFC 4646 language tag. |
| 85 | + /// - Throws: `MusadoraKitError.languageTagsMissing` if `Locale.preferredLanguages` is empty, or an error if there was a problem with the network request or decoding the response. |
| 86 | + static func bestLanguageTag(for storefront: String) async throws -> String { |
| 87 | + try await bestLanguageTag(for: storefront, accepting: Locale.preferredLanguages) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +extension MCatalog { |
| 92 | + /// Returns the URL for fetching the best supported language tag for the specified storefront. |
| 93 | + /// |
| 94 | + /// - Parameters: |
| 95 | + /// - storefront: The identifier for the storefront. |
| 96 | + /// - languages: The language tags to accept, in order of preference. |
| 97 | + /// - localization: An optional localization to use for the request. |
| 98 | + /// |
| 99 | + /// - Returns: The URL for fetching the best supported language tag for the specified storefront. |
| 100 | + internal static func bestLanguageTagURL(storefront: String, languages: [String], localization: String?) throws -> URL { |
| 101 | + guard !languages.isEmpty else { |
| 102 | + throw MusadoraKitError.languageTagsMissing |
| 103 | + } |
| 104 | + |
| 105 | + var urlComponents = AppleMusicURLComponents() |
| 106 | + urlComponents.path = "language/\(storefront)/tag" |
| 107 | + |
| 108 | + var queryItems = [URLQueryItem(name: "acceptLanguage", value: languages.joined(separator: ","))] |
| 109 | + |
| 110 | + if let localization, !localization.isEmpty { |
| 111 | + queryItems.append(URLQueryItem(name: "l", value: localization)) |
| 112 | + } |
| 113 | + |
| 114 | + urlComponents.queryItems = queryItems |
| 115 | + |
| 116 | + guard let url = urlComponents.url else { |
| 117 | + throw URLError(.badURL) |
| 118 | + } |
| 119 | + |
| 120 | + return url |
| 121 | + } |
| 122 | +} |
0 commit comments