/// <summary> /// Parse string key of each line into <see cref="ILine"/> by using <paramref name="lineFormat"/>. /// /// If parse fails, then skips the key, doesn't throw exception. /// </summary> /// <param name="lines"></param> /// <param name="lineFormat"><see cref="ILineFormatParser"/> parses string to line.</param> /// <param name="valueParser"></param> /// <returns>lines with <see cref="ILine"/> keys</returns> public static IEnumerable <ILine> ToLines(this IEnumerable <KeyValuePair <string, string> > lines, ILineFormat lineFormat, IStringFormatParser valueParser) { foreach (var line in lines) { ILine l; if (lineFormat.TryParse(line.Key, out l)) { yield return(line.Value == null ? l : l.String(valueParser.Parse(line.Value))); } } }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <returns></returns> public ILine GetLine(ILine key) { string id = namePolicy.Print(key); CultureInfo culture = null; key.TryGetCultureInfo(out culture); try { string value = culture == null?ResourceManager.GetString(id) : ResourceManager.GetString(id, culture); if (value == null) { return(null); } IString str = ValueParser.Parse(value); return(key.String(str)); } catch (Exception e) { throw new LocalizationException(id, e); //return null; } }
/// <summary> /// Convert <paramref name="lines"/> to asset key lines. /// </summary> /// <param name="lines"></param> /// <param name="lineFormat"></param> /// <param name="valueParser"></param> /// <returns></returns> public static IEnumerable <KeyValuePair <string, IString> > ToStringLines(this IEnumerable <KeyValuePair <ILine, string> > lines, ILineFormat lineFormat, IStringFormatParser valueParser) => lines.Select(line => new KeyValuePair <string, IString>((lineFormat ?? DefaultPolicy).Print(line.Key), valueParser.Parse(line.Value)));
/// <summary> /// Match <paramref name="key"/> to content in <see cref="IStringLocalizer"/>. /// </summary> /// <param name="key"></param> /// <returns></returns> public ILine GetLine(ILine key) { CultureInfo key_culture; if (key.TryGetCultureInfo(out key_culture)) { // Get-or-create culture specific adapter, use that if (this.culture == null) { var localizer = FindStringLocalizer(key, key_culture); if (localizer == null) { return(null); } if (localizer != this) { return(localizer.GetLine(key)); } } // Culture mismatch if (this.culture != null && !this.culture.Equals(key_culture)) { return(null); } } // Parse basename/location/type from key string _location, _basename, _type; int x = key.FindResourceInfos(out _type, out _basename, out _location); // If key has type, and this handler is also assigned to type, but they mismatch, don't strip the type name from key (below) if (x == 1) { if (this is Type type_assigned) { // This adapter is assigned to a specific type. // If a request is made to another type, if might be because it is needed in the name. // So lets not strip the type part from the name we will build (below) if (!_type.Equals(type_assigned.type.FullName)) { _type = null; } } else { // This adapter is not assigned to any Type. // If key has type hint, we can try to fetch for that full name. // Therefore lets not strip type part from the name we will build (below) _type = null; } } // If key has basename/location, and its different than this handler, then return null. if (x == 2) { if (this is Location location_assigned) { // This adapter is assigned for specific assembly/embed_basename. // If key has these hints, and they mismatch, we cant retrieve for the key. if (!_location.Equals(location_assigned.location) || (!_basename.Equals(location_assigned.basename))) { return(null); } } else { // This adapter is not assigned for any specific assembly/embed_basename. // If key has these hints, we can try to fetch for that full name. // Therefore lets not strip location part from the name (below). _location = null; _basename = null; } } // Build id // Strip: culture, and our typesection/asm section int length = 0; for (ILine k = key; k != null; k = k.GetPreviousPart()) { if (k is ILineParameterEnumerable lineParameters) { foreach (var lineParameter in lineParameters) { if (lineParameter.ParameterValue == _type || lineParameter.ParameterValue == _location || lineParameter.ParameterValue == _basename) { continue; // break; ? } string value = lineParameter.ParameterValue; if (lineParameter.ParameterName == "Culture" || string.IsNullOrEmpty(value)) { continue; } if (length > 0) { length++; } length += value.Length; } } if (k is ILineParameter parameter) { if (parameter.ParameterValue == _type || parameter.ParameterValue == _location || parameter.ParameterValue == _basename) { continue; // break; ? } string value = parameter.ParameterValue; if (parameter.ParameterName == "Culture" || string.IsNullOrEmpty(value)) { continue; } if (length > 0) { length++; } length += value.Length; } } char[] chars = new char[length]; int ix = length; for (ILine k = key; k != null; k = k.GetPreviousPart()) { if (k is ILineParameterEnumerable lineParameters) { foreach (var lineParameter in lineParameters) { if (lineParameter.ParameterValue == _type || lineParameter.ParameterValue == _location || lineParameter.ParameterValue == _basename) { continue; // break; ? } string value = lineParameter.ParameterValue; if (lineParameter.ParameterName == "Culture" || string.IsNullOrEmpty(value)) { continue; } if (ix < length) { chars[--ix] = '.'; } ix -= value.Length; value.CopyTo(0, chars, ix, value.Length); } } if (k is ILineParameter parameter) { if (parameter.ParameterValue == _type || parameter.ParameterValue == _location || parameter.ParameterValue == _basename) { continue; // break; ? } string value = parameter.ParameterValue; if (parameter.ParameterName == "Culture" || string.IsNullOrEmpty(value)) { continue; } if (ix < length) { chars[--ix] = '.'; } ix -= value.Length; value.CopyTo(0, chars, ix, value.Length); } } string id = new String(chars); LocalizedString str = stringLocalizer[id]; if (str.ResourceNotFound) { return(null); } return(key.String(ValueParser.Parse(str.Value))); }