iQuarc.DataLocalization 1.0.1

.NET Framework 4.5
There is a newer version of this package available.
See the version list below for details.
dotnet add package iQuarc.DataLocalization --version 1.0.1
NuGet\Install-Package iQuarc.DataLocalization -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="iQuarc.DataLocalization" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add iQuarc.DataLocalization --version 1.0.1
#r "nuget: iQuarc.DataLocalization, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install iQuarc.DataLocalization as a Cake Addin
#addin nuget:?package=iQuarc.DataLocalization&version=1.0.1

// Install iQuarc.DataLocalization as a Cake Tool
#tool nuget:?package=iQuarc.DataLocalization&version=1.0.1

iQuarc.DataLocalization

Data Localization is a helper library for Linq based data access frameworks that allows easy writing queries for retrieving localized data.

Overview

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.

Usage Patterns

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.

Getting Started

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)

! Important !

The translation of Linq queries is only performed if there actually is a linq projection, otherwise the query is unaffected.

iQuarc.AppBoot iQuarc.DataAccess

Changing the localization entity identifying key

In order to change the key used to identify the localization entity the following configurations can be used:

        LocalizationConfig.RegisterLocalizationEntity<Language>(l => l.LCID);
        LocalizationConfig.RegisterCultureMapper(c => c.LCID);

The example sets up the LCID to be the language idetifier, which is a numeric code part of [RFC5646] See Also, Microsoft LCID structure:

Product Versions
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 12,506 3/15/2018
1.0.1 1,207 10/4/2017
1.0.0 1,493 9/22/2016

Helper library for writing Data Localization Queries for Linq based data access technologies