Refine the logic for determining the "partial/full file size" string. It's better, but not perfect, depending on how languages are localized for special cases. A partial size of zero will now always hide the unit.

This commit is contained in:
Mitchell Livingston 2012-09-11 00:46:32 +00:00
parent d04bd9b8be
commit 4cce91e006
2 changed files with 14 additions and 7 deletions

View File

@ -318,7 +318,7 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy
[unitFormatter setAllowsNonnumericFormatting: NO];
[unitFormatter setAllowedUnits: NSByteCountFormatterUseKB];
kbString = [unitFormatter stringFromByteCount: 17]; //use a random value to avoid possible pluralization issues with 1 or 0
kbString = [unitFormatter stringFromByteCount: 17]; //use a random value to avoid possible pluralization issues with 1 or 0 (an example is if we use 1 for bytes, we'd get "byte" when we'd want "bytes" for the generic libtransmission value at least)
[unitFormatter setAllowedUnits: NSByteCountFormatterUseMB];
mbString = [unitFormatter stringFromByteCount: 17];

View File

@ -84,14 +84,21 @@
{
NSByteCountFormatter * fileSizeFormatter = [[NSByteCountFormatterMtLion alloc] init];
//only show units for the partial file size if it's different than the full file size's
[fileSizeFormatter setIncludesCount: NO];
const BOOL partialUnitsDifferent = ![[fileSizeFormatter stringFromByteCount: partialSize] isEqualToString: [fileSizeFormatter stringFromByteCount: fullSize]];
[fileSizeFormatter setIncludesCount: YES];
fullString = [fileSizeFormatter stringFromByteCount: fullSize];
[fileSizeFormatter setIncludesUnit: partialUnitsDifferent];
//figure out the magniture of the two, since we can't rely on comparing the units because of localization and pluralization issues (for example, "1 byte of 2 bytes")
BOOL partialUnitsSame;
if (partialSize == 0)
partialUnitsSame = YES; //we want to just show "0" when we have no partial data, so always set to the same units
else
{
//we have to catch 0 with a special case, so might as well avoid the math for all of magnitude 0
const unsigned int magnitudePartial = partialSize >= 1000 ? log(partialSize)/log(1000) : 0;
const unsigned int magnitudeFull = fullSize >= 1000 ? log(fullSize)/log(1000) : 0;
partialUnitsSame = magnitudePartial == magnitudeFull;
}
[fileSizeFormatter setIncludesUnit: !partialUnitsSame];
partialString = [fileSizeFormatter stringFromByteCount: partialSize];
[fileSizeFormatter release];