private static void LocalizeProperties <T>(T item, string language) { var properties = item .GetType() .GetProperties() .Where(i => Attribute.IsDefined(i, typeof(LocalizedAttribute))); foreach (var propertyInfo in properties) { var propertyValue = propertyInfo.GetValue(item)?.ToString(); if (string.IsNullOrEmpty(propertyValue)) { continue; } LocalizedContent[] localizedContents; if (!LocalizedContent.TryDeserialize(propertyValue, out localizedContents)) { continue; } var contentForLanguage = GetContentForLanguage(localizedContents, language); propertyInfo.SetValue(item, contentForLanguage.Value, null); } }
private static void LocalizeProperties <T>(T item, string language, IEnumerable <Expression <Func <T, string> > > properties) where T : class, ILocalizable { foreach (var property in properties) { var memberExpression = (MemberExpression)property.Body; var propertyValue = property.Compile()(item); if (string.IsNullOrEmpty(propertyValue)) { continue; } LocalizedContent[] localizedContents; if (!LocalizedContent.TryDeserialize(propertyValue, out localizedContents)) { continue; } var contentForLanguage = GetContentForLanguage(localizedContents, language); var propertyInfo = (PropertyInfo)memberExpression.Member; propertyInfo.SetValue(item, contentForLanguage.Value, null); } }
/// <summary> /// Serializes a collection of LocalizedContent items to a JSON string. /// </summary> public static string Serialize(this IEnumerable <LocalizedContent> contents) { return(LocalizedContent.Serialize(contents)); }
/// <summary> /// Serializes a single LocalizedContent item to a JSON string. /// </summary> public static string Serialize(this LocalizedContent content) { var contents = new[] { content }; return(LocalizedContent.Serialize(contents)); }