Orange Jetpack Localization is a library that simplifies the storage and retrieval of multi-language POCOs in a database. It provides interfaces and attributes for setting up localizable classes and extension methods for setting and getting objects and properties.
Original blog post:
https://andy.mehalick.com/2013/09/07/localizing-entity-framework-poco-properties-with-json-part-1/
Install-Package OrangeJetpack.Localization
Localization provides an interface and a property attribute for setting up classes that can be localized:
public class Planet : ILocalizable
{
[Localized]
public string Name { get; set; }
}
You can use the Set() extension method to set properties directly:
var planet = new Planet();
var name = new Dictionary<string, string>
{
{"en", "Earth"},
{"ru", "Земля"},
{"ja", "地球"},
{"ar", "أرض" }
};
planet.Set(i => i.Name, name);
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(Planet planet, LocalizedContent[] name)
{
planet.Set(i => i.Name, name);
//...
return View();
}
You can use Localize() extension methods to get a localized instance of an ILocalizable class or collection.
var planet = _db.Planets.SingleOrDefault(i => i.Id == 1).Localize("en");
var planets = _db.Planets.Localize<Planet>("en");
var planets = _db.Planets.Localize("en", i => i.Name);
// localizes only root objects (DEFAULT)
var planets = _db.Planets.Localize<Planet>("en", LocalizationDepth.Shallow);
// localizes only root objects and immediate children (properties and collections)
var planets = _db.Planets.Localize<Planet>("en", LocalizationDepth.OneLevel);
// localizes only root objects and all children recursively
var planets = _db.Planets.Localize<Planet>("en", LocalizationDepth.Deep);