Programmatic way to find the language of an application

Hello, I would like to know if there is any way to programmatically find out the current language of an application(either budle info, plist,any function from CoreFoundation, etc ). Only information I found relating to this is in CFBundleDevelopmentRegion from plist of the app but its not reliable in all cases. We are coding in C++ but we can also run Objective-C code. Thank you in advance!

I used to have a string "CurrentLanguage" in my localisation text file. E.g. in the english version I have "CurrentLanguage" = "en"; Then I just have to localise "CurrentLanguage" and I have the answer.

It’s not clear what you’re looking for here:

  • Is this code running within an app and you’re looking to find the language that the app is running in?

  • Or is this code running in some other context and you’re looking for find the language that some other app is running in?

  • Or are you working an app on disk, one that’s not currently running, and you’re looking to find the language that it would run in?

Also, what platform are you targeting?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

You can get the current-running application language with:

Bundle.main.preferredLocalizations.first

Bundle.main gets you application bundle. preferredLocalizations is a list of languages your application bundle supports in user-preferred order. The first element will be the language your application is running in.

Thank you, guys, for your answers. @eskimo we are running an application and we would like to know the language of another application, independent of our application. This application might be running or not.

We are targeting Apple x64 machines, both ARM64 and Intel processors.

we are running an application and we would like to know the language of another application, independent of our application. This application might be running or not.

I don’t think there’s a supported way to do that.

To start, the answer depends on user preferences, so the question makes sense only within the context of a given user. But even with that limitation there are still problems.

macOS has always allowed a user to choose a preferred list of languages, and you can combine that preferred list with the list of languages supported by the app to come up with the language that the app will run in by default. However, modern versions of macOS also let the user override that list for specific apps — in System Preferences > Language & Region > Apps — and there’s no API to query that state.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I see, thank you @eskimo for the help!

Hi @eskimo, it seems this solution

Bundle.main gets you application bundle. preferredLocalizations is a list of languages your application bundle supports in user-preferred order. The first element will be the language your application is running in.

is not working as expected, is returning English everytime because English is the first element, even if application language is French, for example. Here is the source code, could you please take a look at it? Not sure if we are doing something wrong or there is something wrong in the CoreFoundation side. Many thanks for your help!

CFArrayRef cfAllLanguages = CFBundleCopyBundleLocalizations(cfBundle)
CFArrayRef cfPrefLang = CFBundleCopyPreferredLocalizationsFromArray(cfAllLanguages); --- this should return language of the application but it returns En each time
CFArrayRef cfLocalPrefLang = CFBundleCopyLocalizationsForPreferences(cfAllLanguages,cfPrefLang);
CFArrayRef cfDefaultLang = CFBundleCopyLocalizationsForPreferences(cfAllLanguages,NULL);
CFStringRef lss = (CFStringRef)CFArrayGetValueAtIndex(cfAllLanguages, 0);

Thank you!

It’s hard to say what’s going on with your code because I don’t know where you’re running it or against which bundle. However, here’s an example taht shows what k13a and I are talking about:

// Get all the languages returned by TextEdit.

let u = URL(fileURLWithPath: "/System/Applications/TextEdit.app")
let b = Bundle(url: u)!
let bundleLocs = b.localizations
print(bundleLocs)       // -> ["de", "he", …]

// Get my preferred list of languages.

let myLocs = Locale.preferredLanguages
print(myLocs)           // -> ["en-GB"]

// Determine which of the languages in the bundle are preferred by the user.

let preferredLocs = Bundle.preferredLocalizations(from: bundleLocs, forPreferences: myLocs)
print(preferredLocs)    // -> ["en_GB", "en"]

// Now imagine a better version of Quinn, one who paid more attention during
// high school German, and thus has the Mac set to German.

let myLocs2 = ["de_DE", "en-GB"]

let preferredLocs2 = Bundle.preferredLocalizations(from: bundleLocs, forPreferences: myLocs2)
print(preferredLocs2)   // -> ["de"]

I tested this with Xcode 13.3.1 running on macOS 12.3.1.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi @eskimo, thank you for your help, is now clear to us! Have a good day!

Programmatic way to find the language of an application
 
 
Q