Data Localization is a helper library for Linq based data access frameworks that allows easy writing queries for retrieving localized data.
The library provides a set of helper methods for querying localized data split in multiple tables. The library works by rewriting Linq that perform projections on the main table to retrieve data from localization tables when available.
For example let's consider the following database which contains a Categories
table and a CategoryLocalization
table with localized data for categories:
CREATE TABLE [Languages]
(
[ID] int NOT NULL IDENTITY(1,1) PRIMARY KEY,
[Name] nvarchar(255) NOT NULL,
[IsoCode] nchar(2) NOT NULL
)
CREATE TABLE [Categories]
(
[ID] int NOT NULL IDENTITY(1,1) PRIMARY KEY,
[Name] nvarchar(512) NOT NULL
)
CREATE TABLE [CategoryTranslations]
(
[ID] int NOT NULL IDENTITY(1,1) PRIMARY KEY,
[LanguageID] int NOT NULL FOREIGN KEY REFERENCES [Languages]([ID]),
[CategoryID] int NOT NULL FOREIGN KEY REFERENCES [Categories]([ID]),
[Name] nvarchar(512) NOT NULL
)
The Linq query to a list of categories without localized data would look like:
var categories = ctx.Categories
.Select(c => new
{
ID = c.ID,
Name = c.Name
}).ToList();
However to retrieve localized category names, for example the French version the query would need to look something like:
var categories = ctx.Categories
.Select(c => new
{
ID = c.ID,
Name = c.CategoryTranslations.Where(ct => ct.Language.IsoCode == "fr").Select(ct => ct.Name).FirstOrDefault() ?? c.Name
}).ToList();
This pattern of writing queries becomes very tedious and error prone in time. This is where iQuarc.DataLocalization comes in, a simple set of helper methods that enable simple Linq queries to be localized automatically.
Using iQuarc.DataLocalization
var categories = this.Categories
.Select(c => new
{
ID = c.ID,
Name = c.Name
})
.Localize(new CultureInfo("fr-Fr")
.ToList();
In the above query the .Localize(...)
method call will automatically replace Name = c.Name
in the projection with Name = c.CategoryTranslations.Where(ct => ct.Language.IsoCode == "fr").Select(ct => ct.Name).FirstOrDefault() ?? c.Name
as if the localized query was written manually.
Step 1. Install from Nuget: https://www.nuget.org/packages/iQuarc.DataLocalization/
Or from Command Line:
PM> Install-Package iQuarc.DataLocalization
Step 2. First step is to register a localization entity, typically this is done on the DbContext static constructor
static AppDbContext()
{
iQuarc.DataLocalization.LocalizationConfig.RegisterLocalizationEntity<Language>(l => l.IsoCode);
}
Step 3. Mark all entities that contain localized data with [TranslationFor]
attribute
[TranslationFor(typeof(Category))]
public class CategoryTranslation
{
public int ID {get;set;}
public int LanguageID {get;set;}
public int CategoryID {get;set;}
public string Name {get;set;}
...
}
Step 4. Use the .Localize()
extension method to localize queries containing projections (contain .Select()
calls that [project data to new entities)
The translation of Linq queries is only performed if there actually is a linq projection
, otherwise the query is unaffected.