/// <summary>
 /// Get the resource string of resource path ralative to current <see cref="GeneratedResourceProvider"/>.
 /// </summary>
 /// <param name="resourceKey">The subpath name of resource path.</param>
 /// <returns>
 /// The resource string of resource path ralative to current <see cref="GeneratedResourceProvider"/>.
 /// </returns>
 public string GetValue(string resourceKey)
 {
     if (resourceKey == null)
     {
         return(this.Value);
     }
     return(LocalizedStrings.GetValue(Key + "/" + resourceKey));
 }
 /// <summary>
 /// Get the resource string of resource path ralative to current <see cref="ResourceProviderBase"/>.
 /// </summary>
 /// <param name="resourceKey">The subpath name of resource path.</param>
 /// <returns>
 /// The resource string of resource path ralative to current <see cref="ResourceProviderBase"/>.
 /// </returns>
 /// <exception cref="ArgumentNullException"><paramref name="resourceKey"/> is <see langword="null"/>.</exception>
 public string GetValue(string resourceKey)
 {
     if (resourceKey == null)
     {
         throw new ArgumentNullException(nameof(resourceKey));
     }
     return(LocalizedStrings.GetValue(this.path + resourceKey));
 }
예제 #3
0
        /// <summary>
        /// Get localized string of <see cref="Path"/>.
        /// </summary>
        /// <returns></returns>
        protected override object ProvideValue()
        {
            var r = LocalizedStrings.GetValue(Path);

            if (string.IsNullOrEmpty(r))
            {
                return(Path);
            }
            return(r);
        }
        private void init()
        {
            var type  = this.provider.GetType();
            var props = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                        .Select(prop => new { prop, path = prop.GetCustomAttribute <ResourcePathAttribute>() })
                        .Where(o => o.path != null)
                        .OrderBy(o => o.prop.PropertyType, Comparer <Type> .Create((a, b) =>
            {
                if (a == b)
                {
                    return(0);
                }
                if (a == typeof(string))
                {
                    return(1);
                }
                else if (b == typeof(string))
                {
                    return(-1);
                }
                return(StringComparer.Ordinal.Compare(a.ToString(), b.ToString()));
            }))
                        .ThenBy(o => o.path.Path)
                        .Select(o =>
            {
                var name = o.path.Path;
                var dot  = name.LastIndexOf('/');
                if (dot != -1)
                {
                    name = name.Substring(dot + 1);
                }
                var value = o.prop.GetValue(this.provider);
                if (value is string vs)
                {
                    return((ResourceView) new ResourceValueView
                    {
                        Name = name,
                        Value = vs,
                        ResourcePath = o.path
                    });
                }
                else
                {
                    return(new ResourcePathView
                    {
                        Name = name,
                        Value = value,
                        ResourcePath = o.path
                    });
                }
            });
            var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                          .Select(method => new { method, path = method.GetCustomAttribute <ResourcePathAttribute>() })
                          .Where(o => o.path != null)
                          .OrderBy(o => o.path.Path)
                          .Select(o =>
            {
                var name = o.method.Name;
                var dot  = name.LastIndexOf('.');
                if (dot != -1)
                {
                    name = name.Substring(dot + 1);
                }
                var value  = LocalizedStrings.GetValue(o.path.Path);
                var format = new FormattableResourceString(value);
                return((ResourceView) new ResourceFormatMethodView
                {
                    Name = name,
                    Value = value,
                    Format = format,
                    ResourcePath = o.path
                });
            });

            this.items = props.Concat(methods).ToArray();
        }