1

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
Brad Lanam
  • 5,192
  • 2
  • 19
  • 29
  • If it can help, you can always access the music folder at `"/Users/bll/Music"` whatever the language the user sets (the translated name is only shown to the user for convenience, but the directory path is unchanged). See this [post](https://stackoverflow.com/a/28550564/4418092) – Jeffrey Mvutu Mabilama Sep 19 '17 at 23:30
  • 'displayNameAtPath' returns the localized names for the languages your app supports. Compare the open dialogs of Xcode and TextEdit. – Willeke Sep 19 '17 at 23:57
  • "...for the languages your app supports" : How do I change the program to indicate that it supports all languages? – Brad Lanam Sep 20 '17 at 02:04

1 Answers1

1

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • I have embedded an Info.plist file, but it is still not displaying the localized name. This is a command line program, is there something else that needs to be done? – Brad Lanam Sep 20 '17 at 15:07
  • Edit your question to show the Info.plist file you're embedding. And might as well show your current code, while you're at it. – Ken Thomases Sep 20 '17 at 15:26
  • Updated the question with current code, Info.plist, compilation. – Brad Lanam Sep 20 '17 at 15:49
  • Huh. It's worked for me for other framework localized strings, but doesn't seem to work for this. – Ken Thomases Sep 20 '17 at 18:15
  • I guess this has to be re-written to open the proper NIB file and do all the lookups. Or rewritten as a GUI program without a window (I have no clue on that, I'll try the first option). – Brad Lanam Sep 20 '17 at 19:18