public ICollection <DiscoveredResource> GetResources(Type target, string resourceKeyPrefix) { var enumType = Enum.GetUnderlyingType(target); var isHidden = target.GetCustomAttribute <HiddenAttribute>() != null; string GetEnumTranslation(MemberInfo mi) { var result = mi.Name; var displayAttribute = mi.GetCustomAttribute <DisplayAttribute>(); if (displayAttribute != null) { result = displayAttribute.Name; } return(result); } return(target.GetMembers(BindingFlags.Public | BindingFlags.Static) .Select(mi => { var isResourceHidden = isHidden || mi.GetCustomAttribute <HiddenAttribute>() != null; return new DiscoveredResource(mi, ResourceKeyBuilder.BuildResourceKey(target, mi.Name), DiscoveredTranslation.FromSingle(GetEnumTranslation(mi)), mi.Name, target, enumType, enumType.IsSimpleType(), isResourceHidden); }) .ToList()); }
public ICollection <DiscoveredResource> GetClassLevelResources(Type target, string resourceKeyPrefix) { var result = new List <DiscoveredResource>(); var resourceAttributesOnModelClass = target.GetCustomAttributes <ResourceKeyAttribute>().ToList(); if (!resourceAttributesOnModelClass.Any()) { return(result); } foreach (var resourceKeyAttribute in resourceAttributesOnModelClass) { result.Add(new DiscoveredResource(null, ResourceKeyBuilder.BuildResourceKey(resourceKeyPrefix, resourceKeyAttribute.Key, separator: string.Empty), DiscoveredTranslation.FromSingle(resourceKeyAttribute.Value), resourceKeyAttribute.Value, target, typeof(string), true)); } return(result); }
private static void AddTranslationScript(LocalizationResource existingResource, StringBuilder buffer, DiscoveredTranslation resource) { var existingTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == resource.Culture); if (existingTranslation == null) { buffer.Append($@" insert into LocalizationResourceTranslations (ResourceId, [Language], [Value]) values ({existingResource.Id}, '{resource.Culture}', N'{resource.Translation.Replace("'", "''")}') "); } else if (!existingTranslation.Value.Equals(resource.Translation)) { buffer.Append($@" update LocalizationResourceTranslations set [Value] = N'{resource.Translation.Replace("'", "''")}' where ResourceId={existingResource.Id} and [Language]='{resource.Culture}' "); } }
private IEnumerable <DiscoveredResource> DiscoverResourcesFromMember( Type target, object instance, MemberInfo mi, string resourceKeyPrefix, bool typeKeyPrefixSpecified, bool isHidden, string typeOldName = null, string typeOldNamespace = null) { // check if there are [ResourceKey] attributes var keyAttributes = mi.GetCustomAttributes <ResourceKeyAttribute>().ToList(); var resourceKey = ResourceKeyBuilder.BuildResourceKey(resourceKeyPrefix, mi.Name); var translation = GetResourceValue(instance, mi); Type declaringType = null; Type returnType = null; var isSimpleType = false; if (mi is PropertyInfo) { var info = (PropertyInfo)mi; declaringType = info.PropertyType; returnType = info.GetMethod.ReturnType; isSimpleType = returnType.IsSimpleType(); } else if (mi is FieldInfo) { var info = (FieldInfo)mi; declaringType = info.GetUnderlyingType(); returnType = info.GetUnderlyingType(); isSimpleType = returnType.IsSimpleType(); } if (!keyAttributes.Any()) { var isResourceHidden = isHidden || mi.GetCustomAttribute <HiddenAttribute>() != null; // try to understand if there is resource "redirect" - [UseResource(..)] var resourceRef = mi.GetCustomAttribute <UseResourceAttribute>(); if (resourceRef != null) { TypeDiscoveryHelper.UseResourceAttributeCache.TryAdd(resourceKey, ResourceKeyBuilder.BuildResourceKey(resourceRef.TargetContainer, resourceRef.PropertyName)); } else { var translations = DiscoveredTranslation.FromSingle(translation); var additionalTranslationsAttributes = mi.GetCustomAttributes <TranslationForCultureAttribute>(); if (additionalTranslationsAttributes != null && additionalTranslationsAttributes.Any()) { translations.AddRange(additionalTranslationsAttributes.Select(a => new DiscoveredTranslation(a.Translation, a.Culture))); } var oldResourceKeys = GenerateOldResourceKey(target, mi.Name, mi, resourceKeyPrefix, typeOldName, typeOldNamespace); yield return(new DiscoveredResource(mi, resourceKey, translations, mi.Name, declaringType, returnType, isSimpleType, isResourceHidden) { TypeName = target.Name, TypeNamespace = target.Namespace, TypeOldName = oldResourceKeys.Item2, TypeOldNamespace = typeOldNamespace, OldResourceKey = oldResourceKeys.Item1 }); } // try to fetch also [Display()] attribute to generate new "...-Description" resource => usually used for help text labels var displayAttribute = mi.GetCustomAttribute <DisplayAttribute>(); if (displayAttribute?.Description != null) { var propertyName = $"{mi.Name}-Description"; var oldResourceKeys = GenerateOldResourceKey(target, propertyName, mi, resourceKeyPrefix, typeOldName, typeOldNamespace); yield return(new DiscoveredResource(mi, $"{resourceKey}-Description", DiscoveredTranslation.FromSingle(displayAttribute.Description), propertyName, declaringType, returnType, isSimpleType) { TypeName = target.Name, TypeNamespace = target.Namespace, TypeOldName = oldResourceKeys.Item2, TypeOldNamespace = typeOldNamespace, OldResourceKey = oldResourceKeys.Item1 }); } var validationAttributes = mi.GetCustomAttributes <ValidationAttribute>(); foreach (var validationAttribute in validationAttributes) { if (validationAttribute.GetType() == typeof(DataTypeAttribute)) { continue; } var validationResourceKey = ResourceKeyBuilder.BuildResourceKey(resourceKey, validationAttribute); var propertyName = validationResourceKey.Split('.').Last(); var oldResourceKeys = GenerateOldResourceKey(target, propertyName, mi, resourceKeyPrefix, typeOldName, typeOldNamespace); yield return(new DiscoveredResource(mi, validationResourceKey, DiscoveredTranslation.FromSingle(string.IsNullOrEmpty(validationAttribute.ErrorMessage) ? propertyName : validationAttribute.ErrorMessage), propertyName, declaringType, returnType, isSimpleType) { TypeName = target.Name, TypeNamespace = target.Namespace, TypeOldName = oldResourceKeys.Item2, TypeOldNamespace = typeOldNamespace, OldResourceKey = oldResourceKeys.Item1 }); } // scan custom registered attributes (if any) foreach (var descriptor in ConfigurationContext.Current.CustomAttributes) { var customAttributes = mi.GetCustomAttributes(descriptor.CustomAttribute); foreach (var customAttribute in customAttributes) { var customAttributeKey = ResourceKeyBuilder.BuildResourceKey(resourceKey, customAttribute); var propertyName = customAttributeKey.Split('.').Last(); var oldResourceKeys = GenerateOldResourceKey(target, propertyName, mi, resourceKeyPrefix, typeOldName, typeOldNamespace); var foreignTranslation = string.Empty; if (descriptor.GenerateTranslation) { var z1 = customAttribute.GetType().ToString(); var z2 = customAttribute.ToString(); foreignTranslation = !z1.Equals(z2) ? z2 : propertyName; } yield return(new DiscoveredResource(mi, customAttributeKey, DiscoveredTranslation.FromSingle(foreignTranslation), propertyName, declaringType, returnType, isSimpleType) { TypeName = target.Name, TypeNamespace = target.Namespace, TypeOldName = oldResourceKeys.Item2, TypeOldNamespace = typeOldNamespace, OldResourceKey = oldResourceKeys.Item1 }); } } } foreach (var resourceKeyAttribute in keyAttributes) { yield return(new DiscoveredResource(mi, ResourceKeyBuilder.BuildResourceKey(typeKeyPrefixSpecified ? resourceKeyPrefix : null, resourceKeyAttribute.Key, separator: string.Empty), DiscoveredTranslation.FromSingle(string.IsNullOrEmpty(resourceKeyAttribute.Value) ? translation : resourceKeyAttribute.Value), null, declaringType, returnType, true) { FromResourceKeyAttribute = true }); } }