// Return true if the oldPattern has been updated private bool PlayPattern(IPattern oldPattern, IPattern newPattern) { if (newPattern == null) { return(false); } if (oldPattern != null && oldPattern.GetType() == typeof(RepeatablePattern)) { if (newPattern.GetType() == typeof(RepeatablePattern) && ((RepeatablePattern)oldPattern).IsRepeating()) { //new pattern is also repeatable, so old one can be updated List <IPattern> patterns = new List <IPattern>(); patterns.AddRange(newPattern.GetSimplePatterns()); MultiPattern mp = new MultiPattern(patterns); ((RepeatablePattern)oldPattern).UpdatePattern(mp); return(true); } else { //new pattern is not repeatable, so old one needs to be stopt and replaced ((RepeatablePattern)oldPattern).Stop(); Main.vibrationDevice.PlayPattern(newPattern); return(false); } } else { Main.vibrationDevice.PlayPattern(newPattern); return(false); } }
public void PluralsIcu() { const string PATTERN = "I have {file, plural =0 {no cars} one {{0} car} other {{0} cars}}."; Same(MultiPattern.Format(PATTERN, 0), "I have no cars."); Same(MultiPattern.Format(PATTERN, 1), "I have 1 car."); Same(MultiPattern.Format(PATTERN, 2), "I have 2 cars."); }
public void SelectIcu() { const string PATTERN = "I have {file, select car {cars} bike {bikes} ski {skis}}."; Same(MultiPattern.Format(PATTERN, "car"), "I have cars."); Same(MultiPattern.Format(PATTERN, "bike"), "I have bikes."); Same(MultiPattern.Format(PATTERN, "ski"), "I have skis."); }
public void GendersLegacy() { const string GENDER = "neutral;{0} will bring the car;male;{0} will bring his car;female;{0} will bring her car"; Same(MultiPattern.Format(GENDER, Gender.Male, "John"), "John will bring his car"); Same(MultiPattern.Format(GENDER, Gender.Female, "Jill"), "Jill will bring her car"); Same(MultiPattern.Format(GENDER, Gender.Neutral, "Somebody"), "Somebody will bring the car"); }
public void GendersIcu() { const string GENDER = "{name, gender, neutral {{0} will bring the car} male {{0} will bring his car} female {{0} will bring her car}}"; Same(MultiPattern.Format(GENDER, Gender.Male, "John"), "John will bring his car"); Same(MultiPattern.Format(GENDER, Gender.Female, "Jill"), "Jill will bring her car"); Same(MultiPattern.Format(GENDER, Gender.Neutral, "Somebody"), "Somebody will bring the car"); }
public void PluralsLegacy() { const string FORMAT = "zero;I have no cars;one;I have one car;other;I have {0} cars"; Same(MultiPattern.Format(FORMAT, 0), "I have no cars"); Same(MultiPattern.Format(FORMAT, 1), "I have one car"); Same(MultiPattern.Format(FORMAT, 2), "I have 2 cars"); }
private void calculateButton_Click(object sender, RoutedEventArgs e) { double time = Distance / Speed; int hours = (int)time; int minutes = (int)Math.Round(60 * (time - hours)); resultLabel.Content = MultiPattern.FormatMulti(Properties.Resources.ResultPlural, hours, minutes); }
private void Process(int count, Label label1, Label label2, Label label3, Label label4, Label label5) { label1.Text = MultiPattern.Format(Properties.Resources.MessagePlural, count, count); label2.Text = MultiPattern.Format(Properties.Resources.ZeroMessagePlural, count, count); label3.Text = MultiPattern.Format(Properties.Resources.TwoMessagePlural, count, count); label4.Text = MultiPattern.Format(Properties.Resources.Two2MessagePlural, count, count); label5.Text = MultiPattern.Format(Properties.Resources.TextMessagePlural, count, count); }
private void calculateButton_Click(object sender, EventArgs e) { double time = Distance / Speed; int hours = (int)time; int minutes = (int)Math.Round(60 * (time - hours)); // Use plural enabled multi pattern Format instead of the plain string.Format resultLabel.Text = MultiPattern.FormatMulti(Properties.Resources.ResultPlural, hours, minutes); }
private void Pattern(uint count, string format, string pattern, string id = "") { if (id != "") { Language.Id = id; } string thisPattern = MultiPattern.GetPattern(format, count); Assert.IsTrue(thisPattern == pattern); }
private void Form(uint count, Plural plural, string id = "") { if (id != "") { language = id; } PluralProc func = MultiPattern.GetProc(new CultureInfo(language)); Assert.IsTrue(func(count, (int)count, 0, 0, 0, 0) == plural); }
public void OnPost() { Message = ""; if (Speed > 0) { double time = Distance / Speed; int hours = (int)time; int minutes = (int)Math.Round(60 * (time - hours)); Message = MultiPattern.FormatMulti(localizer["Driving time is{plural, zero { } one { {0} hour } other { {0} hours }}{plural, one {{0} minute} other {{0} minutes}}"], hours, minutes); //loc 0: Hours or minutes } }
public void OperatorLessThanIcu() { const string VALUE = "I have {file, plural <5 {a few cars} >=5 {many cars} >=10 {plenty of cars}}."; Same(MultiPattern.Format(VALUE, 0), "I have a few cars."); Same(MultiPattern.Format(VALUE, 1), "I have a few cars."); Same(MultiPattern.Format(VALUE, 2), "I have a few cars."); Same(MultiPattern.Format(VALUE, 3), "I have a few cars."); Same(MultiPattern.Format(VALUE, 4), "I have a few cars."); Same(MultiPattern.Format(VALUE, 5), "I have many cars."); Same(MultiPattern.Format(VALUE, 6), "I have many cars."); Same(MultiPattern.Format(VALUE, 10), "I have plenty of cars."); Same(MultiPattern.Format(VALUE, 11), "I have plenty of cars."); }
protected void calculateButton_Click(object sender, EventArgs e) { if ((Distance >= 0) && (Speed > 0)) { double time = Distance / Speed; uint hours = (uint)time; uint minutes = (uint)Math.Round(60 * (time - hours)); resultLabel.Text = MultiPattern.FormatMulti(Driving.Resources.ResultPlural, hours, minutes); } else { resultLabel.Text = Driving.Resources.YouMustEnterValue; } }
private void ProcessPluralUnaware(uint count, Label label) { // On most languages this does not work except when count is 1 // Do not use code like this! label.Text = String.Format(Properties.Resources.File, count); if (MultiPattern.IsSingleFormLanguage() || (count == 1) || ((count == 0) && MultiPattern.IsZeroLikeOne())) { label.ForeColor = Color.Green; } else { label.ForeColor = Color.Red; } }
public void OperatorIcu() { const string VALUE = "I have {file, plural =0 {no cars} =1 {one car} =2 {two cars} 3..4 {few cars} ~12 {dozen cars} other {{0} cars}}."; Same(MultiPattern.Format(VALUE, 0), "I have no cars."); Same(MultiPattern.Format(VALUE, 1), "I have one car."); Same(MultiPattern.Format(VALUE, 2), "I have two cars."); Same(MultiPattern.Format(VALUE, 3), "I have few cars."); Same(MultiPattern.Format(VALUE, 4), "I have few cars."); Same(MultiPattern.Format(VALUE, 5), "I have 5 cars."); Same(MultiPattern.Format(VALUE, 10), "I have 10 cars."); Same(MultiPattern.Format(VALUE, 11), "I have dozen cars."); Same(MultiPattern.Format(VALUE, 12), "I have dozen cars."); Same(MultiPattern.Format(VALUE, 13), "I have dozen cars."); }
public IActionResult Index(DrivingModel driving) { if (driving.Speed > 0) { var time = driving.Distance / driving.Speed; var hours = (int)time; var minutes = (int)Math.Round(60 * (time - hours)); driving.Message = MultiPattern.FormatMulti(localizer["Driving time is{plural, zero { } one { {0} hour } other { {0} hours }}{plural, one {{0} minute} other {{0} minutes}}"], hours, minutes); //loc 0: Hours or minutes } else { driving.Message = ""; } return(View(driving)); }
private void Process(Gender gender, string name, Label messageLabel, Label infoLabel) { // Update message label messageLabel.Text = MultiPattern.Format(Properties.Resources.OtherMessageGender, gender, name); // Update info label var actualGender = MultiPattern.GetGender(Properties.Resources.OtherMessageGender, gender); if (actualGender != gender) { infoLabel.Font = new Font(infoLabel.Font, FontStyle.Bold); } else { infoLabel.Font = new Font(infoLabel.Font, FontStyle.Regular); } infoLabel.Text = PatternNames.GetGenderName(actualGender); }
public void MultiLegacy() { const string MULTI = "male;{0} will bring his {1};female;{0} will bring her {1};next;one;car.;other;{0} cars."; Language.Id = "en"; Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Male, Value = "John" }, 2), "John will bring his 2 cars."); Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Male, Value = "Bill" }, 1), "Bill will bring his car."); Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Female, Value = "Jill" }, 3), "Jill will bring her 3 cars."); Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Female, Value = "Alma" }, 1), "Alma will bring her car."); }
public void MultiIcu() { const string MULTI = "{name, gender, male {{0} will bring his} female {{0} will bring her}} {cars, plural, one {car} other {{0} cars}}."; Language.Id = "en"; Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Male, Value = "John" }, 2), "John will bring his 2 cars."); Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Male, Value = "Bill" }, 1), "Bill will bring his car."); Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Female, Value = "Jill" }, 3), "Jill will bring her 3 cars."); Same( MultiPattern.FormatMulti( MULTI, new { Gender = Gender.Female, Value = "Alma" }, 1), "Alma will bring her car."); }
private void ProcessHomeBrewed(uint count, Label label) { // This works on some Western languages (those that use similar plural rules as English) // but would fail for example in French. // Do not use code like this! if (count == 1) { label.Text = String.Format(Properties.Resources.File, count); } else { label.Text = String.Format(Properties.Resources.Files, count); } if (!MultiPattern.IsSingleFormLanguage() && (count == 0) && MultiPattern.IsZeroLikeOne()) { label.ForeColor = Color.Red; } else { label.ForeColor = Color.Green; } }
private void ProcessMultiPlural(uint completed, int total, Label label) { label.Text = MultiPattern.FormatMulti(Properties.Resources.MessagePlural, completed, total); label.ForeColor = Color.Green; }
// The following two samples handle plural forms correctly. Use this kind of code in your applications. private void ProcessPluralAware(int count, Label label) { label.Text = MultiPattern.Format(Properties.Resources.FilesPlural, count, count); label.ForeColor = Color.Green; }
public string GetPlayers(Sport sport) { return(MultiPattern.FormatMulti(localizer["{plural, one {{0} player} other {{0} players}}"], sport.Players)); //loc 0: Number of players }
private static string Format(double value, AbbreviatedNumberForm form, int precision, AbbreviationRule[] rules) { foreach (var rule in rules) { if (rule.Range <= value) { var range = rule.Range; var result = rule.Value; var p = result.IndexOf('0'); result = result.Remove(p, 1); while ((result != "") && (result[p] == '0')) { result = result.Remove(p, 1); range = range / 10; } string str; double displayValue; if (result != "") { str = GetStr(value / range, precision); displayValue = Convert.ToDouble(str); } else { str = GetNotAbbreviatedStr(value, form, precision); displayValue = value; } uint count = (uint)Math.Round(displayValue); if (displayValue == count) { //var proc = MultiPattern.GetIndexProc(); var plural = MultiPattern.GetProc()(count, (int)count, 0, 0, 0, 0); if (plural != rule.Plural) { // Wrong plural form. Find the correct one and get abbreviation again var newRule = Find(rule.Range, plural, rules); if (newRule != null) { range = newRule.Range; result = newRule.Value; p = result.IndexOf('0'); result = result.Remove(p, 1); while ((result != "") && (result[p] == '0')) { result = result.Remove(p, 1); range = range / 10; } str = GetStr(value / range, precision); } } } result = result.Substring(0, p) + str + result.Substring(p); p = result.IndexOf('¤'); if (p >= 0) { result = result.Substring(0, p) + NumberFormatInfo.CurrentInfo.CurrencySymbol + result.Substring(p + 1); } return(result); } } return(GetNotAbbreviatedStr(value, form, precision)); }