Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/OrangeJetpack.Localization/LocalizedContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
Expand Down
36 changes: 36 additions & 0 deletions src/OrangeJetpack.Localization/LocalizedContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,41 @@ public static T Set<T>(this T item, Expression<Func<T, string>> property, IDicti

return Set(item, property, localizedContents);
}

/// <summary>
/// Sets an item's localized content property for a specific language,
/// Usefull to do updates on objects.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nitpick: "Usefull" --> "Useful"

/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item">An ILocalizable item.</param>
/// <param name="property">The property to set.</param>
/// <param name="lang">The language to set the content.</param>
/// <param name="value">The value to set the content to.</param>
/// <returns></returns>
public static T Set<T>(this T item, Expression<Func<T, string>> property, string lang, string value) where T : class, ILocalizable
{
var memberExpression = (MemberExpression)property.Body;
var propertyInfo = (PropertyInfo)memberExpression.Member;

var itemValue = propertyInfo.GetValue(item);
var content =
new List<LocalizedContent>(
LocalizedContent.Deserialize(itemValue != null ? itemValue as string : LocalizedContent.Init()));

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\ECSaudeWindowsApplication\External Libs\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
Expand Down