avoidwork / filesize.js

JavaScript library to generate a human readable String describing the file size
https://filesizejs.com
BSD 3-Clause "New" or "Revised" License
1.61k stars 97 forks source link

Localization API #96

Closed catamphetamine closed 5 years ago

catamphetamine commented 5 years ago

I can see that your library supports some localization via separator, symbols and fullforms. I guess, for Russian it would be something like:

filesize(..., {
  separator: ',',
  symbols: {
    B: "Б",
    KB: "кБ",
    MB: "мБ",
    ...
  },
  fullforms: [
    '',
    'кило',
    'мега',
    ...
  ]
})

You could make a directory called locales where people could submit pull requests with their languages.

import ru from 'filesize/locales/ru'
filesize(..., { locale: ru })

A similar API: https://github.com/sindresorhus/pretty-bytes#readme

// Localized output using German locale
prettyBytes(1337, {locale: 'de'});
//=> '1,34 kB'

Notice how they also use , in that example. That's because they use number.toLocaleString() instead of separator. They should have also checked for number.toLocaleString existence before using it and then fall back to a default separator. They also don't have kB translated.

These are localization API ideas you might want to review in some future if you decide on adding localization to this library. I don't need localization, I'm using en-US.

avoidwork commented 5 years ago

That would be a much better solution to #93, which is more or less a hack.

catamphetamine commented 5 years ago

It's still not a proper localization because it doesn't translate messages.

avoidwork commented 5 years ago

messages?

catamphetamine commented 5 years ago

A user passing locale: de property would expect the units to be translated into German.

catamphetamine commented 5 years ago

Also your code will break when there's no .toLocaleString().

avoidwork commented 5 years ago

those are separate features; a language pack of custom symbols or full form words can be loaded into the instance. providing all variations is something i wouldn't attempt for free.

catamphetamine commented 5 years ago

Then locale doesn't actually localize things.

avoidwork commented 5 years ago

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Browser_compatibility

the method has always existed, it just wasn't used in this lib.

catamphetamine commented 5 years ago

That table only shows the latest verions of browsers. The method couldn't "always exist" because it's not possible. Your code will still break in older browsers.

avoidwork commented 5 years ago

locale actually does it's job in regards to the number, e.g. 1000000.24' in 'de' would be1.000.000,24` which is only something you'd get for an exceptionally large number; majority will be under 3 significant digits.

catamphetamine commented 5 years ago

locale actually does it's job in regards to the number

locale only does it's job in regards to the number. You should state that it doesn't localize the units.

avoidwork commented 5 years ago

locale actually does it's job in regards to the number

locale only does it's job in regards to the number. You should state that it doesn't localize the units.

are you grasping what you're suggesting? a language pack for all permutations is unmanageable.

as per your claim of support... https://caniuse.com/#feat=internationalization

catamphetamine commented 5 years ago

a language pack for all permutations is unmanageable.

Nothing "unmanageable" in that.

as per your claim of support... https://caniuse.com/#feat=internationalization

As per your claim of support: the link clearly shows that it's not supported in all browsers. So your code will break.

avoidwork commented 5 years ago

i don't care if my code breaks in a deprecated browser that only runs in an OS that's 10+ years out dated. I just don't care. I don't care if you want this feature to work on modern browsers and netscape 1.0.

You're not paying me money, so I don't care.

catamphetamine commented 5 years ago

I don't care about what you care and don't care. You're a low-skill develoepr.

avoidwork commented 5 years ago

And yet you opened this ticket, and it gets over 4 million installs a week.

I don't care if you care or not. I did you a favor.