gentooboontoo / js-quantities

JavaScript library for quantity calculation and unit conversion
http://gentooboontoo.github.io/js-quantities/
MIT License
396 stars 102 forks source link

Translation of units into different languages #126

Open andresgutgon opened 2 years ago

andresgutgon commented 2 years ago

Hi, first of all thanks for this. Is super helpful and looks super complete!

I was reading the issues and search in all repo but I didn't see any references to localization. Is there something related with the translation of the units?

Could be a translations folder. And people could initialize like formatter

import enLocale from 'qt/translations/en.json'
import esLocale  from 'qt/translations/es.json'
const LOCALES = {
  en: enLocale,
  es: esLocale
}
function init (locale: string) {
  Qty.translations =  LOCALES[locale] || LOCALES.en
}

Then you can pass the type of format. We could have long/short format. And also take into account plurals. 1 meter vs 2 meters

I would introduce a new method to

new Qty(1, 'm').localizedFormat({ format: 'long' }) 
// 1 meter

// Change to Spanish
Qty.translations =  LOCALES.es 

new Qty(2, 'm').localizedFormat({ format: 'long' }) // you can omit "targetUnit" if you want the same
// 2 metros
new Qty(2, 'm').localizedFormat({ targetUnit: 'cm', format: 'long' }) 
// 200 centímetros

// Short version by default
new Qty(2, 'm').localizedFormat({ targetUnit: 'cm' }) 
// 200 cm

I realize maybe what I want is doable by myself with Qty.formatter but I think could be nice to have these translations in the repo shared and maintained by the community.

What do you think? : )