Reputation: 5763
I want to retrieve the localized name for a path on Mac OS X. I cobbled together the code below from various sources.
My Mac is currently set to the French locale, set French as the primary language, rebooted, I reset the LANG environment variable in the terminal to fr_FR.UTF-8, but I cannot seem to get the localized path for the folder (e.g. /Users/bll/Music). The locale seems to be working, the localized name does show up in the finder (Musique).
What am I missing here?
(I'm not a Mac programmer, not an objective-c programmer, don't speak french).
Edit: Updated with current code, Info.plist file
(I do understand the issues with display names with / characters embedded, just not going to worry about that at this time).
code:
#import "Foundation/NSObject.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSProcessInfo.h"
#include <stdio.h>
#include <stdlib.h>
#include <MacTypes.h>
int
main (int argc, const char * argv[])
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *path = @"";;
NSString *npath = @"";
NSArray *npathcomp;
NSUInteger count;
if (argc > 1) {
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
path = arguments[1];
npath = path;
if (*argv[1] == '/') {
npathcomp = [fm componentsToDisplayForPath:path];
count = npathcomp.count - 1;
npath = [[npathcomp subarrayWithRange:NSMakeRange (1,count)]
componentsJoinedByString:@"/"];
}
}
printf ("/%s\n", [npath UTF8String]);
return 0;
}
Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleAllowMixedLocalizations</key>
<true/>
<key>CFBundleDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleDisplayName</key>
<string>localizeddirname</string>
<key>CFBundleExecutable</key>
<string>localizeddirname</string>
<key>CFBundleGetInfoString</key>
<string>Copyright 2017 Brad Lanam, Walnut Creek CA USA</string>
<key>CFBundleIdentifier</key>
<string>org.bdj.localizeddirname</string>
<key>CFBundleName</key>
<string>localizedirname</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleVersion</key>
<string>1.0.0</string>
<key>LSMinimumSystemVersion</key>
<string>10.6.0</string>
</dict>
</plist>
Compilation:
cp -p localizeddirname.plist Info.plist
clang \
-v \
-mmacosx-version-min=10.9 \
-framework Cocoa \
-o localizeddirname \
-Wl,-sectcreate,__TEXT,__info_plist,Info.plist \
localizeddirname.m
rm -f Info.plist
Upvotes: 1
Views: 511
Reputation: 90641
As mentioned in the comments, the localized name is only appropriate for display to the user. You shouldn't use it for anything else.
Second, the display path is not the result of concatenating the display names for each item on the path. If you want the display path, you should use -[NSFileManager componentsToDisplayForPath:]
. Note that this gives you an array of display components.
It is not appropriate to concatenate these with slash (/) characters to compose a pseudo-path. That's in part because display names can contain slashes, unlike Unix-level path components. (On disk, those will be stored as colons (:).) The display components should be displayed as separate components (like in a pop-up menu showing current location, an outline-type list, or an NSPathControl
). If you must display the path in a single line, I'd separate the components with a graphical element, such as a right-pointing triangle image (or left-pointing for right-to-left languages).
To allow your tool to retrieve localized strings from the frameworks for locales it doesn't "support", you need to 1) embed an Info.plist into it in the __TEXT,__info_plist
section; and 2) include the CFBundleAllowMixedLocalizations
key, mapped to boolean true. To embed the Info.plist file, use the compiler option -Wl,-sectcreate,__TEXT,__info_plist,Info.plist
.
Upvotes: 1