From fc57e09840b069eba067c65cf823350208af31ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ars=C3=A9nio=20Costa?= Date: Thu, 27 Oct 2016 10:33:01 +0100 Subject: [PATCH 1/2] Added a set method to set a localizable object property specific language given its (languageKey, value) --- .../LocalizedContentExtensions.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs b/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs index 7cb0367..36b26b9 100644 --- a/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs +++ b/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs @@ -253,5 +253,38 @@ public static T Set(this T item, Expression> property, IDicti return Set(item, property, localizedContents); } + + /// + /// Sets an item's localized content property for a specific language, + /// Usefull to do updates on objects. + /// + /// + /// An ILocalizable item. + /// The property to set. + /// The language to set the content. + /// The value to set the content to. + /// + public static T Set(this T item, Expression> property, string lang, string value) where T : class, ILocalizable + { + var memberExpression = (MemberExpression)property.Body; + var propertyInfo = (PropertyInfo)memberExpression.Member; + + var content = new List(LocalizedContent.Deserialize(propertyInfo.GetValue(item) as string)); + + LocalizedContent update = content.FirstOrDefault(p => p.Key.Equals(lang)); + + if (update != null) + { + update.Value = value; + } + else + { + content.Add(new LocalizedContent(lang, value)); + } + + propertyInfo.SetValue(item, content.Serialize(), null); + + return item; + } } } \ No newline at end of file From ff4d67a49e9caf2c1505b64496eaee5261235fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ars=C3=A9nio=20Costa?= Date: Fri, 4 Nov 2016 14:10:25 +0000 Subject: [PATCH 2/2] Fixed issue with possible null --- src/OrangeJetpack.Localization/LocalizedContent.cs | 3 ++- .../LocalizedContentExtensions.cs | 7 +++++-- .../OrangeJetpack.Localization.csproj | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/OrangeJetpack.Localization/LocalizedContent.cs b/src/OrangeJetpack.Localization/LocalizedContent.cs index 8f286c2..bbdb72c 100644 --- a/src/OrangeJetpack.Localization/LocalizedContent.cs +++ b/src/OrangeJetpack.Localization/LocalizedContent.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; + +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; diff --git a/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs b/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs index 36b26b9..13b4116 100644 --- a/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs +++ b/src/OrangeJetpack.Localization/LocalizedContentExtensions.cs @@ -268,8 +268,11 @@ public static T Set(this T item, Expression> property, string { var memberExpression = (MemberExpression)property.Body; var propertyInfo = (PropertyInfo)memberExpression.Member; - - var content = new List(LocalizedContent.Deserialize(propertyInfo.GetValue(item) as string)); + + var itemValue = propertyInfo.GetValue(item); + var content = + new List( + LocalizedContent.Deserialize(itemValue != null ? itemValue as string : LocalizedContent.Init())); LocalizedContent update = content.FirstOrDefault(p => p.Key.Equals(lang)); diff --git a/src/OrangeJetpack.Localization/OrangeJetpack.Localization.csproj b/src/OrangeJetpack.Localization/OrangeJetpack.Localization.csproj index cdfab45..206c3fb 100644 --- a/src/OrangeJetpack.Localization/OrangeJetpack.Localization.csproj +++ b/src/OrangeJetpack.Localization/OrangeJetpack.Localization.csproj @@ -32,9 +32,9 @@ 4 - - ..\..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll - True + + False + ..\..\..\..\ECSaudeWindowsApplication\External Libs\Newtonsoft.Json.dll