-
-
Notifications
You must be signed in to change notification settings - Fork 62
Add a new unit of measurement
Converter NOW uses a units_converter package to convert units. So the first things is to submit a new PR to that repo. Then you have to enable your unit in Converter NOW, which is pretty easy.
Let's add a couple of new units (troy ounce and pennyweight) together: In units_converter I first have to figure out to what property my unit belong. Is it a length unit, a force unit, an energy unit,..? In our case this two units belongs to mass property. Ounce you have figured out the property we have to add the unit to the enum of the property. Our unit has a symbol? if yes we have to add it, otherwise you can avoid to insert it. Let's add our units to MASS enum:
enum MASS {
grams,
//...
pennyweights,
troy_ounces,
}
Let's add their symbols (I usually retrive this info from Wikipedia):
mapSymbols: {
MASS.grams: 'g',
//...
MASS.pennyweights: 'dwt',
MASS.troy_ounces: 'oz t',
},
Next we have to define what is the relation with another unit. For example, 1 centimeter = 0.01 meters, 1 degree fahreneit = 1.8 celsius + 32. 0.01 and 1.8 are product coeffient, 32 is a sum coefficient. In our case 1 pennyweight = 1.55517384 grams and 1 troy ounces = 20 pennyweights. So let's add it:
conversionTree: ConversionNode(name: MASS.grams, leafNodes: [
//...
ConversionNode(
coefficientProduct: 1.55517384, // A pennyweight is 1.55517384 times his parent (grams)
name: MASS.pennyweights,
leafNodes: [
ConversionNode(
coefficientProduct: 20, //A troy ounce is 20 times his parent (pennyweight)
name: MASS.troy_ounces,
),
],
),
]);
And finally let's add the two getter method:
Unit get pennyweights => getUnit(MASS.pennyweights);
Unit get troy_ounces => getUnit(MASS.troy_ounces);
Now we can PR to units_converter repo.
The hard work is finally done, now let's add this units to Converter NOW. The first thing we have to do is translate the unit to all the available languages (I usually use wikipedia for translation with the page of the unit of different languages). Let's edit all the app_xx.arb file (make sure the key is the same for each translation and follows the camelCase format). For example for app_en.arb:
"grams": "Grams",
//...
"pennyweights": "Pennyweights",
"troyOunces": "Troy ounces", //Make sure each line ends with a comma
Note: for currencies translations you can use the currencies names written here.
Next thing to do is to link the translation to the units of the other package. To do it we need to add 2 line of code to PropertyUnitList.dart file:
UnitUi(MASS.pennyweights, l10n.pennyweights, basePath + 'mass.png', PROPERTYX.MASS),
UnitUi(MASS.troy_ounces, l10n.troyOunces, basePath + 'mass.png', PROPERTYX.MASS),
And we are done! Let's compile the app and test if everything is ok and then let's PR this commit!
Thanks for your contribution! 💚