private ReadOnlyDictionary <PluralCategories, string> GetUnitSubFormat(string unit, UnitDisplayFormat format, bool perUnit = false) { XDocument units = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.units.xml.gz"); XElement unitLength = units.XPathSelectElement(String.Format("/root/units[@locale = '{0}']/unitLength[@type = '{1}']", locale, IntlProviderOptions.ToStringValue(format))); if (unitLength == null) { return(this.Parent.GetUnitSubFormat(unit, format)); } if (CldrUtility.IsAlias(unitLength, out string type)) { return(GetUnitSubFormat(unit, IntlProviderOptions.ParseEnum <UnitDisplayFormat>(type), perUnit)); } bool hasInheritedValues = unitLength.Attribute("inherits") != null; if (unit == "per") { XElement perCompountUnit = unitLength.XPathSelectElement("compoundUnit[@type = 'per']/compoundUnitPattern"); if (perCompountUnit == null) { return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null); } return(CreateSingleResult(perCompountUnit.Value)); } XElement unitElm = unitLength.XPathSelectElement(String.Format("unit[substring-after(@type, '-') = '{0}']", unit)); if (unitElm == null) { return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null); } if (perUnit) { XElement perUnitPattern = unitElm.XPathSelectElement("perUnitPattern"); if (perUnitPattern == null) { return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null); } return(CreateSingleResult(perUnitPattern.Value)); } Dictionary <PluralCategories, string> dict = new Dictionary <PluralCategories, string>(); foreach (XElement unitPattern in unitElm.XPathSelectElements("unitPattern")) { dict[IntlProviderOptions.ParseEnum <PluralCategories>(unitPattern.Attribute("count").Value)] = unitPattern.Value; } if (unitElm.Attribute("inherits") != null) { ReadOnlyDictionary <PluralCategories, string> parent = this.Parent.GetUnitSubFormat(unit, format); CldrUtility.CopyPatternFromParent(dict, parent); } return(new ReadOnlyDictionary <PluralCategories, string>(dict)); }
public ReadOnlyDictionary <PluralCategories, NumberFormatPattern> GetUnitStyleFormat(string unit, UnitDisplayFormat format) { IDictionary <PluralCategories, string> pattern = GetUnitSubFormat(unit, format); if (pattern == null) { int pos = unit.IndexOf("-per-"); if (pos <= 0) { throw new ArgumentOutOfRangeException("unit"); } string nUnit = unit.Substring(0, pos); string dUnit = unit.Substring(pos + 5); string perUnitPattern = GetUnitSubFormat(unit, format, true)[PluralCategories.Other]; ReadOnlyDictionary <PluralCategories, string> nUnitPattern = GetUnitSubFormat(nUnit, format); if (perUnitPattern != null) { pattern = nUnitPattern.ToDictionary(v => v.Key, v => String.Format(perUnitPattern, v.Value)); } else { string compoundUnitPattern = GetUnitSubFormat("per", format)[PluralCategories.Other]; string dUnitPattern = GetUnitSubFormat(dUnit, format)[pluralRules.Match(1)]; pattern = nUnitPattern.ToDictionary(v => v.Key, v => String.Format(compoundUnitPattern, v.Value, dUnitPattern.Replace("{0}", "").Trim())); } } return(new ReadOnlyDictionary <PluralCategories, NumberFormatPattern>(pattern.ToDictionary(v => v.Key, v => new NumberFormatPattern(FormattedString.Parse(v.Value))))); }