How to do pluralization for structs #295
-
I have a Dice struct that I was hoping could work for the pluralization formatter: public struct Dice
{
public int Quantity;
public int NumberOfSides;
public int Bonus;
// new Dice(2, 6, 3) means 2d6+3 meaning roll two 6-sided dice and add 3 to the result
public Dice(int quantity, int numberOfSides, int bonus)
{
Quantity = quantity;
NumberOfSides = numberOfSides;
Bonus = bonus;
}
public int Min => Quantity + Bonus;
public int Max => (Quantity * NumberOfSides) + Bonus;
public override string ToString()
{
if (quantity > 0 && numberOfSides > 0)
{
if (bonus == 0)
{
// no bonus value (example: "2d6")
return $"{quantity}d{numberOfSides}";
}
else
{
// with bonus (example: "2d6+3", or "2d6-3")
return $"{quantity}d{numberOfSides}{bonus:+0;-#}";
}
}
// bonus value only, no dice roll
if (bonus != 0)
{
return bonus.ToString();
}
// note: negative value in dice roll ignored, treat it as zero
return "0";
}
} For the purposes of discussion, ignore any nonsensical values like I determine if a Dice value is plural by: public bool IsPlural => Max > 1; It doesn't matter if the lowest value it can produce is 1, the fact that it is capable of producing a value greater than 1 automatically means it's "plural". However I notice there doesn't seem to be any way for me to inform SmartFormat when my value is plural or not. Doesn't even seem like Right now I am just using the Dice Max value, forcing me to pass two values: Dice d = new Dice(2, 6, 3);
Smart.Format("Deal {1:plural(en):{0} point|{0} points} of damage.", d, d.Max); // "Deal 2d6+3 points of damage."
d = new Dice(0, 0, 1);
Smart.Format("Deal {1:plural(en):{0} point|{0} points} of damage.", d, d.Max); // "Deal 1 point of damage."
d = new Dice(0, 0, 2);
Smart.Format("Deal {1:plural(en):{0} point|{0} points} of damage.", d, d.Max); // "Deal 2 points of damage." This is what I was hoping it would do: // not possible, but should output "Deal 2d6+3 points of damage."
// gives: Error parsing format string: No suitable Formatter could be found at 19
Dice d = new Dice(2, 6, 3);
Smart.Format("Deal {0:plural(en):{} point|{} points} of damage.", d); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can't have a using NUnit.Framework;
namespace SmartFormat.Tests.Core;
public struct Dice
{
/*
* Original code does not compile - casing of properties
*/
public int Quantity;
public int NumberOfSides;
public int Bonus;
// new Dice(2, 6, 3) means 2d6+3 meaning roll two 6-sided dice and add 3 to the result
public Dice(int quantity, int numberOfSides, int bonus)
{
Quantity = quantity;
NumberOfSides = numberOfSides;
Bonus = bonus;
}
public int Min => Quantity + Bonus;
public int Max => (Quantity * NumberOfSides) + Bonus;
public override string ToString()
{
if (Quantity > 0 && NumberOfSides > 0)
{
if (Bonus == 0)
{
// no bonus value (example: "2d6")
return $"{Quantity}d{NumberOfSides}";
}
else
{
// with bonus (example: "2d6+3", or "2d6-3")
return $"{Quantity}d{NumberOfSides}{Bonus:+0;-#}";
}
}
// bonus value only, no dice roll
if (Bonus != 0) {
return Bonus.ToString();
}
// note: negative value in dice roll ignored, treat it as zero
return "0";
}
}
[TestFixture]
public class Discussion_295
{
[Test]
public void DiceTest()
{
var d = new Dice(2, 6, 3);
// Note: "ToString" can be omitted - it is called implicitly, as you saw in your code sample
var result = Smart.Format("Deal {0.Max:plural(en):{0.ToString} point|{0.ToString} points} of damage.", d);
// result: Deal 2d6+3 points of damage.
d = new Dice(0, 0, 1);
result = Smart.Format("Deal {0.Max:plural(en):{0.ToString} point|{0.ToString} points} of damage.", d);
// result: Deal 1 point of damage.
d = new Dice(0, 0, 2);
result = Smart.Format("Deal {0.Max:plural(en):{0.ToString} point|{0.ToString} points} of damage.", d);
// result: Deal 2 points of damage.
}
} |
Beta Was this translation helpful? Give feedback.
You can't have a
Dice
class instance as an argument to SmartFormat without including the properties that SmartFormat should evaluate in the format string. Hope this makes the missing part clear: