/// <inheritdoc/> public override void HandleCommandLocalizedString(StringFormatter formatter, StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, LocalizedString value) { if (value == null) throw new FormatException(NucleusStrings.FmtCmdInvalidForGeneratedStrings.Format("variant")); var variantName = arguments.GetArgument(0).Text; var variant = value.GetVariant(ref variantName); output.Append(variant); }
/// <inheritdoc/> public override void HandleCommandLocalizedString(StringFormatter formatter, StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, LocalizedString value) { var targetArgumentArg = arguments.GetArgument(0); var targetArgumentIndex = targetArgumentArg.GetValueAsInt32(); var targetMatchRuleArg = arguments.GetNextArgument(ref targetArgumentArg); var targetMatchRule = targetMatchRuleArg.Text; // Make sure our target is a localized string variant. var target = formatter.GetArgument(targetArgumentIndex); if (target.Reference == null || !(target.Reference is LocalizedStringVariant)) throw new FormatException(NucleusStrings.FmtCmdInvalidForArgument.Format("match", target.Reference?.GetType())); // Run the specified match evaluator. var match = Localization.MatchVariant(value, (LocalizedStringVariant)target.Reference, targetMatchRule); output.Append(match ?? "???"); }
internal LocalizedStringVariant(LocalizedString parent, String group, String value, IEnumerable<String> properties = null) { Contract.Require(parent, nameof(parent)); Contract.Require(group, nameof(group)); Contract.Require(value, nameof(value)); this.parent = parent; this.group = group; this.value = value; this.properties.Add(group); if (properties != null) { foreach (var property in properties) { this.properties.Add(property); } } }
/// <summary> /// Matches a source string to a target variant according to the specified culture and rule. /// </summary> private static String MatchVariantInternal(String culture, LocalizedString source, LocalizedStringVariant target, StringSegment rule) { Dictionary<String, LocalizationMatchEvaluator> registry; if (!registeredMatchEvaluators.TryGetValue(culture, out registry)) return null; foreach (var kvp in registry) { if (rule.Equals(kvp.Key)) { return (kvp.Value == null) ? null : kvp.Value(source, target); } } return null; }
/// <summary> /// Matches a source string to a target variant according to the specified culture and rule. /// </summary> private static String MatchVariantInternal(String culture, LocalizedString source, LocalizedStringVariant target, String rule) { Dictionary<String, LocalizationMatchEvaluator> registry; if (!registeredMatchEvaluators.TryGetValue(culture, out registry)) return null; LocalizationMatchEvaluator evaluator; if (!registry.TryGetValue(rule, out evaluator)) return null; return (evaluator == null) ? null : evaluator(source, target); }
/// <summary> /// Matches a source string to a target variant according to the specified culture and rule. /// </summary> /// <param name="culture">The culture for which to perform the match.</param> /// <param name="source">The source string.</param> /// <param name="target">The target string.</param> /// <param name="rule">The rule which defines how to perform the match.</param> /// <returns>The variant of <paramref name="source"/> which is the best match for <paramref name="target"/> according to the specified rule.</returns> public static String MatchVariant(String culture, LocalizedString source, LocalizedStringVariant target, StringSegment rule) { Contract.RequireNotEmpty(culture, "culture"); Contract.Require(target, "target"); return MatchVariantInternal(culture, source, target, rule); }
/// <summary> /// Matches a source string to a target variant according to the current culture and the specified rule. /// </summary> /// <param name="source">The source string.</param> /// <param name="target">The target string.</param> /// <param name="rule">The rule which defines how to perform the match.</param> /// <returns>The variant of <paramref name="source"/> which is the best match for <paramref name="target"/> according to the specified rule.</returns> public static String MatchVariant(LocalizedString source, LocalizedStringVariant target, StringSegment rule) { Contract.Require(target, "target"); return MatchVariantInternal(CurrentCulture, source, target, rule); }
public virtual void HandleCommandLocalizedString(StringFormatter formatter, StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, LocalizedString value) { throw new NotSupportedException(NucleusStrings.FmtCmdInvalidForArgument.Format(command, typeof(LocalizedString))); }
/// <summary> /// Matches a source string to a target variant according to the specified culture and rule. /// </summary> /// <param name="culture">The culture for which to perform the match.</param> /// <param name="source">The source string.</param> /// <param name="target">The target string.</param> /// <param name="rule">The rule which defines how to perform the match.</param> /// <returns>The variant of <paramref name="source"/> which is the best match for <paramref name="target"/> according to the specified rule.</returns> public static String MatchVariant(String culture, LocalizedString source, LocalizedStringVariant target, String rule) { Contract.RequireNotEmpty(culture, nameof(culture)); Contract.Require(target, nameof(target)); Contract.RequireNotEmpty(rule, nameof(rule)); return MatchVariantInternal(culture, source, target, rule); }
/// <summary> /// Creates a pseudolocalized copy of the specified source string. /// </summary> /// <param name="source">The source string to pseudolocalize.</param> /// <returns>A copy of the source string that is pseudolocalized.</returns> internal static LocalizedString CreatePseudolocalized(LocalizedString source) { Contract.Require(source, "source"); var pseudoString = new LocalizedString(Localization.PseudolocalizedCulture, source.Key, source.ContainsHtmlEncodedCharacters, source.PseudolocalizationDisabled); foreach (var variant in source.variants) { if (pseudoString.PseudolocalizationDisabled) { var pseudoVariant = new LocalizedStringVariant(pseudoString, variant.Value.Group, variant.Value.Value, variant.Value.Properties); pseudoString.variants[variant.Key] = pseudoVariant; } else { var pseudoText = String.Empty; if (pseudoString.ContainsHtmlEncodedCharacters) { pseudoText = Uri.UnescapeDataString(variant.Value.Value); pseudoText = PseudolocalizeString(pseudoText); pseudoText = Uri.EscapeDataString(pseudoText); } else { pseudoText = PseudolocalizeString(variant.Value.Value); } var pseudoVariant = new LocalizedStringVariant(pseudoString, variant.Value.Group, pseudoText, variant.Value.Properties); pseudoString.variants[variant.Key] = pseudoVariant; } } return pseudoString; }
/// <summary> /// Creates a fallback string in the event that the specified key does not exist for a culture. /// </summary> /// <param name="culture">The string's associated culture.</param> /// <param name="key">The string's localization key.</param> /// <returns>The fallback string that was created.</returns> internal static LocalizedString CreateFallback(String culture, String key) { Contract.RequireNotEmpty(key, "key"); Contract.RequireNotEmpty(culture, "culture"); var fallback = new LocalizedString(key, culture, true, false); fallback.variants["singular"] = new LocalizedStringVariant(fallback, "singular", key); return fallback; }
/// <summary> /// Creates a set of localized strings from the specified XML element. /// </summary> /// <param name="xml">The XML element that contains the string definition.</param> /// <param name="strings">The dictionary to populate with strings for each loaded culture.</param> /// <returns>The localization key for the created strings.</returns> internal static String CreateFromXml(XElement xml, Dictionary<String, LocalizedString> strings) { Contract.Require(xml, "xml"); Contract.Require(strings, "strings"); strings.Clear(); var key = xml.AttributeValueString("Key"); if (String.IsNullOrEmpty(key)) throw new InvalidDataException(NucleusStrings.LocalizedStringMissingKey); var html = xml.AttributeValueBoolean("Html") ?? false; var pseudo = xml.AttributeValueBoolean("Pseudo") ?? true; var cultures = xml.Elements(); foreach (var culture in cultures) { var cultureName = culture.Name.LocalName; var cultureString = new LocalizedString(cultureName, key, html, !pseudo); var stringPropertiesAttr = culture.Attribute("Properties"); if (stringPropertiesAttr != null) { var stringProperties = stringPropertiesAttr.Value.Split(',').Select(x => x.Trim()); foreach (var stringProperty in stringProperties) { cultureString.properties.Add(stringProperty, true); } } var variants = culture.Elements("Variant"); foreach (var variant in variants) { var variantGroup = variant.AttributeValueString("Group") ?? "none"; var variantValue = variant.Value; var variantProps = (variant.AttributeValueString("Properties") ?? String.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()); cultureString.variants[variantGroup] = new LocalizedStringVariant(cultureString, variantGroup, variantValue, variantProps); } strings[cultureName] = cultureString; } return key; }
/// <summary> /// Initializes a new instance of the LocalizedStringResult class. /// </summary> /// <param name="str">The string being examined.</param> internal LocalizedStringResult(LocalizedString str) { this.str = str; }