//usr/bin/env SRC=$0 DST=$TMPDIR/${app=`basename ${0%.mm}`}.app/Contents/MacOS/$app ARGS="$*" /bin/bash -c ' \
    plutil -create xml1 ${plist=`dirname $DST`/../Info.plist} && \
        plutil -insert CFBundleDevelopmentRegion -string ${DEVELOPMENT_REGION:-en} $plist && \
        plutil -insert CFBundleLocalizations -array $plist && \
            localizations=(${LOCALIZATIONS-en no}); \
            for ((i=0; i<${#localizations[@]}; i++)) \
                do plutil -insert CFBundleLocalizations.$i -string ""${localizations[$i]}"" $plist; \
            done && \
        plutil -insert CFBundleAllowMixedLocalizations -bool ${ALLOW_MIXED:-YES} $plist && \
        plutil -insert CFBundleIdentifier -string "io.qt.dev.localization" $plist && \
    mkdir -p $(dirname $DST) && clang++ $SRC -o $DST -fmodules -fcxx-modules && \
    echo $DST && exec $DST $ARGS '; exit

@import AppKit;

/*
    Usage:

        chmod 755 localization.mm
        ALLOW_MIXED=NO LOCALIZATIONS="fr sv" DEVELOPMENT_REGION=en \
            ./localization.mm -AppleLanguages "(de,fi)" -AppleLocale "ja_JP
*/

int main()
{
    @autoreleasepool {

        // The standard user defaults are initialized from an ordered list of domains,
        // as documented by NSUserDefaults.standardUserDefaults. This includes e.g.
        // parsing command line arguments, such as -AppleFooBar "baz", as well as
        // global defaults. To ensure that these defaults are available through
        // the lower level Core Foundation preferences APIs, we need to initialize
        // them as early as possible via the Foundation-API, as the lower level APIs
        // do not do this initialization.
        (void)NSUserDefaults.standardUserDefaults;

        // Note: -AppleLanguages must NOT be space separated, i.e. must be "(fi,no)"

        NSLog(@"NSProcessInfo.processInfo.arguments %@", NSProcessInfo.processInfo.arguments);
        NSLog(@"NSBundle.mainBundle.infoDictionary[@\"CFBundleAllowMixedLocalizations\"]: %@",
            NSBundle.mainBundle.infoDictionary[@"CFBundleAllowMixedLocalizations"]);
        NSLog(@"NSBundle.mainBundle.infoDictionary[@\"CFBundleDevelopmentRegion\"]: %@",
            NSBundle.mainBundle.infoDictionary[@"CFBundleDevelopmentRegion"]);

        // The NSLocale current locale is the user's choice of Region in the preferences,
        // used for formatting dates, numbers, etc. Reflects the AppleLocale setting.
        NSLog(@"NSLocale.currentLocale.localeIdentifier %@\n", @[NSLocale.currentLocale.localeIdentifier]);
        // Same as NSLog(@"%@\n", CFLocaleGetIdentifier(CFLocaleCopyCurrent()));

        // The NSLocal's preferred languages is the user's list of preferred UI languages
        // Reflects the AppleLanguages setting.
        NSLog(@"NSLocale.preferredLanguages %@\n", NSLocale.preferredLanguages);
        // Same as NSLog(@"%@\n", CFLocaleCopyPreferredLanguages());

        // The main bundle's localizations is the localizations explicitly declared by the application,
        // either adding `<lang>.lproj` folders to the project, or by declaring CFBundleLocalizations
        // in the Info.plist
        NSLog(@"NSBundle.mainBundle.localizations %@\n", NSBundle.mainBundle.localizations);
        NSLog(@"NSBundle.mainBundle.preferredLocalizations %@\n", NSBundle.mainBundle.preferredLocalizations);

        NSBundle *appKit = [NSBundle bundleForClass:NSApplication.class];
        NSLog(@"NSBundle.appKit.localizations %@\n", appKit.localizations);
        NSLog(@"NSBundle.appKit.preferredLocalizations %@\n", appKit.preferredLocalizations);

        return 0;
    }
}