iQuarc.DataLocalization 1.0.4

.NET Core 2.0 .NET Standard 2.0 .NET Framework 4.5.1
dotnet add package iQuarc.DataLocalization --version 1.0.4
NuGet\Install-Package iQuarc.DataLocalization -Version 1.0.4
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.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add iQuarc.DataLocalization --version 1.0.4
#r "nuget: iQuarc.DataLocalization, 1.0.4"
#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.4

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

iQuarc.DataLocalization

Data Localization is a helper library for Linq based data access frameworks (Such as Entity Framework 6, Entity Framework Core, Linq2SQL, nHibernate, and others) 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 expression trees that perform projections on the main table, to join and 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 net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.0

    • No dependencies.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETStandard 2.0

    • 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,336 3/15/2018
1.0.1 1,183 10/4/2017
1.0.0 1,471 9/22/2016

- Added .NetCore2.0 and .Netstadard2.0 targets
- Fixed nested translation bug preventing translation of nested collections