Request content in a specific locale?

Hi All,

I recently decided to remove my localizations for now because I'm requesting content from a lot of different sources (many of which only provides English text.

But with MusicKit I still receive the locale content. So basically I'm wondering if it's possible to set a locale for a MusicCatalogResourceRequest?

Answered by Frameworks Engineer in 713901022

Hello @hiddevdploeg,

MusicKit automatically attempts to load content from the Apple Music catalog using the language supported for the storefront of the user that is the closest match for the language the app is running in.

One of the main factors MusicKit uses to choose the language is the value of preferredLocalizations for the main bundle. If your app and your device are configured in such a way that this property returns an array where the first element is a string that begins with "en", and if the Apple Music API Storefront for the value of MusicDataRequest.currentCountryCode contains a value that also begins with "en" in the supportedLanguageTags or defaultLanguageTag attributes, then MusicKit will make sure to request content in English.

We use this sort of heuristic to minimize the chances of mixed language UI appearing in any given app that uses MusicKit.

Beyond this automatic heuristic, and to respond more directly to your question, no, MusicKit doesn't currently have any public API for third-party apps to specify which language tag they would like to use.

Since you're asking this question, I'm guessing our current heuristic is not giving you the results you're hoping for. So to triage this better, we need more information from you.

Please modify your app and add the following lines in a function marked as async throws:

let mainBundle = Bundle.main
print("Bundle.main.preferredLocalizations = \(mainBundle.preferredLocalizations)")
print("Bundle.main.localizations = \(mainBundle.localizations)")
print("Bundle.main.developmentLocalization = \(String(describing: mainBundle.developmentLocalization))")

let currentCountryCode = try await MusicDataRequest.currentCountryCode
print("MusicDataRequest.currentCountryCode = \(currentCountryCode)")

Then run your app, capture this output, and respond to this thread including a copy of this output.

Please also include a screenshot of what you see in Settings > General > Language & Region, for good measure.

Thanks,

Accepted Answer

Hello @hiddevdploeg,

MusicKit automatically attempts to load content from the Apple Music catalog using the language supported for the storefront of the user that is the closest match for the language the app is running in.

One of the main factors MusicKit uses to choose the language is the value of preferredLocalizations for the main bundle. If your app and your device are configured in such a way that this property returns an array where the first element is a string that begins with "en", and if the Apple Music API Storefront for the value of MusicDataRequest.currentCountryCode contains a value that also begins with "en" in the supportedLanguageTags or defaultLanguageTag attributes, then MusicKit will make sure to request content in English.

We use this sort of heuristic to minimize the chances of mixed language UI appearing in any given app that uses MusicKit.

Beyond this automatic heuristic, and to respond more directly to your question, no, MusicKit doesn't currently have any public API for third-party apps to specify which language tag they would like to use.

Since you're asking this question, I'm guessing our current heuristic is not giving you the results you're hoping for. So to triage this better, we need more information from you.

Please modify your app and add the following lines in a function marked as async throws:

let mainBundle = Bundle.main
print("Bundle.main.preferredLocalizations = \(mainBundle.preferredLocalizations)")
print("Bundle.main.localizations = \(mainBundle.localizations)")
print("Bundle.main.developmentLocalization = \(String(describing: mainBundle.developmentLocalization))")

let currentCountryCode = try await MusicDataRequest.currentCountryCode
print("MusicDataRequest.currentCountryCode = \(currentCountryCode)")

Then run your app, capture this output, and respond to this thread including a copy of this output.

Please also include a screenshot of what you see in Settings > General > Language & Region, for good measure.

Thanks,

Hi Joe,

Thank you for taking the time to respond.

Bundle.main.preferredLocalizations = ["en"]
Bundle.main.localizations = ["de", "es", "nl", "pt-PT", "en", "Base"]
Bundle.main.developmentLocalization = Optional("en")
MusicDataRequest.currentCountryCode = nl

This is what I get back from the code snippet above. I used to support more languages but I've removed them.

Looking at this response I noticed they were still considered to be active so I investigated it and noticed I still had some localized visuals. After removing them it seems to be fixed :)

Maybe it's an idea that MusicKit looks at the Bundle.main.preferredLocalizations instead of the Bundle.main.localizations? Anyway, thank you so much for this pointer :)

Hello @hiddevdploeg,

MusicKit already looks at both preferredLocalizations and localizations for the main bundle, and it actually already favors preferredLocalizations, which aligns with your suggestion.

I just ran a test with the specific values you created, and the results I get tell me that MusicKit should be requesting data in English from Apple Music API.

Hence, I don't currently see anything misbehaving, or behaving in a way that isn't aligned with preferredLocalizations when there is a good match in the current country code.

Best regards,

Request content in a specific locale?
 
 
Q