views:

17

answers:

1

I got a file size in byte in a (unsigned long long) variable. Is there a standard way to convert it to a string, with localized units?

I am aware of those kind of solutions :

NSString * stringFromFileSize( FileSize theSize )
{
    float floatSize = theSize;
    if (theSize<1023)
        return ([NSString stringWithFormat:@"%i bytes",theSize]);
    floatSize = floatSize / 1024;
    if (floatSize<1023)
        return ([NSString stringWithFormat:@"%1.1f KB",floatSize]);
    floatSize = floatSize / 1024;
    if (floatSize<1023)
        return ([NSString stringWithFormat:@"%1.1f MB",floatSize]);
    floatSize = floatSize / 1024;

    return([NSString stringWithFormat:@"%1.1f GB",floatSize]);
}

But it doesn't work in French, where 'octet' is used instead of bytes (and all B from the units have to be replaced by an 'o'). I've searched around CFLocalGetValue, looked at all formatting methods I know about without any success, so is there any hidden method/function to help me? I can manage to build something by hand, but I would prefere avoiding it.

+2  A: 

Sounds like a job for NSLocalizedString. You can add an entry to your French localization (in Localizable.strings):

"bytes" = "octets";
"KB" = "Ko";
"MB" = "Mo";
"GB" = "Go";

Then use:

return [NSString stringWithFormat:@"%i %@", theSize, NSLocalizedString(@"bytes", nil)];

To more specifically answer the question, I'm not aware of any Cocoa function for handling these conversions.

mipadi
That's what I was planning to do if I didn't found a 'standard' way to do it. Finder do it with no problem, it's weird the platform doesn't give it for free like for the dates :-/
Raoul Supercopter
The Finder does the same thing. You can find a lot of similar strings in Apple frameworks' .lproj folders.
Costique